app/soc/cron/job.py
changeset 2243 c61f9dd5e325
parent 2229 b36ecf371aef
child 2244 96d8083cf974
equal deleted inserted replaced
2242:0326d394dce5 2243:c61f9dd5e325
    53   """
    53   """
    54 
    54 
    55   def __init__(self):
    55   def __init__(self):
    56     """Constructs a new Handler with all known jobs set.
    56     """Constructs a new Handler with all known jobs set.
    57     """
    57     """
       
    58 
       
    59     self.OUT_OF_TIME = 0
       
    60     self.ALREADY_CLAIMED = 1
       
    61     self.SUCCESS = 2
       
    62     self.ABORTED = 3
       
    63     self.ERRORED = 4
    58 
    64 
    59     self.tasks = {}
    65     self.tasks = {}
    60     self.tasks['setupStudentProposalMailing'] = \
    66     self.tasks['setupStudentProposalMailing'] = \
    61         student_proposal_mailer.setupStudentProposalMailing
    67         student_proposal_mailer.setupStudentProposalMailing
    62     self.tasks['sendStudentProposalMail'] = \
    68     self.tasks['sendStudentProposalMail'] = \
   151     try:
   157     try:
   152       job = db.run_in_transaction(self.claimJob, job_key)
   158       job = db.run_in_transaction(self.claimJob, job_key)
   153 
   159 
   154       if not job:
   160       if not job:
   155         # someone already claimed the job
   161         # someone already claimed the job
   156         return True
   162         return self.ALREADY_CLAIMED
   157 
   163 
   158       if job.task_name not in self.tasks:
   164       if job.task_name not in self.tasks:
   159         logging.error("Unknown job %s" % job.task_name)
   165         logging.error("Unknown job %s" % job.task_name)
   160         db.run_in_transaction(self.abortJob, job_key)
   166         db.run_in_transaction(self.abortJob, job_key)
   161         return True
   167         return self.ABORTED
   162 
   168 
   163       task = self.tasks[job.task_name]
   169       task = self.tasks[job.task_name]
   164 
   170 
   165       # execute the actual job
   171       # execute the actual job
   166       task(job)
   172       task(job)
   167 
   173 
   168       db.run_in_transaction(self.finishJob, job_key)
   174       db.run_in_transaction(self.finishJob, job_key)
   169       return True
   175       return self.SUCCESS
   170     except DeadlineExceededError, exception:
   176     except DeadlineExceededError, exception:
   171       db.run_in_transaction(self.timeoutJob, job_key)
   177       db.run_in_transaction(self.timeoutJob, job_key)
   172       return False
   178       return self.OUT_OF_TIME
   173     except FatalJobError, exception:
   179     except FatalJobError, exception:
   174       logging.exception(exception)
   180       logging.exception(exception)
   175       db.run_in_transaction(self.abortJob, job_key)
   181       db.run_in_transaction(self.abortJob, job_key)
   176       return True
   182       return self.ABORTED
   177     except Exception, exception:
   183     except Exception, exception:
   178       logging.exception(exception)
   184       logging.exception(exception)
   179       db.run_in_transaction(self.failJob, job_key)
   185       db.run_in_transaction(self.failJob, job_key)
   180       return True
   186       return self.ERRORED
   181 
   187 
   182 
   188 
   183 handler = Handler()
   189 handler = Handler()