本文整理汇总了Python中limpyd_jobs.models.Job类的典型用法代码示例。如果您正苦于以下问题:Python Job类的具体用法?Python Job怎么用?Python Job使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Job类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_a_job_in_error_could_be_requeued
def test_a_job_in_error_could_be_requeued(self):
job = Job(identifier='job:1', status=STATUSES.ERROR)
job.requeue('test')
queue = Queue.get_queue('test')
self.assertEqual(queue.waiting.llen(), 1)
self.assertEqual(queue.delayed.zcard(), 0)
self.assertEqual(job.status.hget(), STATUSES.WAITING)
示例2: test_job_error_method_should_be_called
def test_job_error_method_should_be_called(self):
class ExceptionWithCode(Exception):
def __init__(self, message, code):
super(ExceptionWithCode, self).__init__(message)
self.message = message
self.code = code
def callback(job, queue):
raise ExceptionWithCode('foobar', 42)
job1 = Job.add_job(identifier='job:1', queue_name='test')
queue = Queue.get_queue(name='test')
worker = Worker(name='test', max_loops=1) # no callback
worker.run()
self.assertEqual(job1.status.hget(), STATUSES.ERROR)
self.assertIn(job1.get_pk(), queue.errors.lmembers())
self.assertEqual(len(Error.collection()), 1)
error = Error.get(identifier='job:1')
self.assertEqual(error.message.hget(), 'You must implement your own action')
self.assertEqual(error.code.hget(), None)
job2 = Job.add_job(identifier='job:2', queue_name='test')
queue = Queue.get_queue(name='test')
worker = Worker(name='test', max_loops=1, callback=callback) # callback with exception
worker.run()
self.assertEqual(job2.status.hget(), STATUSES.ERROR)
self.assertIn(job2.get_pk(), queue.errors.lmembers())
self.assertEqual(len(Error.collection()), 2)
error = Error.get(identifier='job:2')
self.assertEqual(error.message.hget(), 'foobar')
self.assertEqual(error.code.hget(), '42')
示例3: test_requeue_delayed_jobs_put_back_ready_delayed_jobs_to_the_waiting_list
def test_requeue_delayed_jobs_put_back_ready_delayed_jobs_to_the_waiting_list(self):
queue = Queue.get_queue(name='test')
job1 = Job.add_job(identifier='job:1', queue_name='test', delayed_for=10)
job2 = Job.add_job(identifier='job:2', queue_name='test', delayed_for=1)
self.assertEqual(Queue.count_waiting_jobs('test'), 0)
self.assertEqual(Queue.count_delayed_jobs('test'), 2)
self.assertEqual(job1.status.hget(), STATUSES.DELAYED)
self.assertEqual(job2.status.hget(), STATUSES.DELAYED)
# must not move any jobs, too soon
queue.requeue_delayed_jobs()
self.assertEqual(Queue.count_waiting_jobs('test'), 0)
self.assertEqual(Queue.count_delayed_jobs('test'), 2)
self.assertEqual(job1.status.hget(), STATUSES.DELAYED)
self.assertEqual(job2.status.hget(), STATUSES.DELAYED)
sleep(1)
# now we should have one job in the waiting list
queue.requeue_delayed_jobs()
self.assertEqual(Queue.count_waiting_jobs('test'), 1)
self.assertEqual(Queue.count_delayed_jobs('test'), 1)
self.assertEqual(job1.status.hget(), STATUSES.DELAYED)
self.assertEqual(job2.status.hget(), STATUSES.WAITING)
waiting_jobs = queue.waiting.lmembers()
self.assertEqual(waiting_jobs, [job2.ident])
delayed_jobs = queue.delayed.zrange(0, -1, withscores=True)
self.assertEqual(delayed_jobs[0][0], job1.ident)
示例4: test_get_from_ident_should_return_a_job
def test_get_from_ident_should_return_a_job(self):
job = Job.add_job(identifier='job:1', queue_name='test')
test_job = Job.get_from_ident('limpyd_jobs.models.Job:%s' % job.pk.get())
self.assertEqual(job.ident, test_job.ident)
job = JobFooBar.add_job(identifier='foobar:1', queue_name='foobar')
test_job = Job.get_from_ident('tests.models.JobFooBar:%s' % job.pk.get())
self.assertEqual(job.ident, test_job.ident)
示例5: test_get_model_repr_should_return_the_string_repr_of_the_model
def test_get_model_repr_should_return_the_string_repr_of_the_model(self):
self.assertEqual(Job.get_model_repr(), 'limpyd_jobs.models.Job')
job = Job()
self.assertEqual(job.get_model_repr(), 'limpyd_jobs.models.Job')
self.assertEqual(JobFooBar.get_model_repr(), 'tests.models.JobFooBar')
job = JobFooBar()
self.assertEqual(job.get_model_repr(), 'tests.models.JobFooBar')
示例6: test_prepending_an_existing_job_should_move_it_at_the_beginning
def test_prepending_an_existing_job_should_move_it_at_the_beginning(self):
queue = Queue.get_queue(name='test', priority=1)
job1 = Job.add_job(identifier='job:1', queue_name='test', priority=1)
job2 = Job.add_job(identifier='job:2', queue_name='test', priority=1)
self.assertEqual(queue.waiting.lmembers(), [job1.ident, job2.ident])
Job.add_job(identifier='job:2', queue_name='test', priority=1, prepend=True)
self.assertEqual(queue.waiting.lmembers(), [job2.ident, job1.ident])
示例7: test_requeuing_a_job_with_a_datetime_should_put_it_in_the_delayed_lits
def test_requeuing_a_job_with_a_datetime_should_put_it_in_the_delayed_lits(self):
job = Job(identifier='job:1', status=STATUSES.ERROR)
queue = Queue.get_queue('test')
self.assertEqual(queue.waiting.llen(), 0)
self.assertEqual(queue.delayed.zcard(), 0)
job.requeue('test', delayed_until=datetime.utcnow()+timedelta(seconds=5))
self.assertEqual(queue.waiting.llen(), 0)
self.assertEqual(queue.delayed.zcard(), 1)
self.assertEqual(job.status.hget(), STATUSES.DELAYED)
示例8: test_enqueue_or_delay_should_delay_if_in_the_future
def test_enqueue_or_delay_should_delay_if_in_the_future(self):
queue = Queue.get_queue(name='test', priority=1)
job = Job(identifier='job:1')
delayed_until = datetime.utcnow() + timedelta(seconds=5)
job.enqueue_or_delay('test', 1, delayed_until=delayed_until)
self.assertEqual(queue.delayed.zcard(), 1)
self.assertEqual(queue.waiting.llen(), 0)
self.assertEqual(job.delayed_until.hget(), str(delayed_until))
self.assertEqual(job.status.hget(), STATUSES.DELAYED)
self.assertEqual(job.priority.hget(), '1')
示例9: test_first_delayed_should_return_the_first_delayed_job_to_be_ready
def test_first_delayed_should_return_the_first_delayed_job_to_be_ready(self):
Job.add_job(identifier='job:1', queue_name='test', delayed_for=5)
job2 = Job.add_job(identifier='job:2', queue_name='test', delayed_for=1)
queue = Queue.get_queue(name='test')
attended_timestamp = datetime_to_score(parse(job2.delayed_until.hget()))
job_ident, timestamp = queue.first_delayed
self.assertEqual(job_ident, job2.ident)
self.assertEqual(timestamp, attended_timestamp)
timestamp = queue.first_delayed_time
self.assertEqual(timestamp, attended_timestamp)
示例10: test_run_ended_method_should_be_called
def test_run_ended_method_should_be_called(self):
class TestWorker(Worker):
passed = False
def run_ended(self):
super(TestWorker, self).run_ended()
self.passed = True
Job.add_job('job:1', 'test')
worker = TestWorker('test', max_loops=1) # one loop to run only one job
worker.run()
self.assertEqual(worker.passed, True)
示例11: test_job_started_method_should_be_called
def test_job_started_method_should_be_called(self):
class TestWorker(Worker):
passed = False
def execute():
pass
def job_started(self, job, queue):
super(TestWorker, self).job_started(job, queue)
self.passed = True
Job.add_job(identifier='job:1', queue_name='test')
worker = TestWorker(name='test', max_loops=1)
worker.run()
self.assertTrue(worker.passed)
示例12: test_add_error_method_should_add_an_error_instance
def test_add_error_method_should_add_an_error_instance(self):
e = ErrorTests.ExceptionWithCode('the answer', 42)
job = Job.add_job(identifier='job:1', queue_name='test')
error1 = Error.add_error(queue_name='test', job=job, error=e)
self.assertEqual(list(Error.collection()), [error1.pk.get()])
Error.add_error(queue_name='test', job=job, error=e)
self.assertEqual(len(Error.collection()), 2)
示例13: test_adding_a_job_with_delayed_until_not_in_the_future_should_add_the_job_in_the_waiting_list
def test_adding_a_job_with_delayed_until_not_in_the_future_should_add_the_job_in_the_waiting_list(self):
queue = Queue.get_queue(name='test')
job = Job.add_job(identifier='job:1', queue_name='test', delayed_until=datetime.utcnow() - timedelta(seconds=5))
self.assertEqual(job.status.hget(), STATUSES.WAITING)
self.assertEqual(queue.waiting.llen(), 1)
self.assertEqual(queue.delayed.zcard(), 0)
示例14: test_wait_for_job_should_respond_with_queue_and_job_when_a_queue_is_not_empty
def test_wait_for_job_should_respond_with_queue_and_job_when_a_queue_is_not_empty(self):
queue = Queue.get_queue('test')
worker = Worker('test')
worker.test_content = None
class Thread(threading.Thread):
def run(self):
worker.update_keys()
queue, job = worker.wait_for_job()
worker.test_content = queue, job
# start listening
thread = Thread()
thread.start()
time.sleep(0.1)
# no job, nothing received
self.assertIsNone(worker.test_content)
# add a job
job = Job.add_job(queue_name='test', identifier='job:1')
time.sleep(0.1)
# info should be received
self.assertEqual(worker.test_content[0].get_pk(), queue.get_pk())
self.assertTrue(isinstance(worker.test_content[0], Queue))
self.assertEqual(worker.test_content[1].get_pk(), job.get_pk())
self.assertTrue(isinstance(worker.test_content[1], Job))
# job must no be in the queue anymore
self.assertNotIn(job.get_pk(), queue.waiting.lmembers())
示例15: test_send_stop_signal_should_stop_worker
def test_send_stop_signal_should_stop_worker(self):
class TestWorker(Worker):
def execute(self, job, queue):
"""
Simulate a signal by directly calling the signal handler
"""
self.catch_end_signal(signal.SIGINT, None)
Job.add_job('job:1', 'test')
Job.add_job('job:2', 'test')
queue = Queue.get_queue('test')
worker = TestWorker('test', max_loops=2)
worker.run()
self.assertEqual(worker.end_signal_caught, True)
self.assertEqual(queue.success.llen(), 1)
self.assertEqual(queue.waiting.llen(), 1)