def _SetCronEnabledBit(self, cronjob_id, enabled, cursor=None): res = cursor.execute( "UPDATE cron_jobs SET enabled=%d WHERE job_id=%%s" % int(enabled), [cronjob_id]) if res != 1: raise db.UnknownCronJobError("CronJob with id %s not found." % cronjob_id)
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
def WriteCronJobRun(self, run_object): """Stores a cron job run object in the database.""" if run_object.cron_job_id not in self.cronjobs: raise db.UnknownCronJobError( "Job with id %s not found." % run_object.cron_job_id) clone = run_object.Copy() clone.timestamp = rdfvalue.RDFDatetime.Now() self.cronjob_runs[(clone.cron_job_id, clone.run_id)] = clone
def DeleteCronJob(self, cronjob_id): """Deletes a cronjob along with all its runs.""" 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 for job_run in self.ReadCronJobRuns(cronjob_id): del self.cronjob_runs[(cronjob_id, job_run.run_id)]
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
def WriteCronJobRun(self, run_object, cursor=None): """Stores a cron job run object in the database.""" query = ("INSERT INTO cron_job_runs " "(job_id, run_id, write_time, run) " "VALUES (%s, %s, %s, %s) " "ON DUPLICATE KEY UPDATE " "run=VALUES(run), write_time=VALUES(write_time)") write_time_str = mysql_utils.RDFDatetimeToMysqlString( rdfvalue.RDFDatetime.Now()) try: cursor.execute(query, [ run_object.cron_job_id, run_object.run_id, write_time_str, run_object.SerializeToString(), ]) except MySQLdb.IntegrityError as e: raise db.UnknownCronJobError( "CronJob with id %s not found." % run_object.cron_job_id, cause=e)
def ReadCronJobs(self, cronjob_ids=None, cursor=None): """Reads all cronjobs from the database.""" query = ("SELECT job, create_time, enabled, forced_run_requested, " "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.cron_job_id for c in res]) raise db.UnknownCronJobError( "CronJob(s) with id(s) %s not found." % missing) return res
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, forced_run_requested=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 if forced_run_requested != db.Database.unchanged: job.forced_run_requested = forced_run_requested
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, forced_run_requested=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(mysql_utils.CronJobRunIDToInt(current_run_id)) if state != db.Database.unchanged: updates.append("state=%s") args.append(state.SerializeToString()) if forced_run_requested != db.Database.unchanged: updates.append("forced_run_requested=%s") args.append(forced_run_requested) 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)
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
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)