Skip to content

Commit 0ca3475

Browse files
committed
Adding job moving feature
1 parent 97fe009 commit 0ca3475

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

mrq/basetasks/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ def perform_action(self, action, query, destination_queue):
9898
if list(query.keys()) == ["queue"]:
9999
Queue(query["queue"]).empty()
100100

101+
elif action == "move":
102+
cursor = self.collection.find(query, projection=["_id", "queue"])
103+
fetched_jobs = list(cursor)
104+
for jobs in group_iter(fetched_jobs, n=1000):
105+
jobs_by_queue = defaultdict(list)
106+
for job in jobs:
107+
jobs_by_queue[job["queue"]].append(job["_id"])
108+
stats["requeued"] += 1
109+
110+
for queue in jobs_by_queue:
111+
112+
updates = {
113+
"status": "queued",
114+
"datequeued": datetime.datetime.utcnow(),
115+
"dateupdated": datetime.datetime.utcnow(),
116+
"queue": destination_queue,
117+
"retry_count": 0
118+
}
119+
120+
self.collection.update({
121+
"_id": {"$in": jobs_by_queue[queue]}
122+
}, {"$set": updates}, multi=True)
123+
101124
elif action in ("requeue", "requeue_retry"):
102125

103126
# Requeue task by groups of maximum 1k items (if all in the same

mrq/dashboard/static/js/views/jobs.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ define(["jquery", "underscore", "views/generic/datatablepage", "models"],functio
223223
});
224224
self.refreshCallStack(job_id);
225225

226+
} else if (action == "move") {
227+
228+
var queue = prompt("Test");
229+
self.jobaction(evt, {
230+
"id": job_id,
231+
"action": action,
232+
"destination_queue": queue
233+
});
234+
226235
} else {
227236

228237
self.jobaction(evt, {
@@ -392,6 +401,7 @@ define(["jquery", "underscore", "views/generic/datatablepage", "models"],functio
392401
"<br/><br/>"+
393402
"<button class='btn btn-xs btn-danger pull-right' data-action='cancel'><span class='glyphicon glyphicon-remove-circle'></span> Cancel</button>"+
394403
"<button class='btn btn-xs btn-warning' data-action='requeue'><span class='glyphicon glyphicon-refresh'></span> Requeue</button>"+
404+
"<button class='btn btn-xs btn-warning' data-action='move'><span class='glyphicon glyphicon-refresh'></span> Move to...</button>"+
395405
"</div>";
396406
}
397407
return "";

mrq/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class AbortInterrupt(_MrqInterrupt):
2727
pass
2828

2929

30+
class MovedInterrupt(_MrqInterrupt):
31+
pass
32+
33+
3034
class RetryInterrupt(_MrqInterrupt):
3135
delay = None
3236
queue = None

mrq/job.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from bson import ObjectId
66
from redis.exceptions import LockError
77
import time
8-
from .exceptions import RetryInterrupt, MaxRetriesInterrupt, AbortInterrupt, MaxConcurrencyInterrupt
8+
from .exceptions import RetryInterrupt, MaxRetriesInterrupt, AbortInterrupt, MaxConcurrencyInterrupt, MovedInterrupt
99
from .utils import load_class_by_path, group_iter
1010
import gevent
1111
import objgraph
@@ -270,6 +270,13 @@ def requeue(self, queue=None, retry_count=0):
270270
"retry_count": retry_count
271271
})
272272

273+
def move(self, queue=None, retry_count=0):
274+
""" Cancel and requeues the current job in an other queue."""
275+
exc = MovedInterrupt()
276+
self._attach_original_exception(exc)
277+
self.requeue(queue, retry_count)
278+
raise exc
279+
273280
def perform(self):
274281
""" Loads and starts the main task for this job, the saves the result. """
275282

0 commit comments

Comments
 (0)