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, 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()
python类get_task_logger()的实例源码
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()
def __init__(self):
super().__init__()
self.token: str = None
self.project_name = os.getenv('COMPOSE_PROJECT_NAME', 'zimfarm')
self.logger = get_task_logger(__name__)
self.start_time: datetime = None
self.ended_time: datetime = None
self.zim_file_name = None
self.status = 'PENDING'
self.current_index = 0
self.steps = []
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')
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)
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)))
def log(self):
logger = get_task_logger(self.name)
return logger
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, 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()
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
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)
def errLog(x):
"""
Logs x to celery INFO. Used as a callback in sh piping to manually print
otherwise swallowed error logs.
"""
logger = get_task_logger(__name__)
logger.info(x)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.log = get_task_logger(self.__class__.__name__)
self.configuration = configuration
def create_celery_routes(celery, cfg):
logger = get_task_logger(__name__)
# XXX This is kinda a thing. If we have a item that is not retried then
# we will not be able to deliver a message. So eventually we should probably
# have a cron task that takes messages that have failed their retries and
# continually resend them
@celery.task(max_retries=cfg.config.celery.transmit_gcm_id.retries)
def transmit_gcm_id(gcm_iid, msg_id, client_id, action):
# Only send predefined actions
if action not in constants.PHONE_ACTIONS:
raise Exception("Choose an action that is one of {}".format(PHONE_ACTIONS))
# Get GCM API key
gcm = GCM(cfg.config.gcm_api_key)
# Log transmission
data = {"message_id": msg_id, "client_id": client_id, "action": action}
logger.info("Transmit id: {} to phone with iid: {}".format(id, gcm_iid))
# Get Response
response = gcm.json_request(registration_ids=[gcm_iid], data=data, priority="high")
# Check errors and retry if necessary
if 'errors' in response:
logger.warn("Error found in response: {}".format(response))
transmit_gcm_id.retry(
args=[gcm_iid, msg_id, client_id, action], countdown=cfg.config.celery.transmit_gcm_id.timeout
)
# Log success
else:
logger.debug("Message transmitted successfully response: {}".format(response))
@celery.task(max_retries=cfg.config.celery.remove_key_data.retries)
def remove_key_data(gcm_iid):
# Send request to phone to delete revoked private key
gcm = GCM(cfg.config.gcm_api_key)
data = {"action": "revoke"}
# Check if there were errors and retry if needed
response = gcm.json_request(registration_ids=[gcm_iid], data=data)
if 'errors' in response:
remove_key_data.retry(
args=[gcm_iid], countdown=cfg.config.celery.remove_key_data.timeout
)
CeleryTasks = namedtuple('CeleryTasks', ['transmit_gcm_id', 'remove_key_data'])
return CeleryTasks(transmit_gcm_id, remove_key_data)
def task_instant_link(quovo_user_id, account_id):
instant_link_logger = get_task_logger('instant_link')
quovo_user = QuovoUser.objects.get(quovo_id=quovo_user_id)
# update account
instant_link_logger.info('updating user account: {}'.format(quovo_user_id))
quovo_user.update_accounts()
# update portfolio
instant_link_logger.info('updating user portfolio: {}'.format(quovo_user_id))
quovo_user.update_portfolios()
# update holdings
instant_link_logger.info('updating user holding: {}'.format(quovo_user_id))
new_holdings = quovo_user.get_new_holdings()
if not quovo_user.current_holdings_equal_holding_json(new_holdings):
instant_link_logger.info('new holdings found for user: {}'.format(quovo_user_id))
quovo_user.set_current_holdings(new_holdings)
if not quovo_user.has_completed_user_holdings():
instant_link_logger.info('user has some holdings that are not completed: {}'.format(quovo_user_id))
quovo_user.is_completed = False
quovo_user.save()
# get holding information
for current_holdings in quovo_user.get_current_holdings():
instant_link_logger.info('updating holding {} for user: {}'.format(current_holdings, quovo_user_id))
nightly_process.update_holding(current_holdings.holding)
# update user display holdings
instant_link_logger.info('updating display holding user: {}'.format(quovo_user_id))
quovo_user.update_display_holdings()
if quovo_user.has_completed_user_holdings():
quovo_user.is_completed = True
quovo_user.save()
if quovo_user.user_accounts.exists():
account = quovo_user.user_accounts.filter(quovo_id=account_id)
if account.exists() and account.first():
holdings = account.first().account_current_holdings.all()
for dh in holdings:
holding = dh.holding
if holding.is_completed():
mailchimp.send_holding_processing_complete_notification(quovo_user.user_profile.user.email)
break
# update user stats info
instant_link_logger.info('updating user stats: {}'.format(quovo_user_id))
for acct in quovo_user.user_accounts.all():
acct.get_account_returns()
quovo_user.get_user_sharpe()
quovo_user.get_user_bond_equity()
# update user transactions
instant_link_logger.info('updating transactions user: {}'.format(quovo_user_id))
quovo_user.update_transactions()
instant_link_logger.info('updating fees user: {}'.format(quovo_user_id))
quovo_user.update_fees()
instant_link_logger.info('instant link completed for user: {}'.format(quovo_user_id))