Python celery.utils.log.get_task_logger() Examples

The following are 19 code examples of celery.utils.log.get_task_logger(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module celery.utils.log , or try the search function .
Example #1
Source File: mgmt.py    From esdc-ce with Apache License 2.0 6 votes vote down vote up
def __call__(self, *args, **kwargs):
        self.logger = get_task_logger('que.mgmt')
        task = 'MgmtTask %s%s' % (self.name, args[:2])
        tidlock = kwargs.pop('tidlock', None)
        check_user_tasks = kwargs.pop('check_user_tasks', False)
        kwargs.pop('cache_result', None)
        kwargs.pop('cache_timeout', None)
        kwargs.pop('nolog', None)
        tid = self.request.id

        if tidlock:
            task_lock = TaskLock(tidlock, desc=task, reverse_key=tid, logger=self.logger)
        else:
            task_lock = NoLock()

        try:
            if check_user_tasks:  # Wait for task to appear in UserTasks - bug #chili-618
                UserTasks.check(tid, logger=self.logger)  # Will raise an exception in case the task does not show up

            task_lock.task_check()  # Will raise an exception in case the lock does not exist

            return super(MgmtTask, self).__call__(tid, *args, **kwargs)  # run()
        finally:
            task_lock.delete() 
Example #2
Source File: test_celery.py    From pylogctx with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_task():
    from pylogctx import context

    app = Celery(task_cls=LoggingTask)

    @app.task
    def my_task():
        context.update(taskField='RUNNED')
        logger = get_task_logger(current_task.name)
        logger.info("I log!")
        return context.as_dict()

    result = my_task.apply()
    if VERSION.major < 4:
        result.maybe_reraise()
    else:
        result.maybe_throw()
    fields = result.result
    assert 'taskField' in fields
    assert not context.as_dict() 
Example #3
Source File: test_celery.py    From pylogctx with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_old_task():
    from pylogctx import context

    app = Celery(task_cls=OldLoggingTask)

    @app.task
    def my_task():
        logger = get_task_logger(current_task.name)
        logger.info("I log!")
        return context.as_dict()

    result = my_task.apply()
    if VERSION.major < 4:
        result.maybe_reraise()
    else:
        result.maybe_throw()
    fields = result.result
    assert 'taskField' in fields
    assert not context.as_dict() 
Example #4
Source File: tasks.py    From lost with MIT License 5 votes vote down vote up
def delete_pipe(pipe_id):
    logger = get_task_logger(__name__)
    logger.info("DELETED BY CELERY {}".format(pipe_id))
    lostconfig = LOSTConfig()
    dbm = DBMan(lostconfig)
    pipeline.delete(dbm, pipe_id)
    dbm.close_session() 
Example #5
Source File: celery_ext.py    From toptal-blog-celery-toy-ex with MIT License 5 votes vote down vote up
def log(self):
        logger = get_task_logger(self.name)
        return logger 
Example #6
Source File: mgmt.py    From esdc-ce with Apache License 2.0 5 votes vote down vote up
def __call__(self, *args, **kwargs):
        self.logger = get_task_logger('que.mgmt')
        from api.exceptions import OPERATIONAL_ERRORS

        try:
            return super(MgmtCallbackTask, self).__call__(*args, **kwargs)  # run()
        except OPERATIONAL_ERRORS as exc:
            self.logger.warning('Execution of mgmt callback task failed because of an operational error: %s', exc)
            self.retry(exc=exc)  # Will raise special exception


# noinspection PyAbstractClass 
Example #7
Source File: tasks.py    From esdc-ce with Apache License 2.0 5 votes vote down vote up
def __call__(self, cmd, *args, **kwargs):
        self.logger = get_task_logger('que.tasks')
        self.all_done = False
        task = 'Task %s("%s")' % (self.name, cmd)
        lock = kwargs.pop('lock', False)
        block = kwargs.pop('block', None)
        check_user_tasks = kwargs.pop('check_user_tasks', False)
        tid = self.request.id
        blocked = False

        if lock:
            task_lock = TaskLock(lock, desc=task, reverse_key=tid, logger=self.logger)
        else:
            task_lock = NoLock()

        try:
            if check_user_tasks:  # Wait for task to appear in UserTasks - bug #chili-618
                UserTasks.check(tid, logger=self.logger)  # Will raise an exception in case the task does not show up

            task_lock.task_check()  # Will raise an exception in case the lock does not exist

            if block and redis.exists(block):
                blocked = True
                self.retry(exc=TaskRetry(None))  # Will raise special exception

            return super(MetaTask, self).__call__(cmd, *args, **kwargs)  # run()
        finally:
            if not blocked:  # Lock must _not_ be deleted when failing on retry
                task_lock.delete() 
Example #8
Source File: alerttask.py    From MozDef with Mozilla Public License 2.0 5 votes vote down vote up
def log(self):
        return get_task_logger("%s.%s" % (__name__, self.alert_name)) 
Example #9
Source File: tasks.py    From django-wechat-example with MIT License 5 votes vote down vote up
def refresh_wechat_token(self, appid):
    """
    刷新已授权公众号
    """
    logger = get_task_logger('refresh_wechat_token')
    wechat = Wechat.objects.get(appid=appid)
    if not wechat.authorized:
        logger.error('公众号{0}失去授权'.format(wechat.appid))
        return None
    try:
        result = wechat.client.fetch_access_token()
        logger.info(result)
    except Exception as e:
        logger.error(u'刷新已授权公众号{0}失败:{1}'.format(appid, str(e))) 
Example #10
Source File: tasks.py    From django-wechat-example with MIT License 5 votes vote down vote up
def refresh_all_wechat_token(self):
    """
    定时1小时,刷新所有已授权公众号
    """
    logger = get_task_logger('refresh_all_wechat_token')
    for wechat in Wechat.objects.exclude(appid=settings.TEST_APPID).all():
        if not wechat.authorized:
            logger.error('公众号{0}失去授权'.format(wechat.appid))
            continue
        refresh_wechat_token.delay(wechat.appid) 
Example #11
Source File: tasks.py    From django-wechat-example with MIT License 5 votes vote down vote up
def process_wechat_query_auth_code_test(FromUserName, query_auth_code):
    """
    处理发布前微信的自动化测试query_auth_code
    """
    logger = get_task_logger('process_wechat_query_auth_code_test')
    logger.info(FromUserName)
    logger.info(query_auth_code)
    component = get_component()
    client = component.get_client_by_authorization_code(query_auth_code)
    client.message.send_text(FromUserName, query_auth_code+'_from_api') 
Example #12
Source File: logger.py    From lightflow with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_logger(name):
    """ Helper function to return a  valid logger object

    Args:
        name (str): The name of the logger. Typically: __name__.

    Returns:
        Logger: A logger object for sending messages to the logging system
    """
    return get_task_logger(name) 
Example #13
Source File: tasks.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_logger(name):
    logger = get_task_logger(name)
    return logger 
Example #14
Source File: tasks.py    From udata with GNU Affero General Public License v3.0 5 votes vote down vote up
def log(self):
        return get_task_logger(self.name) 
Example #15
Source File: cron.py    From lost with MIT License 5 votes vote down vote up
def __init__(self, dbm, pipe, lostconfig):
        '''
        :type dbm: lost.db.access.DBMan
        :type pipe: lost.db.model.Pipe
        '''
        super().__init__(dbm=dbm, pipe=pipe)
        self.lostconfig = lostconfig #type: lost.logic.config.LOSTConfig
        self.file_man = FileMan(self.lostconfig)
        # self.logger = lost.logic.log.get_file_logger(
        #     'Executor: {}'.format(self.lostconfig.env), 
        #     self.file_man.app_log_path)
        self.logger = get_task_logger(__name__) 
Example #16
Source File: worker.py    From lost with MIT License 5 votes vote down vote up
def send_life_sign():
    logger = get_task_logger(__name__)
    lostconfig = LOSTConfig()
    dbm = DBMan(lostconfig)
    worker = dbm.get_worker(lostconfig.worker_name)
    if worker is None:
        register_worker(dbm, lostconfig)
        logger.info('Registered worker: {}'.format(lostconfig.worker_name))
    else:
        worker.timestamp = datetime.utcnow()
        dbm.add(worker)
        dbm.commit()
        #logger.info('Sent lifesign: {}'.format(worker.worker_name))
    dbm.close_session() 
Example #17
Source File: purecollector.py    From pureelk with Apache License 2.0 5 votes vote down vote up
def __init__(self, ps_client, es_client, array_context):
        self._ps_client = ps_client;
        self._es_client = es_client;
        self._array_name = array_context.name
        self._array_id = array_context.id
        self._data_ttl = array_context.data_ttl
        self.logger = get_task_logger(__name__) 
Example #18
Source File: cron.py    From lost with MIT License 4 votes vote down vote up
def celery_exec_script(pipe_element_id):
    try:
        # Collect context information for celery task
        logger = get_task_logger(__name__)
        lostconfig = LOSTConfig()
        dbm = DBMan(lostconfig)
        pipe_e = dbm.get_pipe_element(pipe_e_id=pipe_element_id)
        worker = CurrentWorker(dbm, lostconfig)
        if not worker.enough_resources(pipe_e.script):
            logger.warning('Not enough resources! Rejected {} (PipeElement ID {})'.format(pipe_e.script.path, pipe_e.idx))
            return
        pipe_e.state = state.PipeElement.IN_PROGRESS
        dbm.save_obj(pipe_e)
        file_man = FileMan(lostconfig)
        pipe = pipe_e.pipe

        cmd = gen_run_cmd("pudb3", pipe_e, lostconfig)
        debug_script_path = file_man.get_instance_path(pipe_e)
        debug_script_path = os.path.join(debug_script_path, 'debug.sh')
        with open(debug_script_path, 'w') as sfile:
            sfile.write(cmd)

        cmd = gen_run_cmd("python3", pipe_e, lostconfig)
        start_script_path = file_man.get_instance_path(pipe_e)
        start_script_path = os.path.join(start_script_path, 'start.sh')
        with open(start_script_path, 'w') as sfile:
            sfile.write(cmd)
        p = subprocess.Popen('bash {}'.format(start_script_path), stdout=subprocess.PIPE,
            stderr=subprocess.PIPE, shell=True)
        logger.info("{} ({}): Started script\n{}".format(pipe.name, pipe.idx, cmd))
        worker.add_script(pipe_e, pipe_e.script)       
        out, err = p.communicate()
        worker.remove_script(pipe_e, pipe_e.script)       
        if p.returncode != 0:
            raise Exception(err.decode('utf-8'))
        logger.info('{} ({}): Executed script successful: {}'.format(pipe.name, 
            pipe.idx, pipe_e.script.path))
        dbm.close_session()

    except:
        pipe = pipe_e.pipe
        logger.info('{} ({}): Exception occurred in script: {}'.format(pipe.name, 
            pipe.idx, pipe_e.script.path))
        msg = traceback.format_exc()
        logger.error(msg)
        script_api.report_script_err(pipe_e, pipe, dbm, msg)
        dbm.close_session() 
Example #19
Source File: tasks.py    From jorvik with GNU General Public License v3.0 4 votes vote down vote up
def invia_mail_forzato(self, pk_tuple):
    """
    Questo task invia forzatamente la mail.
    Nessuna verifica si fa se il messaggio è stato precedentemente inviato
    oppure sia accodato. (come con l'invio normale nella funzione di sopra)
    """
    from celery import uuid
    from celery.result import AsyncResult
    from .models import Messaggio

    logger = get_task_logger(__name__)
    rescheduled_tasks_id = list()

    messages_to_resend = Messaggio.objects.filter(pk__in=pk_tuple)
    for msg in messages_to_resend:
        pk = msg.pk

        logger.info("[forced] Controllo messaggio id=%d" % pk)

        # Controllo se il messaggio ha un task_id,
        # se presente - dimentico il task per assegnare un nuovo task_id al messaggio
        if msg.task_id is not None:
            task = AsyncResult(msg.task_id)
            task.forget()
            logger.info("[forced] Dimentico task_id %s per il messaggio id=%d" % (msg.task_id, pk))

        # Creiamo un nuovo task ID e riaccodiamo il task.
        msg.task_id = uuid()
        msg.save()

        logger.warning("[forced] Nuovo task per l'invio accodato con id=%s." % msg.task_id)

        is_sent = msg.invia(forced=True)
        if not is_sent:
            logger.error("[forced] Errore temporaneo, nuovo tentativo richiesto.")
            raise self.retry()

        # Messaggio inviato con successo.
        logger.info("[forced] Messaggio %s inviato. Rimuovo task_id e salvo." % msg.pk)

        rescheduled_tasks_id.append(msg.task_id)
        msg.task_id = None

        msg.save()

    return len(rescheduled_tasks_id)