Пример #1
0
    def UpdateCronJob(self,
                      cronjob_id,
                      last_run_status=db.Database.unchanged,
                      last_run_time=db.Database.unchanged,
                      current_run_id=db.Database.unchanged,
                      state=db.Database.unchanged,
                      cursor=None):
        """Updates run information for an existing cron job."""
        updates = []
        args = []
        if last_run_status != db.Database.unchanged:
            updates.append("last_run_status=%s")
            args.append(int(last_run_status))
        if last_run_time != db.Database.unchanged:
            updates.append("last_run_time=%s")
            args.append(mysql_utils.RDFDatetimeToMysqlString(last_run_time))
        if current_run_id != db.Database.unchanged:
            updates.append("current_run_id=%s")
            args.append(current_run_id or 0)
        if state != db.Database.unchanged:
            updates.append("state=%s")
            args.append(state.SerializeToString())

        if not updates:
            return

        query = "UPDATE cron_jobs SET "
        query += ", ".join(updates)
        query += " WHERE job_id=%s"
        res = cursor.execute(query, args + [cronjob_id])
        if res != 1:
            raise db.UnknownCronjobError("Cronjob with id %s not found." %
                                         cronjob_id)
Пример #2
0
 def _SetCronDisabledBit(self, cronjob_id, disabled, cursor=None):
     res = cursor.execute(
         "UPDATE cron_jobs SET disabled=%d WHERE job_id=%%s" %
         int(disabled), [cronjob_id])
     if res != 1:
         raise db.UnknownCronjobError("Cronjob with id %s not found." %
                                      cronjob_id)
Пример #3
0
 def DeleteCronJob(self, cronjob_id):
     """Deletes a cronjob."""
     if cronjob_id not in self.cronjobs:
         raise db.UnknownCronjobError("Cron job %s not known." % cronjob_id)
     del self.cronjobs[cronjob_id]
     try:
         del self.cronjob_leases[cronjob_id]
     except KeyError:
         pass
Пример #4
0
    def ReadCronJobs(self, cronjob_ids=None):
        """Reads a cronjob from the database."""
        if cronjob_ids is None:
            res = [job.Copy() for job in itervalues(self.cronjobs)]

        else:
            res = []
            for job_id in cronjob_ids:
                try:
                    res.append(self.cronjobs[job_id].Copy())
                except KeyError:
                    raise db.UnknownCronjobError(
                        "Cron job with id %s not found." % job_id)

        for job in res:
            lease = self.cronjob_leases.get(job.cron_job_id)
            if lease:
                job.leased_until, job.leased_by = lease
        return res
Пример #5
0
  def UpdateCronJob(self,
                    cronjob_id,
                    last_run_status=db.Database.unchanged,
                    last_run_time=db.Database.unchanged,
                    current_run_id=db.Database.unchanged,
                    state=db.Database.unchanged):
    """Updates run information for an existing cron job."""
    job = self.cronjobs.get(cronjob_id)
    if job is None:
      raise db.UnknownCronjobError("Cron job %s not known." % cronjob_id)

    if last_run_status != db.Database.unchanged:
      job.last_run_status = last_run_status
    if last_run_time != db.Database.unchanged:
      job.last_run_time = last_run_time
    if current_run_id != db.Database.unchanged:
      job.current_run_id = current_run_id
    if state != db.Database.unchanged:
      job.state = state
Пример #6
0
    def ReadCronJobs(self, cronjob_ids=None, cursor=None):
        """Reads all cronjobs from the database."""
        query = ("SELECT job, create_time, disabled, "
                 "last_run_status, last_run_time, current_run_id, state, "
                 "leased_until, leased_by "
                 "FROM cron_jobs")
        if cronjob_ids is None:
            cursor.execute(query)
            return [self._CronjobFromRow(row) for row in cursor.fetchall()]

        query += " WHERE job_id IN (%s)" % ", ".join(["%s"] * len(cronjob_ids))
        cursor.execute(query, cronjob_ids)
        res = []
        for row in cursor.fetchall():
            res.append(self._CronjobFromRow(row))

        if len(res) != len(cronjob_ids):
            missing = set(cronjob_ids) - set([c.job_id for c in res])
            raise db.UnknownCronjobError(
                "Cronjob(s) with id(s) %s not found." % missing)
        return res
Пример #7
0
 def DisableCronJob(self, cronjob_id):
     """Disables a cronjob."""
     job = self.cronjobs.get(cronjob_id)
     if job is None:
         raise db.UnknownCronjobError("Cron job %s not known." % cronjob_id)
     job.enabled = False
Пример #8
0
 def DeleteCronJob(self, cronjob_id, cursor=None):
     res = cursor.execute("DELETE FROM cron_jobs WHERE job_id=%s",
                          [cronjob_id])
     if res != 1:
         raise db.UnknownCronjobError("Cronjob with id %s not found." %
                                      cronjob_id)