You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

511 line
19 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import (absolute_import, division, print_function,
  3. unicode_literals)
  4. import uuid
  5. import warnings
  6. from redis import WatchError
  7. from .compat import as_text, string_types, total_ordering
  8. from .connections import resolve_connection
  9. from .defaults import DEFAULT_RESULT_TTL
  10. from .exceptions import (DequeueTimeout, InvalidJobDependency, NoSuchJobError,
  11. UnpickleError)
  12. from .job import Job, JobStatus
  13. from .utils import backend_class, import_attribute, utcnow, parse_timeout
  14. def compact(lst):
  15. return [item for item in lst if item is not None]
  16. @total_ordering
  17. class Queue(object):
  18. job_class = Job
  19. DEFAULT_TIMEOUT = 180 # Default timeout seconds.
  20. redis_queue_namespace_prefix = 'rq:queue:'
  21. redis_queues_keys = 'rq:queues'
  22. @classmethod
  23. def all(cls, connection=None, job_class=None):
  24. """Returns an iterable of all Queues.
  25. """
  26. connection = resolve_connection(connection)
  27. def to_queue(queue_key):
  28. return cls.from_queue_key(as_text(queue_key),
  29. connection=connection,
  30. job_class=job_class)
  31. return [to_queue(rq_key)
  32. for rq_key in connection.smembers(cls.redis_queues_keys)
  33. if rq_key]
  34. @classmethod
  35. def from_queue_key(cls, queue_key, connection=None, job_class=None):
  36. """Returns a Queue instance, based on the naming conventions for naming
  37. the internal Redis keys. Can be used to reverse-lookup Queues by their
  38. Redis keys.
  39. """
  40. prefix = cls.redis_queue_namespace_prefix
  41. if not queue_key.startswith(prefix):
  42. raise ValueError('Not a valid RQ queue key: {0}'.format(queue_key))
  43. name = queue_key[len(prefix):]
  44. return cls(name, connection=connection, job_class=job_class)
  45. def __init__(self, name='default', default_timeout=None, connection=None,
  46. is_async=True, job_class=None, **kwargs):
  47. self.connection = resolve_connection(connection)
  48. prefix = self.redis_queue_namespace_prefix
  49. self.name = name
  50. self._key = '{0}{1}'.format(prefix, name)
  51. self._default_timeout = parse_timeout(default_timeout) or self.DEFAULT_TIMEOUT
  52. self._is_async = is_async
  53. if 'async' in kwargs:
  54. self._is_async = kwargs['async']
  55. warnings.warn('The `async` keyword is deprecated. Use `is_async` instead', DeprecationWarning)
  56. # override class attribute job_class if one was passed
  57. if job_class is not None:
  58. if isinstance(job_class, string_types):
  59. job_class = import_attribute(job_class)
  60. self.job_class = job_class
  61. def __len__(self):
  62. return self.count
  63. def __nonzero__(self):
  64. return True
  65. def __bool__(self):
  66. return True
  67. def __iter__(self):
  68. yield self
  69. @property
  70. def key(self):
  71. """Returns the Redis key for this Queue."""
  72. return self._key
  73. @property
  74. def registry_cleaning_key(self):
  75. """Redis key used to indicate this queue has been cleaned."""
  76. return 'rq:clean_registries:%s' % self.name
  77. def acquire_cleaning_lock(self):
  78. """Returns a boolean indicating whether a lock to clean this queue
  79. is acquired. A lock expires in 899 seconds (15 minutes - 1 second)
  80. """
  81. return self.connection.set(self.registry_cleaning_key, 1, nx=1, ex=899)
  82. def empty(self):
  83. """Removes all messages on the queue."""
  84. script = """
  85. local prefix = "{0}"
  86. local q = KEYS[1]
  87. local count = 0
  88. while true do
  89. local job_id = redis.call("lpop", q)
  90. if job_id == false then
  91. break
  92. end
  93. -- Delete the relevant keys
  94. redis.call("del", prefix..job_id)
  95. redis.call("del", prefix..job_id..":dependents")
  96. count = count + 1
  97. end
  98. return count
  99. """.format(self.job_class.redis_job_namespace_prefix).encode("utf-8")
  100. script = self.connection.register_script(script)
  101. return script(keys=[self.key])
  102. def delete(self, delete_jobs=True):
  103. """Deletes the queue. If delete_jobs is true it removes all the associated messages on the queue first."""
  104. if delete_jobs:
  105. self.empty()
  106. with self.connection.pipeline() as pipeline:
  107. pipeline.srem(self.redis_queues_keys, self._key)
  108. pipeline.delete(self._key)
  109. pipeline.execute()
  110. def is_empty(self):
  111. """Returns whether the current queue is empty."""
  112. return self.count == 0
  113. @property
  114. def is_async(self):
  115. """Returns whether the current queue is async."""
  116. return bool(self._is_async)
  117. def fetch_job(self, job_id):
  118. try:
  119. job = self.job_class.fetch(job_id, connection=self.connection)
  120. except NoSuchJobError:
  121. self.remove(job_id)
  122. else:
  123. if job.origin == self.name:
  124. return job
  125. def get_job_ids(self, offset=0, length=-1):
  126. """Returns a slice of job IDs in the queue."""
  127. start = offset
  128. if length >= 0:
  129. end = offset + (length - 1)
  130. else:
  131. end = length
  132. return [as_text(job_id) for job_id in
  133. self.connection.lrange(self.key, start, end)]
  134. def get_jobs(self, offset=0, length=-1):
  135. """Returns a slice of jobs in the queue."""
  136. job_ids = self.get_job_ids(offset, length)
  137. return compact([self.fetch_job(job_id) for job_id in job_ids])
  138. @property
  139. def job_ids(self):
  140. """Returns a list of all job IDS in the queue."""
  141. return self.get_job_ids()
  142. @property
  143. def jobs(self):
  144. """Returns a list of all (valid) jobs in the queue."""
  145. return self.get_jobs()
  146. @property
  147. def count(self):
  148. """Returns a count of all messages in the queue."""
  149. return self.connection.llen(self.key)
  150. @property
  151. def failed_job_registry(self):
  152. """Returns this queue's FailedJobRegistry."""
  153. from rq.registry import FailedJobRegistry
  154. return FailedJobRegistry(queue=self)
  155. def remove(self, job_or_id, pipeline=None):
  156. """Removes Job from queue, accepts either a Job instance or ID."""
  157. job_id = job_or_id.id if isinstance(job_or_id, self.job_class) else job_or_id
  158. if pipeline is not None:
  159. pipeline.lrem(self.key, 1, job_id)
  160. return
  161. return self.connection.lrem(self.key, 1, job_id)
  162. def compact(self):
  163. """Removes all "dead" jobs from the queue by cycling through it, while
  164. guaranteeing FIFO semantics.
  165. """
  166. COMPACT_QUEUE = '{0}_compact:{1}'.format(
  167. self.redis_queue_namespace_prefix, uuid.uuid4()) # noqa
  168. self.connection.rename(self.key, COMPACT_QUEUE)
  169. while True:
  170. job_id = as_text(self.connection.lpop(COMPACT_QUEUE))
  171. if job_id is None:
  172. break
  173. if self.job_class.exists(job_id, self.connection):
  174. self.connection.rpush(self.key, job_id)
  175. def push_job_id(self, job_id, pipeline=None, at_front=False):
  176. """Pushes a job ID on the corresponding Redis queue.
  177. 'at_front' allows you to push the job onto the front instead of the back of the queue"""
  178. connection = pipeline if pipeline is not None else self.connection
  179. if at_front:
  180. connection.lpush(self.key, job_id)
  181. else:
  182. connection.rpush(self.key, job_id)
  183. def enqueue_call(self, func, args=None, kwargs=None, timeout=None,
  184. result_ttl=None, ttl=None, failure_ttl=None,
  185. description=None, depends_on=None, job_id=None,
  186. at_front=False, meta=None):
  187. """Creates a job to represent the delayed function call and enqueues
  188. it.
  189. It is much like `.enqueue()`, except that it takes the function's args
  190. and kwargs as explicit arguments. Any kwargs passed to this function
  191. contain options for RQ itself.
  192. """
  193. timeout = parse_timeout(timeout)
  194. if timeout is None:
  195. timeout = self._default_timeout
  196. elif timeout == 0:
  197. raise ValueError('0 timeout is not allowed. Use -1 for infinite timeout')
  198. result_ttl = parse_timeout(result_ttl)
  199. failure_ttl = parse_timeout(failure_ttl)
  200. ttl = parse_timeout(ttl)
  201. if ttl is not None and ttl <= 0:
  202. raise ValueError('Job ttl must be greater than 0')
  203. job = self.job_class.create(
  204. func, args=args, kwargs=kwargs, connection=self.connection,
  205. result_ttl=result_ttl, ttl=ttl, failure_ttl=failure_ttl,
  206. status=JobStatus.QUEUED, description=description,
  207. depends_on=depends_on, timeout=timeout, id=job_id,
  208. origin=self.name, meta=meta)
  209. # If job depends on an unfinished job, register itself on it's
  210. # parent's dependents instead of enqueueing it.
  211. # If WatchError is raised in the process, that means something else is
  212. # modifying the dependency. In this case we simply retry
  213. if depends_on is not None:
  214. if not isinstance(depends_on, self.job_class):
  215. depends_on = self.job_class(id=depends_on,
  216. connection=self.connection)
  217. with self.connection.pipeline() as pipe:
  218. while True:
  219. try:
  220. pipe.watch(depends_on.key)
  221. # If the dependency does not exist, raise an
  222. # exception to avoid creating an orphaned job.
  223. if not self.job_class.exists(depends_on.id,
  224. self.connection):
  225. raise InvalidJobDependency('Job {0} does not exist'.format(depends_on.id))
  226. if depends_on.get_status() != JobStatus.FINISHED:
  227. pipe.multi()
  228. job.set_status(JobStatus.DEFERRED)
  229. job.register_dependency(pipeline=pipe)
  230. job.save(pipeline=pipe)
  231. job.cleanup(ttl=job.ttl, pipeline=pipe)
  232. pipe.execute()
  233. return job
  234. break
  235. except WatchError:
  236. continue
  237. job = self.enqueue_job(job, at_front=at_front)
  238. return job
  239. def run_job(self, job):
  240. job.perform()
  241. job.set_status(JobStatus.FINISHED)
  242. job.save(include_meta=False)
  243. job.cleanup(DEFAULT_RESULT_TTL)
  244. return job
  245. def enqueue(self, f, *args, **kwargs):
  246. """Creates a job to represent the delayed function call and enqueues
  247. it.
  248. Expects the function to call, along with the arguments and keyword
  249. arguments.
  250. The function argument `f` may be any of the following:
  251. * A reference to a function
  252. * A reference to an object's instance method
  253. * A string, representing the location of a function (must be
  254. meaningful to the import context of the workers)
  255. """
  256. if not isinstance(f, string_types) and f.__module__ == '__main__':
  257. raise ValueError('Functions from the __main__ module cannot be processed '
  258. 'by workers')
  259. # Detect explicit invocations, i.e. of the form:
  260. # q.enqueue(foo, args=(1, 2), kwargs={'a': 1}, job_timeout=30)
  261. timeout = kwargs.pop('job_timeout', None)
  262. description = kwargs.pop('description', None)
  263. result_ttl = kwargs.pop('result_ttl', None)
  264. ttl = kwargs.pop('ttl', None)
  265. failure_ttl = kwargs.pop('failure_ttl', None)
  266. depends_on = kwargs.pop('depends_on', None)
  267. job_id = kwargs.pop('job_id', None)
  268. at_front = kwargs.pop('at_front', False)
  269. meta = kwargs.pop('meta', None)
  270. if 'args' in kwargs or 'kwargs' in kwargs:
  271. assert args == (), 'Extra positional arguments cannot be used when using explicit args and kwargs' # noqa
  272. args = kwargs.pop('args', None)
  273. kwargs = kwargs.pop('kwargs', None)
  274. return self.enqueue_call(
  275. func=f, args=args, kwargs=kwargs, timeout=timeout,
  276. result_ttl=result_ttl, ttl=ttl, failure_ttl=failure_ttl,
  277. description=description, depends_on=depends_on, job_id=job_id,
  278. at_front=at_front, meta=meta
  279. )
  280. def enqueue_job(self, job, pipeline=None, at_front=False):
  281. """Enqueues a job for delayed execution.
  282. If Queue is instantiated with is_async=False, job is executed immediately.
  283. """
  284. pipe = pipeline if pipeline is not None else self.connection.pipeline()
  285. # Add Queue key set
  286. pipe.sadd(self.redis_queues_keys, self.key)
  287. job.set_status(JobStatus.QUEUED, pipeline=pipe)
  288. job.origin = self.name
  289. job.enqueued_at = utcnow()
  290. if job.timeout is None:
  291. job.timeout = self._default_timeout
  292. job.save(pipeline=pipe)
  293. job.cleanup(ttl=job.ttl, pipeline=pipe)
  294. if self._is_async:
  295. self.push_job_id(job.id, pipeline=pipe, at_front=at_front)
  296. if pipeline is None:
  297. pipe.execute()
  298. if not self._is_async:
  299. job = self.run_job(job)
  300. return job
  301. def enqueue_dependents(self, job, pipeline=None):
  302. """Enqueues all jobs in the given job's dependents set and clears it.
  303. When called without a pipeline, this method uses WATCH/MULTI/EXEC.
  304. If you pass a pipeline, only MULTI is called. The rest is up to the
  305. caller.
  306. """
  307. from .registry import DeferredJobRegistry
  308. pipe = pipeline if pipeline is not None else self.connection.pipeline()
  309. dependents_key = job.dependents_key
  310. while True:
  311. try:
  312. # if a pipeline is passed, the caller is responsible for calling WATCH
  313. # to ensure all jobs are enqueued
  314. if pipeline is None:
  315. pipe.watch(dependents_key)
  316. dependent_jobs = [self.job_class.fetch(as_text(job_id), connection=self.connection)
  317. for job_id in pipe.smembers(dependents_key)]
  318. pipe.multi()
  319. for dependent in dependent_jobs:
  320. registry = DeferredJobRegistry(dependent.origin,
  321. self.connection,
  322. job_class=self.job_class)
  323. registry.remove(dependent, pipeline=pipe)
  324. if dependent.origin == self.name:
  325. self.enqueue_job(dependent, pipeline=pipe)
  326. else:
  327. queue = Queue(name=dependent.origin, connection=self.connection)
  328. queue.enqueue_job(dependent, pipeline=pipe)
  329. pipe.delete(dependents_key)
  330. if pipeline is None:
  331. pipe.execute()
  332. break
  333. except WatchError:
  334. if pipeline is None:
  335. continue
  336. else:
  337. # if the pipeline comes from the caller, we re-raise the
  338. # exception as it it the responsibility of the caller to
  339. # handle it
  340. raise
  341. def pop_job_id(self):
  342. """Pops a given job ID from this Redis queue."""
  343. return as_text(self.connection.lpop(self.key))
  344. @classmethod
  345. def lpop(cls, queue_keys, timeout, connection=None):
  346. """Helper method. Intermediate method to abstract away from some
  347. Redis API details, where LPOP accepts only a single key, whereas BLPOP
  348. accepts multiple. So if we want the non-blocking LPOP, we need to
  349. iterate over all queues, do individual LPOPs, and return the result.
  350. Until Redis receives a specific method for this, we'll have to wrap it
  351. this way.
  352. The timeout parameter is interpreted as follows:
  353. None - non-blocking (return immediately)
  354. > 0 - maximum number of seconds to block
  355. """
  356. connection = resolve_connection(connection)
  357. if timeout is not None: # blocking variant
  358. if timeout == 0:
  359. raise ValueError('RQ does not support indefinite timeouts. Please pick a timeout value > 0')
  360. result = connection.blpop(queue_keys, timeout)
  361. if result is None:
  362. raise DequeueTimeout(timeout, queue_keys)
  363. queue_key, job_id = result
  364. return queue_key, job_id
  365. else: # non-blocking variant
  366. for queue_key in queue_keys:
  367. blob = connection.lpop(queue_key)
  368. if blob is not None:
  369. return queue_key, blob
  370. return None
  371. @classmethod
  372. def dequeue_any(cls, queues, timeout, connection=None, job_class=None):
  373. """Class method returning the job_class instance at the front of the given
  374. set of Queues, where the order of the queues is important.
  375. When all of the Queues are empty, depending on the `timeout` argument,
  376. either blocks execution of this function for the duration of the
  377. timeout or until new messages arrive on any of the queues, or returns
  378. None.
  379. See the documentation of cls.lpop for the interpretation of timeout.
  380. """
  381. job_class = backend_class(cls, 'job_class', override=job_class)
  382. while True:
  383. queue_keys = [q.key for q in queues]
  384. result = cls.lpop(queue_keys, timeout, connection=connection)
  385. if result is None:
  386. return None
  387. queue_key, job_id = map(as_text, result)
  388. queue = cls.from_queue_key(queue_key,
  389. connection=connection,
  390. job_class=job_class)
  391. try:
  392. job = job_class.fetch(job_id, connection=connection)
  393. except NoSuchJobError:
  394. # Silently pass on jobs that don't exist (anymore),
  395. # and continue in the look
  396. continue
  397. except UnpickleError as e:
  398. # Attach queue information on the exception for improved error
  399. # reporting
  400. e.job_id = job_id
  401. e.queue = queue
  402. raise e
  403. return job, queue
  404. return None, None
  405. # Total ordering defition (the rest of the required Python methods are
  406. # auto-generated by the @total_ordering decorator)
  407. def __eq__(self, other): # noqa
  408. if not isinstance(other, Queue):
  409. raise TypeError('Cannot compare queues to other objects')
  410. return self.name == other.name
  411. def __lt__(self, other):
  412. if not isinstance(other, Queue):
  413. raise TypeError('Cannot compare queues to other objects')
  414. return self.name < other.name
  415. def __hash__(self): # pragma: no cover
  416. return hash(self.name)
  417. def __repr__(self): # noqa # pragma: no cover
  418. return '{0}({1!r})'.format(self.__class__.__name__, self.name)
  419. def __str__(self):
  420. return '<{0} {1}>'.format(self.__class__.__name__, self.name)