redis.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from __future__ import absolute_import
  2. from datetime import datetime
  3. from pytz import utc
  4. import six
  5. from apscheduler.jobstores.base import BaseJobStore, JobLookupError, ConflictingIdError
  6. from apscheduler.util import datetime_to_utc_timestamp, utc_timestamp_to_datetime
  7. from apscheduler.job import Job
  8. try:
  9. import cPickle as pickle
  10. except ImportError: # pragma: nocover
  11. import pickle
  12. try:
  13. from redis import StrictRedis
  14. except ImportError: # pragma: nocover
  15. raise ImportError('RedisJobStore requires redis installed')
  16. class RedisJobStore(BaseJobStore):
  17. """
  18. Stores jobs in a Redis database. Any leftover keyword arguments are directly passed to redis's
  19. :class:`~redis.StrictRedis`.
  20. Plugin alias: ``redis``
  21. :param int db: the database number to store jobs in
  22. :param str jobs_key: key to store jobs in
  23. :param str run_times_key: key to store the jobs' run times in
  24. :param int pickle_protocol: pickle protocol level to use (for serialization), defaults to the
  25. highest available
  26. """
  27. def __init__(self, db=0, jobs_key='apscheduler.jobs', run_times_key='apscheduler.run_times',
  28. pickle_protocol=pickle.HIGHEST_PROTOCOL, **connect_args):
  29. super(RedisJobStore, self).__init__()
  30. if db is None:
  31. raise ValueError('The "db" parameter must not be empty')
  32. if not jobs_key:
  33. raise ValueError('The "jobs_key" parameter must not be empty')
  34. if not run_times_key:
  35. raise ValueError('The "run_times_key" parameter must not be empty')
  36. self.pickle_protocol = pickle_protocol
  37. self.jobs_key = jobs_key
  38. self.run_times_key = run_times_key
  39. self.redis = StrictRedis(db=int(db), **connect_args)
  40. def lookup_job(self, job_id):
  41. job_state = self.redis.hget(self.jobs_key, job_id)
  42. return self._reconstitute_job(job_state) if job_state else None
  43. def get_due_jobs(self, now):
  44. timestamp = datetime_to_utc_timestamp(now)
  45. job_ids = self.redis.zrangebyscore(self.run_times_key, 0, timestamp)
  46. if job_ids:
  47. job_states = self.redis.hmget(self.jobs_key, *job_ids)
  48. return self._reconstitute_jobs(six.moves.zip(job_ids, job_states))
  49. return []
  50. def get_next_run_time(self):
  51. next_run_time = self.redis.zrange(self.run_times_key, 0, 0, withscores=True)
  52. if next_run_time:
  53. return utc_timestamp_to_datetime(next_run_time[0][1])
  54. def get_all_jobs(self):
  55. job_states = self.redis.hgetall(self.jobs_key)
  56. jobs = self._reconstitute_jobs(six.iteritems(job_states))
  57. paused_sort_key = datetime(9999, 12, 31, tzinfo=utc)
  58. return sorted(jobs, key=lambda job: job.next_run_time or paused_sort_key)
  59. def add_job(self, job):
  60. if self.redis.hexists(self.jobs_key, job.id):
  61. raise ConflictingIdError(job.id)
  62. with self.redis.pipeline() as pipe:
  63. pipe.multi()
  64. pipe.hset(self.jobs_key, job.id, pickle.dumps(job.__getstate__(),
  65. self.pickle_protocol))
  66. if job.next_run_time:
  67. pipe.zadd(self.run_times_key, datetime_to_utc_timestamp(job.next_run_time), job.id)
  68. pipe.execute()
  69. def update_job(self, job):
  70. if not self.redis.hexists(self.jobs_key, job.id):
  71. raise JobLookupError(job.id)
  72. with self.redis.pipeline() as pipe:
  73. pipe.hset(self.jobs_key, job.id, pickle.dumps(job.__getstate__(),
  74. self.pickle_protocol))
  75. if job.next_run_time:
  76. pipe.zadd(self.run_times_key, datetime_to_utc_timestamp(job.next_run_time), job.id)
  77. else:
  78. pipe.zrem(self.run_times_key, job.id)
  79. pipe.execute()
  80. def remove_job(self, job_id):
  81. if not self.redis.hexists(self.jobs_key, job_id):
  82. raise JobLookupError(job_id)
  83. with self.redis.pipeline() as pipe:
  84. pipe.hdel(self.jobs_key, job_id)
  85. pipe.zrem(self.run_times_key, job_id)
  86. pipe.execute()
  87. def remove_all_jobs(self):
  88. with self.redis.pipeline() as pipe:
  89. pipe.delete(self.jobs_key)
  90. pipe.delete(self.run_times_key)
  91. pipe.execute()
  92. def shutdown(self):
  93. self.redis.connection_pool.disconnect()
  94. def _reconstitute_job(self, job_state):
  95. job_state = pickle.loads(job_state)
  96. job = Job.__new__(Job)
  97. job.__setstate__(job_state)
  98. job._scheduler = self._scheduler
  99. job._jobstore_alias = self._alias
  100. return job
  101. def _reconstitute_jobs(self, job_states):
  102. jobs = []
  103. failed_job_ids = []
  104. for job_id, job_state in job_states:
  105. try:
  106. jobs.append(self._reconstitute_job(job_state))
  107. except:
  108. self._logger.exception('Unable to restore job "%s" -- removing it', job_id)
  109. failed_job_ids.append(job_id)
  110. # Remove all the jobs we failed to restore
  111. if failed_job_ids:
  112. with self.redis.pipeline() as pipe:
  113. pipe.hdel(self.jobs_key, *failed_job_ids)
  114. pipe.zrem(self.run_times_key, *failed_job_ids)
  115. pipe.execute()
  116. return jobs
  117. def __repr__(self):
  118. return '<%s>' % self.__class__.__name__