Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@db_task()
def foo(number):
print('foo(%s)' % number)
@periodic_task(crontab(minute='*/2'))
def every_other_minute():
tprint('This task runs every 2 minutes.', 35)
@task()
def mul(a, b):
return a * b
@task(retries=3, retry_delay=10)
def try_thrice():
if random.randint(1, 3) == 1:
print('OK')
else:
print('About to fail, will retry in 10 seconds')
raise Exception('Crap something went wrong')
@task()
def add(a, b):
return a + b
def retry_once():
if not retry_once._did_retry:
retry_once._did_retry = True
raise RetryTask()
return "Done."
def app_with_scout(scout_config=None):
"""
Context manager that configures a Huey app with Scout installed.
"""
# Enable Scout by default in tests.
if scout_config is None:
scout_config = {}
scout_config.setdefault("monitor", True)
scout_config["core_agent_launch"] = False
huey = MemoryHuey(immediate=True)
@huey.task()
@huey.lock_task("hello")
def hello():
return "Hello World!"
@huey.task()
def retry_once():
if not retry_once._did_retry:
retry_once._did_retry = True
raise RetryTask()
return "Done."
retry_once._did_retry = False
@huey.task()
@db_task()
def send_email(recipient, subject, body, sender):
"""
Sends the given application e-mail.
:return: None
"""
if not mail.init():
logger.error("email failed: SMTP not configured")
msg = EmailMultiAlternatives(subject, body, sender, (recipient, ))
msg.attach_alternative(body, "text/html")
if msg.send() == 0:
logger.error("email failed: %s - '%s'", recipient, subject)
else:
logger.info("email sent: %s - '%s'", recipient, subject)
def respond(self, data):
if not isinstance(data, list):
try:
data = data.split()
except:
raise CommandError('Unrecognized request type.')
if not isinstance(data[0], basestring):
raise CommandError('First parameter must be command name.')
command = data[0].upper()
if command not in self._commands:
raise CommandError('Unrecognized command: %s' % command)
else:
logger.debug('Received %s', decode(command))
return self._commands[command](*data[1:])
def respond(self, data):
if not isinstance(data, list):
try:
data = data.split()
except:
raise CommandError('Unrecognized request type.')
if not isinstance(data[0], basestring):
raise CommandError('First parameter must be command name.')
command = data[0].upper()
if command not in self._commands:
raise CommandError('Unrecognized command: %s' % command)
else:
logger.debug('Received %s', decode(command))
return self._commands[command](*data[1:])