events.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. __all__ = ('EVENT_SCHEDULER_STARTED', 'EVENT_SCHEDULER_SHUTDOWN', 'EVENT_SCHEDULER_PAUSED',
  2. 'EVENT_SCHEDULER_RESUMED', 'EVENT_EXECUTOR_ADDED', 'EVENT_EXECUTOR_REMOVED',
  3. 'EVENT_JOBSTORE_ADDED', 'EVENT_JOBSTORE_REMOVED', 'EVENT_ALL_JOBS_REMOVED',
  4. 'EVENT_JOB_ADDED', 'EVENT_JOB_REMOVED', 'EVENT_JOB_MODIFIED', 'EVENT_JOB_EXECUTED',
  5. 'EVENT_JOB_ERROR', 'EVENT_JOB_MISSED', 'EVENT_JOB_SUBMITTED', 'EVENT_JOB_MAX_INSTANCES',
  6. 'SchedulerEvent', 'JobEvent', 'JobExecutionEvent')
  7. EVENT_SCHEDULER_STARTED = EVENT_SCHEDULER_START = 2 ** 0
  8. EVENT_SCHEDULER_SHUTDOWN = 2 ** 1
  9. EVENT_SCHEDULER_PAUSED = 2 ** 2
  10. EVENT_SCHEDULER_RESUMED = 2 ** 3
  11. EVENT_EXECUTOR_ADDED = 2 ** 4
  12. EVENT_EXECUTOR_REMOVED = 2 ** 5
  13. EVENT_JOBSTORE_ADDED = 2 ** 6
  14. EVENT_JOBSTORE_REMOVED = 2 ** 7
  15. EVENT_ALL_JOBS_REMOVED = 2 ** 8
  16. EVENT_JOB_ADDED = 2 ** 9
  17. EVENT_JOB_REMOVED = 2 ** 10
  18. EVENT_JOB_MODIFIED = 2 ** 11
  19. EVENT_JOB_EXECUTED = 2 ** 12
  20. EVENT_JOB_ERROR = 2 ** 13
  21. EVENT_JOB_MISSED = 2 ** 14
  22. EVENT_JOB_SUBMITTED = 2 ** 15
  23. EVENT_JOB_MAX_INSTANCES = 2 ** 16
  24. EVENT_ALL = (EVENT_SCHEDULER_STARTED | EVENT_SCHEDULER_SHUTDOWN | EVENT_SCHEDULER_PAUSED |
  25. EVENT_SCHEDULER_RESUMED | EVENT_EXECUTOR_ADDED | EVENT_EXECUTOR_REMOVED |
  26. EVENT_JOBSTORE_ADDED | EVENT_JOBSTORE_REMOVED | EVENT_ALL_JOBS_REMOVED |
  27. EVENT_JOB_ADDED | EVENT_JOB_REMOVED | EVENT_JOB_MODIFIED | EVENT_JOB_EXECUTED |
  28. EVENT_JOB_ERROR | EVENT_JOB_MISSED | EVENT_JOB_SUBMITTED | EVENT_JOB_MAX_INSTANCES)
  29. class SchedulerEvent(object):
  30. """
  31. An event that concerns the scheduler itself.
  32. :ivar code: the type code of this event
  33. :ivar alias: alias of the job store or executor that was added or removed (if applicable)
  34. """
  35. def __init__(self, code, alias=None):
  36. super(SchedulerEvent, self).__init__()
  37. self.code = code
  38. self.alias = alias
  39. def __repr__(self):
  40. return '<%s (code=%d)>' % (self.__class__.__name__, self.code)
  41. class JobEvent(SchedulerEvent):
  42. """
  43. An event that concerns a job.
  44. :ivar code: the type code of this event
  45. :ivar job_id: identifier of the job in question
  46. :ivar jobstore: alias of the job store containing the job in question
  47. """
  48. def __init__(self, code, job_id, jobstore):
  49. super(JobEvent, self).__init__(code)
  50. self.code = code
  51. self.job_id = job_id
  52. self.jobstore = jobstore
  53. class JobSubmissionEvent(JobEvent):
  54. """
  55. An event that concerns the submission of a job to its executor.
  56. :ivar scheduled_run_times: a list of datetimes when the job was intended to run
  57. """
  58. def __init__(self, code, job_id, jobstore, scheduled_run_times):
  59. super(JobSubmissionEvent, self).__init__(code, job_id, jobstore)
  60. self.scheduled_run_times = scheduled_run_times
  61. class JobExecutionEvent(JobEvent):
  62. """
  63. An event that concerns the running of a job within its executor.
  64. :ivar scheduled_run_time: the time when the job was scheduled to be run
  65. :ivar retval: the return value of the successfully executed job
  66. :ivar exception: the exception raised by the job
  67. :ivar traceback: a formatted traceback for the exception
  68. """
  69. def __init__(self, code, job_id, jobstore, scheduled_run_time, retval=None, exception=None,
  70. traceback=None):
  71. super(JobExecutionEvent, self).__init__(code, job_id, jobstore)
  72. self.scheduled_run_time = scheduled_run_time
  73. self.retval = retval
  74. self.exception = exception
  75. self.traceback = traceback