/// <summary> /// Remove (delete) the <see cref="Trigger" /> with the /// given name, and store the new given one - which must be associated /// with the same job. /// </summary> /// <param name="ctxt">The scheduling context.</param> /// <param name="triggerName">The name of the <see cref="Trigger" /> to be removed.</param> /// <param name="groupName">The group name of the <see cref="Trigger" /> to be removed.</param> /// <param name="newTrigger">The new <see cref="Trigger" /> to be stored.</param> /// <returns> /// <see langword="null" /> if a <see cref="Trigger" /> with the given /// name and group was not found and removed from the store, otherwise /// the first fire time of the newly scheduled trigger. /// </returns> public virtual NullableDateTime RescheduleJob(SchedulingContext ctxt, string triggerName, string groupName, Trigger newTrigger) { ValidateState(); if (groupName == null) { groupName = SchedulerConstants.DefaultGroup; } newTrigger.Validate(); ICalendar cal = null; if (newTrigger.CalendarName != null) { cal = resources.JobStore.RetrieveCalendar(ctxt, newTrigger.CalendarName); } NullableDateTime ft = newTrigger.ComputeFirstFireTimeUtc(cal); if (!ft.HasValue) { throw new SchedulerException("Based on configured schedule, the given trigger will never fire.", SchedulerException.ErrorClientError); } if (resources.JobStore.ReplaceTrigger(ctxt, triggerName, groupName, newTrigger)) { NotifySchedulerThread(newTrigger.GetNextFireTimeUtc()); NotifySchedulerListenersUnscheduled(triggerName, groupName); NotifySchedulerListenersScheduled(newTrigger); } else { return null; } return ft; }
/// <summary> /// Schedule the given <see cref="Trigger" /> with the /// <see cref="IJob" /> identified by the <see cref="Trigger" />'s settings. /// </summary> public virtual DateTime ScheduleJob(SchedulingContext ctxt, Trigger trigger) { ValidateState(); if (trigger == null) { throw new SchedulerException("Trigger cannot be null", SchedulerException.ErrorClientError); } trigger.Validate(); ICalendar cal = null; if (trigger.CalendarName != null) { cal = resources.JobStore.RetrieveCalendar(ctxt, trigger.CalendarName); if (cal == null) { throw new SchedulerException(string.Format(CultureInfo.InvariantCulture, "Calendar not found: {0}", trigger.CalendarName), SchedulerException.ErrorPersistenceCalendarDoesNotExist); } } NullableDateTime ft = trigger.ComputeFirstFireTimeUtc(cal); if (!ft.HasValue) { throw new SchedulerException("Based on configured schedule, the given trigger will never fire.", SchedulerException.ErrorClientError); } resources.JobStore.StoreTrigger(ctxt, trigger, false); NotifySchedulerThread(trigger.GetNextFireTimeUtc()); NotifySchedulerListenersScheduled(trigger); return ft.Value; }
/// <summary> /// Add the <see cref="IJob" /> identified by the given /// <see cref="JobDetail" /> to the Scheduler, and /// associate the given <see cref="Trigger" /> with it. /// <p> /// If the given Trigger does not reference any <see cref="IJob" />, then it /// will be set to reference the Job passed with it into this method. /// </p> /// </summary> public virtual DateTime ScheduleJob(SchedulingContext ctxt, JobDetail jobDetail, Trigger trigger) { ValidateState(); if (jobDetail == null) { throw new SchedulerException("JobDetail cannot be null", SchedulerException.ErrorClientError); } if (trigger == null) { throw new SchedulerException("Trigger cannot be null", SchedulerException.ErrorClientError); } jobDetail.Validate(); if (trigger.JobName == null) { trigger.JobName = jobDetail.Name; trigger.JobGroup = jobDetail.Group; } else if (trigger.JobName != null && !trigger.JobName.Equals(jobDetail.Name)) { throw new SchedulerException("Trigger does not reference given job!", SchedulerException.ErrorClientError); } else if (trigger.JobGroup != null && !trigger.JobGroup.Equals(jobDetail.Group)) { throw new SchedulerException("Trigger does not reference given job!", SchedulerException.ErrorClientError); } trigger.Validate(); ICalendar cal = null; if (trigger.CalendarName != null) { cal = resources.JobStore.RetrieveCalendar(ctxt, trigger.CalendarName); if (cal == null) { throw new SchedulerException(string.Format(CultureInfo.InvariantCulture, "Calendar not found: {0}", trigger.CalendarName), SchedulerException.ErrorPersistenceCalendarDoesNotExist); } } NullableDateTime ft = trigger.ComputeFirstFireTimeUtc(cal); if (!ft.HasValue) { throw new SchedulerException("Based on configured schedule, the given trigger will never fire.", SchedulerException.ErrorClientError); } resources.JobStore.StoreJobAndTrigger(ctxt, jobDetail, trigger); NotifySchedulerThread(trigger.GetNextFireTimeUtc()); NotifySchedulerListenersScheduled(trigger); return ft.Value; }
//--------------------------------------------------------------------------- // triggers //--------------------------------------------------------------------------- /// <summary> /// Insert the base trigger data. /// </summary> /// <param name="conn">the DB Connection</param> /// <param name="trigger">the trigger to insert</param> /// <param name="state">the state that the trigger should be stored in</param> /// <param name="jobDetail">The job detail.</param> /// <returns>the number of rows inserted</returns> public virtual int InsertTrigger(ConnectionAndTransactionHolder conn, Trigger trigger, string state, JobDetail jobDetail) { byte[] baos = null; if (trigger.JobDataMap.Count > 0) { baos = SerializeJobData(trigger.JobDataMap); } using (IDbCommand cmd = PrepareCommand(conn, ReplaceTablePrefix(SqlInsertTrigger))) { AddCommandParameter(cmd, 1, "triggerName", trigger.Name); AddCommandParameter(cmd, 2, "triggerGroup", trigger.Group); AddCommandParameter(cmd, 3, "triggerJobName", trigger.JobName); AddCommandParameter(cmd, 4, "triggerJobGroup", trigger.JobGroup); AddCommandParameter(cmd, 5, "triggerVolatile", GetDbBooleanValue(trigger.Volatile)); AddCommandParameter(cmd, 6, "triggerDescription", trigger.Description); if (trigger.GetNextFireTimeUtc().HasValue) { AddCommandParameter(cmd, 7, "triggerNextFireTime", Convert.ToDecimal(trigger.GetNextFireTimeUtc().Value.Ticks)); } else { AddCommandParameter(cmd, 7, "triggerNextFireTime", null); } long prevFireTime = -1; if (trigger.GetPreviousFireTimeUtc().HasValue) { prevFireTime = trigger.GetPreviousFireTimeUtc().Value.Ticks; } AddCommandParameter(cmd, 8, "triggerPreviousFireTime", Convert.ToDecimal(prevFireTime)); AddCommandParameter(cmd, 9, "triggerState", state); string paramName = "triggerType"; if (trigger is SimpleTrigger && !trigger.HasAdditionalProperties) { AddCommandParameter(cmd, 10, paramName, TriggerTypeSimple); } else if (trigger is CronTrigger && !trigger.HasAdditionalProperties) { AddCommandParameter(cmd, 10, paramName, TriggerTypeCron); } else { // (trigger instanceof BlobTrigger or additional properties in sub-class AddCommandParameter(cmd, 10, paramName, TriggerTypeBlob); } AddCommandParameter(cmd, 11, "triggerStartTime", Convert.ToDecimal(trigger.StartTimeUtc.Ticks)); long endTime = 0; if (trigger.EndTimeUtc.HasValue) { endTime = trigger.EndTimeUtc.Value.Ticks; } AddCommandParameter(cmd, 12, "triggerEndTime", Convert.ToDecimal(endTime)); AddCommandParameter(cmd, 13, "triggerCalendarName", trigger.CalendarName); AddCommandParameter(cmd, 14, "triggerMisfireInstruction", trigger.MisfireInstruction); paramName = "triggerJobJobDataMap"; if (baos != null) { AddCommandParameter(cmd, 15, paramName, baos, dbProvider.Metadata.DbBinaryType); } else { AddCommandParameter(cmd, 15, paramName, null, dbProvider.Metadata.DbBinaryType); } AddCommandParameter(cmd, 16, "triggerPriority", trigger.Priority); int insertResult = cmd.ExecuteNonQuery(); if (insertResult > 0) { string[] trigListeners = trigger.TriggerListenerNames; for (int i = 0; trigListeners != null && i < trigListeners.Length; i++) { InsertTriggerListener(conn, trigger, trigListeners[i]); } } return insertResult; } }
/// <summary> /// Insert a fired trigger. /// </summary> /// <param name="conn">the DB Connection</param> /// <param name="trigger">the trigger</param> /// <param name="state">the state that the trigger should be stored in</param> /// <param name="job">The job.</param> /// <returns>the number of rows inserted</returns> public virtual int InsertFiredTrigger(ConnectionAndTransactionHolder conn, Trigger trigger, string state, JobDetail job) { using (IDbCommand cmd = PrepareCommand(conn, ReplaceTablePrefix(SqlInsertFiredTrigger))) { AddCommandParameter(cmd, 1, "triggerEntryId", trigger.FireInstanceId); AddCommandParameter(cmd, 2, "triggerName", trigger.Name); AddCommandParameter(cmd, 3, "triggerGroup", trigger.Group); AddCommandParameter(cmd, 4, "triggerVolatile", GetDbBooleanValue(trigger.Volatile)); AddCommandParameter(cmd, 5, "triggerInstanceName", instanceId); AddCommandParameter(cmd, 6, "triggerFireTime", Convert.ToDecimal(trigger.GetNextFireTimeUtc().Value.Ticks)); AddCommandParameter(cmd, 7, "triggerState", state); if (job != null) { AddCommandParameter(cmd, 8, "triggerJobName", trigger.JobName); AddCommandParameter(cmd, 9, "triggerJobGroup", trigger.JobGroup); AddCommandParameter(cmd, 10, "triggerJobStateful", GetDbBooleanValue(job.Stateful)); AddCommandParameter(cmd, 11, "triggerJobRequestsRecovery", GetDbBooleanValue(job.RequestsRecovery)); } else { AddCommandParameter(cmd, 8, "triggerJobName", null); AddCommandParameter(cmd, 9, "triggerJobGroup", null); AddCommandParameter(cmd, 10, "triggerJobStateful", GetDbBooleanValue(false)); AddCommandParameter(cmd, 11, "triggerJobRequestsRecovery", GetDbBooleanValue(false)); } AddCommandParameter(cmd, 12, "triggerPriority", trigger.Priority); return cmd.ExecuteNonQuery(); } }
/// <summary> /// Update the base trigger data. /// </summary> /// <param name="conn">The DB Connection.</param> /// <param name="trigger">The trigger to insert.</param> /// <param name="state">The state that the trigger should be stored in.</param> /// <param name="jobDetail">The job detail.</param> /// <returns>The number of rows updated.</returns> public virtual int UpdateTrigger(ConnectionAndTransactionHolder conn, Trigger trigger, string state, JobDetail jobDetail) { // save some clock cycles by unnecessarily writing job data blob ... bool updateJobData = trigger.JobDataMap.Dirty; byte[] baos = null; if (updateJobData && trigger.JobDataMap.Count > 0) { baos = SerializeJobData(trigger.JobDataMap); } IDbCommand cmd; int insertResult; if (updateJobData) { cmd = PrepareCommand(conn, ReplaceTablePrefix(SqlUpdateTrigger)); } else { cmd = PrepareCommand(conn, ReplaceTablePrefix(SqlUpdateTriggerSkipData)); } AddCommandParameter(cmd, 1, "triggerJobName", trigger.JobName); AddCommandParameter(cmd, 2, "triggerJobGroup", trigger.JobGroup); AddCommandParameter(cmd, 3, "triggerVolatile", GetDbBooleanValue(trigger.Volatile)); AddCommandParameter(cmd, 4, "triggerDescription", trigger.Description); long nextFireTime = -1; if (trigger.GetNextFireTimeUtc().HasValue) { nextFireTime = trigger.GetNextFireTimeUtc().Value.Ticks; } AddCommandParameter(cmd, 5, "triggerNextFireTime", Convert.ToDecimal(nextFireTime)); long prevFireTime = -1; if (trigger.GetPreviousFireTimeUtc().HasValue) { prevFireTime = trigger.GetPreviousFireTimeUtc().Value.Ticks; } AddCommandParameter(cmd, 6, "triggerPreviousFireTime", Convert.ToDecimal(prevFireTime)); AddCommandParameter(cmd, 7, "triggerState", state); string paramName = "triggerType"; if (trigger is SimpleTrigger && !trigger.HasAdditionalProperties) { // UpdateSimpleTrigger(conn, (SimpleTrigger)trigger); AddCommandParameter(cmd, 8, paramName, TriggerTypeSimple); } else if (trigger is CronTrigger && !trigger.HasAdditionalProperties) { // UpdateCronTrigger(conn, (CronTrigger)trigger); AddCommandParameter(cmd, 8, paramName, TriggerTypeCron); } else { // UpdateBlobTrigger(conn, trigger); AddCommandParameter(cmd, 8, paramName, TriggerTypeBlob); } AddCommandParameter(cmd, 9, "triggerStartTime", Convert.ToDecimal(trigger.StartTimeUtc.Ticks)); long endTime = 0; if (trigger.EndTimeUtc.HasValue) { endTime = trigger.EndTimeUtc.Value.Ticks; } AddCommandParameter(cmd, 10, "triggerEndTime", Convert.ToDecimal(endTime)); AddCommandParameter(cmd, 11, "triggerCalendarName", trigger.CalendarName); AddCommandParameter(cmd, 12, "triggerMisfireInstruction", trigger.MisfireInstruction); AddCommandParameter(cmd, 13, "triggerPriority", trigger.Priority); paramName = "triggerJobJobDataMap"; if (updateJobData) { if (baos != null) { AddCommandParameter(cmd, 14, paramName, baos, dbProvider.Metadata.DbBinaryType); } else { AddCommandParameter(cmd, 14, paramName, null, dbProvider.Metadata.DbBinaryType); } AddCommandParameter(cmd, 15, "triggerName", trigger.Name); AddCommandParameter(cmd, 16, "triggerGroup", trigger.Group); } else { AddCommandParameter(cmd, 14, "triggerName", trigger.Name); AddCommandParameter(cmd, 15, "triggerGroup", trigger.Group); } insertResult = cmd.ExecuteNonQuery(); if (insertResult > 0) { DeleteTriggerListeners(conn, trigger.Name, trigger.Group); String[] trigListeners = trigger.TriggerListenerNames; for (int i = 0; trigListeners != null && i < trigListeners.Length; i++) { InsertTriggerListener(conn, trigger, trigListeners[i]); } } return insertResult; }