app/soc/cron/job.py
author Daniel Diniz <ajaksu@gmail.com>
Wed, 08 Jul 2009 10:40:46 +0200
changeset 2570 851640749319
parent 2366 1a954e80d444
permissions -rw-r--r--
Several Survey UI fixes. Fixes: Too narrow fieldsets in new question/option dialogs. Survey submit (on take view) and save/export/etc. buttons at weird places, instead of at bottom. Weird placement of radio buttons in Opera. Too narrow selects in IE. Broken images in edit view in IE and Opera. Reviewed by: Lennard de Rijk (Only on IE)
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
2345
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2247
diff changeset
    32
from soc.cron import unique_user_id_adder
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
from soc.models.job import Job
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
2345
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2247
diff changeset
    35
2217
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    36
class Error(Exception):
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    37
  """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
    38
  """
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    39
  pass
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    40
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    41
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    42
class FatalJobError(Error):
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    43
  """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
    44
  """
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    45
  pass
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    46
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
    47
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
class Handler(object):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
  """A handler that dispatches a cron job.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
  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
    52
  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
    53
  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
    54
  is automatically rescheduled for execution.
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
  """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
  def __init__(self):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
    """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
    59
    """
2366
1a954e80d444 Style fixes and pylint: disable-msg comments in different modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2345
diff changeset
    60
    # pylint: disable-msg=C0103
2243
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
    61
    self.OUT_OF_TIME = 0
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
    62
    self.ALREADY_CLAIMED = 1
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
    63
    self.SUCCESS = 2
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
    64
    self.ABORTED = 3
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
    65
    self.ERRORED = 4
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
    66
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
    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
    68
    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
    69
        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
    70
    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
    71
        student_proposal_mailer.sendStudentProposalMail
2345
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2247
diff changeset
    72
    self.tasks['setupUniqueUserIdAdder'] = \
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2247
diff changeset
    73
        unique_user_id_adder.setupUniqueUserIdAdder
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2247
diff changeset
    74
    self.tasks['addUniqueUserIds'] = \
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2247
diff changeset
    75
        unique_user_id_adder.addUniqueUserIds
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
  def claimJob(self, job_key):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
    """A transaction to claim a job.
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
    79
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
    80
    The transaction is rolled back if the status is not 'waiting'.
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
    """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
    job = Job.get_by_id(job_key)
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
    if job.status != 'waiting':
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
      raise db.Rollback()
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
    job.status = 'started'
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
2366
1a954e80d444 Style fixes and pylint: disable-msg comments in different modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2345
diff changeset
    90
    # pylint: disable-msg=E1103
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
    if job.put():
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
      return job
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
    else:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
      return None
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
    96
  def timeoutJob(self, job):
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
    97
    """Timeout a job.
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
    99
    If a job has timed out more than 50 times, the job is aborted.
2229
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   100
    """
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   101
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.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
   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
    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
   105
      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
   106
    else:
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   107
      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
   108
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   109
    job.put()
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   110
2229
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   111
    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
   112
    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
   113
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   114
  def failJob(self, job):
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   115
    """Fail a job.
2229
b36ecf371aef Store how many times a job has timed out and abort if needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2224
diff changeset
   116
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   117
    If the job has failed more than 5 times, the job is aborted.
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
    """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
    job.errors += 1
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
    if job.errors > 5:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
      job.status = 'aborted'
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
    else:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   125
      job.status = 'waiting'
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   127
    job.put()
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   128
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   129
    job_id = job.key().id()
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   130
    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
   131
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   132
  def finishJob(self, job):
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   133
    """Finish a job.
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   134
    """
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
    job.status = 'finished'
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   137
    job.put()
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   139
  def abortJob(self, job):
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   140
    """Abort a job.
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
    """
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
    job.status = 'aborted'
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   144
    job.put()
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
  def handle(self, job_key):
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   147
    """Handle one job.
2213
c0f52da7a808 Remove testing method and update docstring for cron system
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2211
diff changeset
   148
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   149
    Returns: one of the following status codes:
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   150
      self.OUT_OF_TIME: returned when a DeadlineExceededError is raised
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   151
      self.ALREADY_CLAIMED: if job.status is not 'waiting'
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   152
      self.SUCCESS: if the job.status has been set to 'succes'
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   153
      self.ABORTED: if the job.status has been set to 'aborted'
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   154
      self.ERRORED: if the job encountered an error
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   155
    """
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   156
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   157
    job = None
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   158
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   159
    try:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   160
      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
   161
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
      if not job:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   163
        # someone already claimed the job
2243
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
   164
        return self.ALREADY_CLAIMED
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   165
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   166
      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
   167
        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
   168
        db.run_in_transaction(self.abortJob, job_key)
2243
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
   169
        return self.ABORTED
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   170
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   171
      task = self.tasks[job.task_name]
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   172
2219
6f835882160e Fixed typo and added myself to authors in the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2217
diff changeset
   173
      # execute the actual job
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   174
      task(job)
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   175
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   176
      self.finishJob(job)
2243
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
   177
      return self.SUCCESS
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   178
    except DeadlineExceededError, exception:
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   179
      if job:
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   180
        self.timeoutJob(job)
2243
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
   181
      return self.OUT_OF_TIME
2217
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
   182
    except FatalJobError, exception:
2c6ec0357149 Added FatalJobError to the cron/job module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2213
diff changeset
   183
      logging.exception(exception)
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   184
      if job:
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   185
        self.abortJob(job)
2243
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
   186
      return self.ABORTED
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   187
    except Exception, exception:
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   188
      logging.exception(exception)
2247
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   189
      if job:
64968b86b07c Don't run in a transaction if not needed
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2246
diff changeset
   190
        self.failJob(job)
2243
c61f9dd5e325 Use status codes in job.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2229
diff changeset
   191
      return self.ERRORED
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   192
2246
c29272f640b0 Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2244
diff changeset
   193
  def iterate(self, jobs, retry_jobs):
c29272f640b0 Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2244
diff changeset
   194
    """Trivial iterator that iterates over jobs then retry_jobs
c29272f640b0 Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2244
diff changeset
   195
    """
c29272f640b0 Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2244
diff changeset
   196
c29272f640b0 Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2244
diff changeset
   197
    for job in jobs:
c29272f640b0 Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2244
diff changeset
   198
      yield job
c29272f640b0 Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2244
diff changeset
   199
    while retry_jobs:
c29272f640b0 Tweak the 'load balancing' algorithm
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2244
diff changeset
   200
      yield retry_jobs[0]
2211
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   201
f7497180d037 Add cron, the core of the job system
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   202
handler = Handler()