app/soc/cron/job.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 19 Apr 2009 00:06:12 +0000
changeset 2229 b36ecf371aef
parent 2224 fc719e902a70
child 2243 c61f9dd5e325
permissions -rw-r--r--
Store how many times a job has timed out and abort if needed Patch by: Sverre Rabblier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Cron jobs.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
2219
6f835882160e Fixed typo and added myself to authors in the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2217
diff changeset
    22
    '"Lennard de Rijk" <ljvderijk@gmail.com>',
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
  ]
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
import logging
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
from google.appengine.ext import db
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
from google.appengine.runtime import DeadlineExceededError
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
2224
fc719e902a70 Hook up the Student Proposal Mailer in the cron/jobs module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2219
diff changeset
    31
from soc.cron import student_proposal_mailer
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
from soc.models.job import Job
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
2217
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    34
class Error(Exception):
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    35
  """Base class for all exceptions raised by this module.
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    36
  """
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    37
  pass
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    38
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    39
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    40
class FatalJobError(Error):
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    41
  """Class for all errors that lead to immediate job abortion.
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    42
  """
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    43
  pass
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    44
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    45
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
class Handler(object):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
  """A handler that dispatches a cron job.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
  The tasks that are mapped into tasks will be called when a worker
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
  has claimed the job. However, there is no guarantee as to how long
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
  the task will be allowed to run. If an Exception is raised the task
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
  is automatically rescheduled for execution.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
  """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
  def __init__(self):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
    """Constructs a new Handler with all known jobs set.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
    """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
    self.tasks = {}
2224
fc719e902a70 Hook up the Student Proposal Mailer in the cron/jobs module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2219
diff changeset
    60
    self.tasks['setupStudentProposalMailing'] = \
fc719e902a70 Hook up the Student Proposal Mailer in the cron/jobs module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2219
diff changeset
    61
        student_proposal_mailer.setupStudentProposalMailing
fc719e902a70 Hook up the Student Proposal Mailer in the cron/jobs module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2219
diff changeset
    62
    self.tasks['sendStudentProposalMail'] = \
fc719e902a70 Hook up the Student Proposal Mailer in the cron/jobs module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2219
diff changeset
    63
        student_proposal_mailer.sendStudentProposalMail
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
  def claimJob(self, job_key):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
    """A transaction to claim a job.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
    """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
    job = Job.get_by_id(job_key)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
    if job.status != 'waiting':
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
      raise db.Rollback()
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
    job.status = 'started'
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
    if job.put():
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
      return job
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
    else:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
      return None
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
  def freeJob(self, job_key):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
    """A transaction to free a job.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
    """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    84
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
    job = Job.get_by_id(job_key)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
    job.status = 'waiting'
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
    return job.put()
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
2229
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
    91
  def timeoutJob(self, job_key):
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
    92
    """A transaction to tiemout a job.
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
    93
    """
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
    94
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
    95
    job = Job.get_by_id(job_key)
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
    96
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
    97
    job.timeouts += 1
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
    98
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
    99
    if job.timeouts > 50:
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   100
      job.status = 'aborted'
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   101
    else:
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   102
      job.status = 'waiting'
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   103
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   104
    job_id = job.key().id()
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   105
    logging.debug("job %d now timeout %d time(s)" % (job_id, job.timeouts))
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   106
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   107
    return job.put()
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   108
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
  def failJob(self, job_key):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
    """A transaction to fail a job.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
    """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
    job = Job.get_by_id(job_key)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   114
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   115
    job.errors += 1
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   116
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
    if job.errors > 5:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
      job.status = 'aborted'
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
    else:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
      job.status = 'waiting'
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   121
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
    job_id = job.key().id()
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
    logging.warning("job %d now failed %d time(s)" % (job_id, job.errors))
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   125
    return job.put()
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
  def finishJob(self, job_key):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   128
    """A transaction to finish a job.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   129
    """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   130
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   131
    job = Job.get_by_id(job_key)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   132
    job.status = 'finished'
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   133
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   134
    return job.put()
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   135
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   136
  def abortJob(self, job_key):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   137
    """A transaction to abort a job.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
    """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
    job = Job.get_by_id(job_key)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
    job.status = 'aborted'
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   142
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   143
    return job.put()
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
  def handle(self, job_key):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
    """Handle one job.
2213
c0f52da7a808 Remove testing method and update docstring for cron system
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2211
diff changeset
   147
c0f52da7a808 Remove testing method and update docstring for cron system
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2211
diff changeset
   148
    Returns: whether another job should be started after this one
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   149
    """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   150
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   151
    try:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   152
      job = db.run_in_transaction(self.claimJob, job_key)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   153
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   154
      if not job:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   155
        # someone already claimed the job
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   156
        return True
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   157
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   158
      if job.task_name not in self.tasks:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   159
        logging.error("Unknown job %s" % job.task_name)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   160
        db.run_in_transaction(self.abortJob, job_key)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   161
        return True
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   163
      task = self.tasks[job.task_name]
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   164
2219
6f835882160e Fixed typo and added myself to authors in the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2217
diff changeset
   165
      # execute the actual job
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   166
      task(job)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   167
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   168
      db.run_in_transaction(self.finishJob, job_key)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   169
      return True
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   170
    except DeadlineExceededError, exception:
2229
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   171
      db.run_in_transaction(self.timeoutJob, job_key)
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   172
      return False
2217
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
   173
    except FatalJobError, exception:
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
   174
      logging.exception(exception)
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
   175
      db.run_in_transaction(self.abortJob, job_key)
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
   176
      return True
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   177
    except Exception, exception:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   178
      logging.exception(exception)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   179
      db.run_in_transaction(self.failJob, job_key)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   180
      return True
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   181
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   182
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   183
handler = Handler()