Developer's Guide

  • Docs Home
  • Community Home

3. Life Cycle of a Job

When zenjobs runs a Job, it calls the start() method, which calls run() and returns a Deferred that will fire when the Job finishes; setup steps that can't happen in run() for whatever reason should occur here. run() should, as mentioned above, call finished(); Jobs that require post-run actions may override finished() to provide them.

Example 7.2. A Job that sends an email when starting and finishing

class EmailSendingJob(Job):

    def start()
        self.preRun()
        return super(EmailSendingJob, self).start()

    def run(self, r):
        # Do whatever
        self.finished(SUCCESS)

    def finished(self, r):
        self.postRun()
        return super(EmailSendingJob, self).finished(r)

    def preRun(self):
        sendEmail("Job %s is starting" % self.id)

    def postRun(self):
        sendEmail("Job %s has finished" % self.id)

A Job may provide an interrupt() method that halts the job. The implementation of this method in the base class does nothing at all.