def test_entry(self): m = create_model_interval(schedule(timedelta(seconds=10))) e = self.Entry(m) self.assertListEqual(e.args, [2, 2]) self.assertDictEqual(e.kwargs, {"callback": "foo"}) self.assertTrue(e.schedule) self.assertEqual(e.total_run_count, 0) self.assertIsInstance(e.last_run_at, datetime) self.assertDictContainsSubset( { "queue": "xaz", "exchange": "foo", "routing_key": "cpu" }, e.options) right_now = now() m2 = create_model_interval(schedule(timedelta(seconds=10)), last_run_at=right_now) self.assertTrue(m2.last_run_at) e2 = self.Entry(m2) self.assertIs(e2.last_run_at, right_now) e3 = e2.next() self.assertGreater(e3.last_run_at, e2.last_run_at) self.assertEqual(e3.total_run_count, 1)
def test_tasksetmeta(self): m1 = self.createTaskSetMeta() m2 = self.createTaskSetMeta() m3 = self.createTaskSetMeta() self.assertTrue(unicode(m1).startswith('<TaskSet:')) self.assertTrue(m1.taskset_id) self.assertIsInstance(m1.date_done, datetime) self.assertEqual( TaskSetMeta.objects.restore_taskset(m1.taskset_id).taskset_id, m1.taskset_id) # Have to avoid save() because it applies the auto_now=True. TaskSetMeta.objects.filter(taskset_id=m1.taskset_id).update( date_done=now() - timedelta(days=10)) expired = TaskSetMeta.objects.get_all_expired( celery.conf.CELERY_TASK_RESULT_EXPIRES) self.assertIn(m1, expired) self.assertNotIn(m2, expired) self.assertNotIn(m3, expired) TaskSetMeta.objects.delete_expired( celery.conf.CELERY_TASK_RESULT_EXPIRES) self.assertNotIn(m1, TaskSetMeta.objects.all()) m4 = self.createTaskSetMeta() self.assertEqual( TaskSetMeta.objects.restore_taskset(m4.taskset_id).taskset_id, m4.taskset_id) TaskSetMeta.objects.delete_taskset(m4.taskset_id) self.assertIsNone(TaskSetMeta.objects.restore_taskset(m4.taskset_id))
def test_taskmeta(self): m1 = self.createTaskMeta() m2 = self.createTaskMeta() m3 = self.createTaskMeta() self.assertTrue(unicode(m1).startswith("<Task:")) self.assertTrue(m1.task_id) self.assertIsInstance(m1.date_done, datetime) self.assertEqual(TaskMeta.objects.get_task(m1.task_id).task_id, m1.task_id) self.assertNotEqual(TaskMeta.objects.get_task(m1.task_id).status, states.SUCCESS) TaskMeta.objects.store_result(m1.task_id, True, status=states.SUCCESS) TaskMeta.objects.store_result(m2.task_id, True, status=states.SUCCESS) self.assertEqual(TaskMeta.objects.get_task(m1.task_id).status, states.SUCCESS) self.assertEqual(TaskMeta.objects.get_task(m2.task_id).status, states.SUCCESS) # Have to avoid save() because it applies the auto_now=True. TaskMeta.objects.filter(task_id=m1.task_id).update( date_done=now() - timedelta(days=10)) expired = TaskMeta.objects.get_all_expired( current_app.conf.CELERY_TASK_RESULT_EXPIRES) self.assertIn(m1, expired) self.assertNotIn(m2, expired) self.assertNotIn(m3, expired) TaskMeta.objects.delete_expired( current_app.conf.CELERY_TASK_RESULT_EXPIRES) self.assertNotIn(m1, TaskMeta.objects.all())
def test_taskmeta(self): m1 = self.createTaskMeta() m2 = self.createTaskMeta() m3 = self.createTaskMeta() self.assertTrue(unicode(m1).startswith("<Task:")) self.assertTrue(m1.task_id) self.assertIsInstance(m1.date_done, datetime) self.assertEqual(TaskMeta.objects.get_task(m1.task_id).task_id, m1.task_id) self.assertNotEqual(TaskMeta.objects.get_task(m1.task_id).status, states.SUCCESS) TaskMeta.objects.store_result(m1.task_id, True, status=states.SUCCESS) TaskMeta.objects.store_result(m2.task_id, True, status=states.SUCCESS) self.assertEqual(TaskMeta.objects.get_task(m1.task_id).status, states.SUCCESS) self.assertEqual(TaskMeta.objects.get_task(m2.task_id).status, states.SUCCESS) # Have to avoid save() because it applies the auto_now=True. TaskMeta.objects.filter(task_id=m1.task_id).update( date_done=now() - timedelta(days=10)) expired = TaskMeta.objects.get_all_expired( celery.conf.CELERY_TASK_RESULT_EXPIRES) self.assertIn(m1, expired) self.assertNotIn(m2, expired) self.assertNotIn(m3, expired) TaskMeta.objects.delete_expired( celery.conf.CELERY_TASK_RESULT_EXPIRES) self.assertNotIn(m1, TaskMeta.objects.all())
def on_failure_patched(self, exc, task_id, args, kwargs, einfo): TaskState.objects.create(task_id=uuid4().hex, state='FAILURE', name=self.name, result=einfo, args=args, kwargs=kwargs, tstamp=now())
def on_success_patched(self, retval, task_id, args, kwargs): TaskState.objects.create(task_id=uuid4().hex, state='SUCCESS', name=self.name, result=retval, args=args, kwargs=kwargs, tstamp=now())
def test_handle_task_received(self): worker = Worker(hostname="fuzzie") worker.on_online(timestamp=time()) self.cam.handle_worker((worker.hostname, worker)) task = create_task(worker) task.on_received(timestamp=time()) self.assertEqual(task.state, "RECEIVED") mt = self.cam.handle_task((task.uuid, task)) self.assertEqual(mt.name, task.name) self.assertTrue(unicode(mt)) self.assertTrue(repr(mt)) mt.eta = now() self.assertIn("eta", unicode(mt)) self.assertIn(mt, models.TaskState.objects.active())
def test_cleanup(self): b = DatabaseBackend() b.TaskModel._default_manager.all().delete() ids = [gen_unique_id() for _ in xrange(3)] for i, res in enumerate((16, 32, 64)): b.mark_as_done(ids[i], res) self.assertEqual(b.TaskModel._default_manager.count(), 3) then = now() - current_app.conf.CELERY_TASK_RESULT_EXPIRES * 2 # Have to avoid save() because it applies the auto_now=True. b.TaskModel._default_manager.filter(task_id__in=ids[:-1]) \ .update(date_done=then) b.cleanup() self.assertEqual(b.TaskModel._default_manager.count(), 1)
def test_entry(self): m = create_model_interval(schedule(timedelta(seconds=10))) e = self.Entry(m) self.assertListEqual(e.args, [2, 2]) self.assertDictEqual(e.kwargs, {"callback": "foo"}) self.assertTrue(e.schedule) self.assertEqual(e.total_run_count, 0) self.assertIsInstance(e.last_run_at, datetime) self.assertDictContainsSubset({"queue": "xaz", "exchange": "foo", "routing_key": "cpu"}, e.options) right_now = now() m2 = create_model_interval(schedule(timedelta(seconds=10)), last_run_at=right_now) self.assertTrue(m2.last_run_at) e2 = self.Entry(m2) self.assertIs(e2.last_run_at, right_now) e3 = e2.next() self.assertGreater(e3.last_run_at, e2.last_run_at) self.assertEqual(e3.total_run_count, 1)
def do_export(job): job.begin = now() job.save() job.do_export() job.end = now() job.save()