mongodb.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from __future__ import absolute_import
  2. import warnings
  3. from apscheduler.jobstores.base import BaseJobStore, JobLookupError, ConflictingIdError
  4. from apscheduler.util import maybe_ref, datetime_to_utc_timestamp, utc_timestamp_to_datetime
  5. from apscheduler.job import Job
  6. try:
  7. import cPickle as pickle
  8. except ImportError: # pragma: nocover
  9. import pickle
  10. try:
  11. from bson.binary import Binary
  12. from pymongo.errors import DuplicateKeyError
  13. from pymongo import MongoClient, ASCENDING
  14. except ImportError: # pragma: nocover
  15. raise ImportError('MongoDBJobStore requires PyMongo installed')
  16. class MongoDBJobStore(BaseJobStore):
  17. """
  18. Stores jobs in a MongoDB database. Any leftover keyword arguments are directly passed to
  19. pymongo's `MongoClient
  20. <http://api.mongodb.org/python/current/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`_.
  21. Plugin alias: ``mongodb``
  22. :param str database: database to store jobs in
  23. :param str collection: collection to store jobs in
  24. :param client: a :class:`~pymongo.mongo_client.MongoClient` instance to use instead of
  25. providing connection arguments
  26. :param int pickle_protocol: pickle protocol level to use (for serialization), defaults to the
  27. highest available
  28. """
  29. def __init__(self, database='apscheduler', collection='jobs', client=None,
  30. pickle_protocol=pickle.HIGHEST_PROTOCOL, **connect_args):
  31. super(MongoDBJobStore, self).__init__()
  32. self.pickle_protocol = pickle_protocol
  33. if not database:
  34. raise ValueError('The "database" parameter must not be empty')
  35. if not collection:
  36. raise ValueError('The "collection" parameter must not be empty')
  37. if client:
  38. self.client = maybe_ref(client)
  39. else:
  40. connect_args.setdefault('w', 1)
  41. self.client = MongoClient(**connect_args)
  42. self.collection = self.client[database][collection]
  43. def start(self, scheduler, alias):
  44. super(MongoDBJobStore, self).start(scheduler, alias)
  45. self.collection.ensure_index('next_run_time', sparse=True)
  46. @property
  47. def connection(self):
  48. warnings.warn('The "connection" member is deprecated -- use "client" instead',
  49. DeprecationWarning)
  50. return self.client
  51. def lookup_job(self, job_id):
  52. document = self.collection.find_one(job_id, ['job_state'])
  53. return self._reconstitute_job(document['job_state']) if document else None
  54. def get_due_jobs(self, now):
  55. timestamp = datetime_to_utc_timestamp(now)
  56. return self._get_jobs({'next_run_time': {'$lte': timestamp}})
  57. def get_next_run_time(self):
  58. document = self.collection.find_one({'next_run_time': {'$ne': None}},
  59. projection=['next_run_time'],
  60. sort=[('next_run_time', ASCENDING)])
  61. return utc_timestamp_to_datetime(document['next_run_time']) if document else None
  62. def get_all_jobs(self):
  63. jobs = self._get_jobs({})
  64. self._fix_paused_jobs_sorting(jobs)
  65. return jobs
  66. def add_job(self, job):
  67. try:
  68. self.collection.insert({
  69. '_id': job.id,
  70. 'next_run_time': datetime_to_utc_timestamp(job.next_run_time),
  71. 'job_state': Binary(pickle.dumps(job.__getstate__(), self.pickle_protocol))
  72. })
  73. except DuplicateKeyError:
  74. raise ConflictingIdError(job.id)
  75. def update_job(self, job):
  76. changes = {
  77. 'next_run_time': datetime_to_utc_timestamp(job.next_run_time),
  78. 'job_state': Binary(pickle.dumps(job.__getstate__(), self.pickle_protocol))
  79. }
  80. result = self.collection.update({'_id': job.id}, {'$set': changes})
  81. if result and result['n'] == 0:
  82. raise JobLookupError(job.id)
  83. def remove_job(self, job_id):
  84. result = self.collection.remove(job_id)
  85. if result and result['n'] == 0:
  86. raise JobLookupError(job_id)
  87. def remove_all_jobs(self):
  88. self.collection.remove()
  89. def shutdown(self):
  90. self.client.close()
  91. def _reconstitute_job(self, job_state):
  92. job_state = pickle.loads(job_state)
  93. job = Job.__new__(Job)
  94. job.__setstate__(job_state)
  95. job._scheduler = self._scheduler
  96. job._jobstore_alias = self._alias
  97. return job
  98. def _get_jobs(self, conditions):
  99. jobs = []
  100. failed_job_ids = []
  101. for document in self.collection.find(conditions, ['_id', 'job_state'],
  102. sort=[('next_run_time', ASCENDING)]):
  103. try:
  104. jobs.append(self._reconstitute_job(document['job_state']))
  105. except:
  106. self._logger.exception('Unable to restore job "%s" -- removing it',
  107. document['_id'])
  108. failed_job_ids.append(document['_id'])
  109. # Remove all the jobs we failed to restore
  110. if failed_job_ids:
  111. self.collection.remove({'_id': {'$in': failed_job_ids}})
  112. return jobs
  113. def __repr__(self):
  114. return '<%s (client=%s)>' % (self.__class__.__name__, self.client)