def test_multi_fail(self): tasklist = [] messages = {'COMPLETED': 0, 'FAILURE': 0, 'RECOVERED': 0} def send(subject, text, html_message): if subject.find('COMPLETED') >= 0: messages['COMPLETED'] += 1 elif subject.find('FAILURE') >= 0: messages['FAILURE'] += 1 elif subject.find('RECOVERED') >= 0: messages['RECOVERED'] += 1 else: raise Exception('Should not be here... %s' % subject) if messages['COMPLETED'] > 0 and messages['RECOVERED'] > 0: self.assertEqual(messages['FAILURE'], 1) self.assertEqual(messages['RECOVERED'], 1) tasklist[0]._stop(check_subprocesses=False) tl = TaskList( Task('test_multi_fail', ('tests/errsuccess.py', 'test_multi_fail.db', '5'), '* *', mail_failure=send, mail_success=send, max_lines=0, failure_email_threshold=1)) tasklist.append(tl) tl.start()
def test_send_success(self): tasklist = [] def send(subject, text, html_message): tasklist[0]._stop(check_subprocesses=False) self.assertEqual(len(text.splitlines()), 105) tl = TaskList( Task('test_send_success', ('seq', '100', '200'), '*', run_on_start=True, mail_success=send)) tasklist.append(tl) tl.start()
def test_max_lines2(self): tasklist = [] def send(subject, text, html_message): tasklist[0]._stop(check_subprocesses=False) self.assertEqual(len(text.splitlines()), 16) tl = TaskList( Task( 'test_max_lines2', ('tests/task_script.py', ), '*', run_on_start=True, mail_success=send, max_lines=((1, 10), None), )) tasklist.append(tl) tl.start()
def test_skip_mail_no_limitation(self): tasklist = [] messages = [0] def send(subject, text, html_message): if subject.find('SKIPPED') >= 0: messages[0] += 1 else: raise Exception('Should only send skipped messages') if messages[0] == 3: tasklist[0]._stop(check_subprocesses=False) tl = TaskList( Task('test_skip_mail_no_limitation', ('tests/longtask.py', ), '* *', mail_skipped=send, skip_delayed_email_threshold=None)) tasklist.append(tl) tl.start()
def test_skip_noblock_mail_limitation(self): tasklist = [] messages = {'SKIPPED': 0, 'NOT BLOCKED': 0} def send(subject, text, html_message): if subject.find('SKIPPED') >= 0: messages['SKIPPED'] += 1 elif subject.find('NOT BLOCKED') >= 0: messages['NOT BLOCKED'] += 1 if messages['NOT BLOCKED'] == 1: self.assertEqual(messages['SKIPPED'], 2) tasklist[0]._stop(check_subprocesses=False) tl = TaskList( Task('test_skip_noblock_mail_limitation', ('tests/longtask.py', ), '* *', mail_skipped=send, skip_delayed_email_threshold=2)) tasklist.append(tl) tl.start()
#!/usr/bin/env python3 import signal from periodtask import TaskList, Task def send(subject, message, html_message=None): print('|%s|' % message) tasks = TaskList( Task( 'example_task', ('tests/task_script.py', 'e'), ['0 0 0'], run_on_start=True, mail_success=send, stop_signal=signal.SIGINT, max_lines=((2, 5), 3), )) tasks.start()
stdout = logging.StreamHandler(sys.stdout) fmt = logging.Formatter('%(levelname)s|%(name)s|%(asctime)s|%(message)s') stdout.setFormatter(fmt) root = logging.getLogger() root.addHandler(stdout) root.setLevel(logging.INFO) tasklist = [] if os.environ.get('SEND_MAIL_TASK') == 'True': tasklist.append( Task( name='send_mail', command=('django-admin', 'send_mail'), periods='*/5 *', wait_timeout=5, stop_signal=signal.SIGINT, policy=SKIP, stdout_level=logging.DEBUG, stderr_level=logging.DEBUG, )) if os.environ.get('RETRY_DEFERRED_TASK') == 'True': tasklist.append( Task( name='retry_deferred', command=('django-admin', 'retry_deferred'), periods='10 *', wait_timeout=5, stop_signal=signal.SIGINT, policy=SKIP, stdout_level=logging.DEBUG,
#!/usr/bin/env python3 import logging import signal from django.conf import settings from periodtask import Task, TaskList, SKIP logging.basicConfig(format='%(message)s', level=logging.INFO) tasks = TaskList( Task(name='MailSender', command=( 'python', '/src/periodic_tasks/mailsender/run.py', ), periods='*/10 * * * * *', run_on_start=settings.DEBUG, wait_timeout=5, stop_signal=signal.SIGINT, policy=SKIP)) tasks.start()
def test_simple_task(self): task = Task('test_simple_task', ('ls', ), '* * * * * -1526', run_on_start=False) task.check_for_second(ts('2018-07-10 10:15:00')) self.assertEqual(len(task.process_threads), 0)
send_success = MailSender(os.environ.get('EMAIL_HOST'), int(os.environ.get('EMAIL_PORT')), os.environ.get('EMAIL_FROM'), os.environ.get('EMAIL_RECIPIENT'), timeout=10, use_ssl=True, use_tls=False, username=user, password=password).send_mail tasks = TaskList( Task('curator', ('./registry.sh', '-curator'), ['0 0 0 * * * UTC'], mail_success=None, mail_failure=send_success, mail_skipped=send_success, mail_delayed=None, wait_timeout=5, stop_signal=signal.SIGTERM, max_lines=10, run_on_start=True, policy=SKIP, template_dir='/tmp', stdout_logger=stdout_logger, stdout_level=logging.DEBUG, stderr_logger=stderr_logger, stderr_level=logging.WARNING, cwd=None), ) tasks.start()