def test_interrupt_redis_started_jobs(worker): worker.start( queues="xxx", flags=" --config tests/fixtures/config-lostjobs.py") worker.send_task("tests.tasks.general.Add", { "a": 41, "b": 1, "sleep": 10}, block=False, queue="xxx") worker.send_task("tests.tasks.general.Add", { "a": 41, "b": 1, "sleep": 10}, block=False, queue="xxx") time.sleep(3) worker.stop(deps=False) assert Queue("xxx").size() == 0 assert connections.redis.zcard(Queue.redis_key_started()) == 2 worker.start(queues="default", start_deps=False, flush=False) assert connections.redis.zcard(Queue.redis_key_started()) == 2 res = worker.send_task("mrq.basetasks.cleaning.RequeueRedisStartedJobs", { "timeout": 0 }, block=True, queue="default") assert res["fetched"] == 2 assert res["requeued"] == 2 assert Queue("xxx").size() == 2 assert Queue("default").size() == 0 assert connections.redis.zcard(Queue.redis_key_started()) == 0
def run(self, params): self.collection = connections.mongodb_jobs.mrq_jobs redis_key_started = Queue.redis_key_started() stats = { "fetched": 0, "requeued": 0 } # Fetch all the jobs started more than a minute ago - they should not be in redis:started anymore job_ids = connections.redis.zrangebyscore(redis_key_started, "-inf", time.time() - params.get("timeout", 60)) for job_id in job_ids: queue = Job(job_id, start=False, fetch=False).fetch(full_data=True).data["queue"] stats["fetched"] += 1 log.info("Requeueing %s on %s" % (job_id, queue)) # TODO LUA script & don't rpush if not in zset anymore. with connections.redis.pipeline(transaction=True) as pipeline: pipeline.zrem(redis_key_started, job_id) pipeline.rpush(Queue(queue).redis_key, job_id) pipeline.execute() stats["requeued"] += 1 return stats
def run(self, params): redis_key_started = Queue.redis_key_started() stats = {"fetched": 0, "requeued": 0} # Fetch all the jobs started more than a minute ago - they should not # be in redis:started anymore job_ids = connections.redis.zrangebyscore(redis_key_started, "-inf", time.time() - params.get("timeout", 60)) # TODO this should be wrapped inside Queue or Worker # we shouldn't access these internals here queue_obj = Queue("default") unserialized_job_ids = queue_obj.unserialize_job_ids(job_ids) for i, job_id in enumerate(job_ids): queue = Job(unserialized_job_ids[i], start=False, fetch=False).fetch(full_data=True).data["queue"] queue_obj = Queue(queue) stats["fetched"] += 1 log.info("Requeueing %s on %s" % (unserialized_job_ids[i], queue)) # TODO LUA script & don't rpush if not in zset anymore. with connections.redis.pipeline(transaction=True) as pipeline: pipeline.zrem(redis_key_started, job_id) pipeline.rpush(queue_obj.redis_key, job_id) pipeline.execute() stats["requeued"] += 1 return stats
def run(self, params): redis_key_started = Queue.redis_key_started() stats = { "fetched": 0, "requeued": 0 } # Fetch all the jobs started more than a minute ago - they should not # be in redis:started anymore job_ids = connections.redis.zrangebyscore( redis_key_started, "-inf", time.time() - params.get("timeout", 60)) # TODO this should be wrapped inside Queue or Worker # we shouldn't access these internals here queue_obj = Queue("default") unserialized_job_ids = queue_obj.unserialize_job_ids(job_ids) for i, job_id in enumerate(job_ids): queue = Job(unserialized_job_ids[i], start=False, fetch=False).fetch( full_data=True).data["queue"] queue_obj = Queue(queue) stats["fetched"] += 1 log.info("Requeueing %s on %s" % (unserialized_job_ids[i], queue)) # TODO LUA script & don't rpush if not in zset anymore. with connections.redis.pipeline(transaction=True) as pipeline: pipeline.zrem(redis_key_started, job_id) pipeline.rpush(queue_obj.redis_key, job_id) pipeline.execute() stats["requeued"] += 1 return stats