def test_kill_horse_command(self): """Ensure that shutdown command works properly.""" connection = self.testconn queue = Queue('foo', connection=connection) job = queue.enqueue(long_running_job, 4) worker = Worker('foo', connection=connection) def _send_kill_horse_command(): """Waits 0.25 seconds before sending kill-horse command""" time.sleep(0.25) send_kill_horse_command(connection, worker.name) p = Process(target=_send_kill_horse_command) p.start() worker.work(burst=True) p.join(1) job.refresh() self.assertTrue(job.id in queue.failed_job_registry) def start_work(): worker.work() p = Process(target=start_work) p.start() p.join(2) send_kill_horse_command(connection, worker.name) worker.refresh() # Since worker is not busy, command will be ignored self.assertEqual(worker.get_state(), WorkerStatus.IDLE) send_shutdown_command(connection, worker.name)
def handle(self, *args, **options): with Connection(REDIS_CLIENT): workers = Worker.all(REDIS_CLIENT) for worker in workers: send_kill_horse_command(REDIS_CLIENT, worker.name) send_shutdown_command(REDIS_CLIENT, worker.name) worker.register_death() job_ids = AsyncCronMail.objects.values_list('job_id').filter(started_at__isnull=False,status=False).first() if AsyncCronMail.objects.filter(started_at__isnull=False,status=False).count() > 0: try: job = Job.fetch(job_ids[0], connection=REDIS_CLIENT) DEFAULT_QUEUE.empty() DEFAULT_QUEUE.enqueue_job(job) except: print('Job does not exist') topper_registry = FailedJobRegistry(queue=TOPPER_QUEUE) for job_id in topper_registry.get_job_ids(): topper_registry.remove(job_id, delete_job=True) w = Worker([DEFAULT_QUEUE,TOPPER_QUEUE], connection=REDIS_CLIENT, name='default_worker') w.work()
def _send_shutdown_command(): time.sleep(0.25) send_shutdown_command(connection, worker.name)