public async Task ExecuteJobAsync(ICronJob cronJob) { _logger.LogInformation($"Executing {cronJob}"); await cronJob.ExecuteAsync(_token); _logger.LogInformation($"Finished {cronJob}"); }
public JobHandler(Guid jobId, ICronJob cronJob, string cronExpression, IServiceScopeFactory serviceScopeFactory) { JobId = jobId; _cronJob = cronJob; _serviceScopeFactory = serviceScopeFactory; _cronParser = new CronParser(new CronDateTimeService()); _cronExpression = _cronParser.Parse(cronExpression); }
/// <summary> /// Gets an <see cref="EventLogAdapter"/> instance, writing to the "Application" event log with a source name of "NCron". /// </summary> /// <param name="job">The job which the log will be used with. This parameter is ignored.</param> /// <returns>An <see cref="EventLogAdapter"/>, writing to the "Application" event log with a source name of "NCron".</returns> public ILog GetLogForJob(ICronJob job) { var eventLog = new EventLog { Source = Bootstrap.ApplicationName }; return(new EventLogAdapter(eventLog)); }
/// <summary> /// 移除任务 /// </summary> /// <param name="job">被移除任务</param> /// <returns></returns> public bool RemoveJob(ICronJob job) { lock (JobLock) { job.abort(); return(_cronJobs.Remove(job)); } }
private static bool GetJob(string className, out ICronJob job) { try { Type classType = Type.GetType(className, true); job = (ICronJob)Activator.CreateInstance(classType); return(true); } catch { Console.WriteLine("Invalid job name."); PrintUsage(); job = null; return(false); } }
public void AddCronJob(ICronJob cronJob) { if (cronJob == null) { throw new ArgumentNullException("cronJob"); } if (cronJob.Enabled) { _recurringJobManager.AddOrUpdate(cronJob.Name, () => cronJob.Execute(JobCancellationToken.Null), cronJob.Schedule); } else { _recurringJobManager.RemoveIfExists(cronJob.Name); } }
private async Task RunCronJob <T>() where T : ICronJob { using (var serviceScope = _serviceScopeFactory.CreateScope()) { _cronJob = serviceScope.ServiceProvider.GetService <T>(); while (!_cancellationTokenSource.Token.IsCancellationRequested) { var timeToNext = _cronExpression.GetTimeToNext(); if (!timeToNext.HasValue) { return; } await Task.Delay((int)timeToNext.Value.TotalMilliseconds, _cancellationTokenSource.Token); await _cronJob.RunJob(_cancellationTokenSource.Token); } } }
private void ExecuteJob(ICronJob job) { using (var log = _logFactory.GetLogForJob(job)) { var context = new CronContext(job, log); log.Info(() => String.Format("Executing job: {0}", job)); // This inner try-catch serves to report ICronJob failures to the ILog. // Such exceptions are expected, and are thus handled seperately. try { job.Initialize(context); job.Execute(); } catch (Exception exception) { log.Error(() => String.Format("The job \"{0}\" threw an unhandled exception.", job), () => exception); } } }
public ILog GetLogForJob(ICronJob job) { var nlogger = LogManager.GetLogger(job.GetType().FullName); return new LogAdapter(nlogger); }
public ILog GetLogForJob(ICronJob job) { var nlogger = LogManager.GetLogger(job.GetType().FullName); return(new LogAdapter(nlogger)); }
private void ExecuteJob(ICronJob job) { using (var log = _logFactory.GetLogForJob(job)) { var context = new CronContext(job, log); log.Info(() => String.Format("Executing job: {0}", job)); // This inner try-catch serves to report ICronJob failures to the ILog. // Such exceptions are expected, and are thus handled seperately. try { job.Initialize(context); InvokeJobStarted(new JobEventArgs(job)); job.Execute(); InvokeJobFinished(new JobEventArgs(job)); } catch (Exception exception) { log.Error(() => String.Format("The job \"{0}\" threw an unhandled exception.", job), () => exception); } } }
/// <summary> /// Gets an <see cref="EventLogAdapter"/> instance, writing to the "Application" event log with a source name of "NCron". /// </summary> /// <param name="job">The job which the log will be used with. This parameter is ignored.</param> /// <returns>An <see cref="EventLogAdapter"/>, writing to the "Application" event log with a source name of "NCron".</returns> public ILog GetLogForJob(ICronJob job) { var eventLog = new EventLog { Source = ApplicationInfo.ApplicationName }; return new EventLogAdapter(eventLog); }
/// <summary> /// Creates a new <see cref="CronContext"/> with all properties explicitly set. /// </summary> /// <param name="job">The job being executed.</param> /// <param name="log">Tog to be used while executing the job in this context.</param> public CronContext(ICronJob job, ILog log) { Job = job; Log = log; }
public void Add(ICronJob job) { job.JobExecuted += Job_JobExecuted; job.JobExecuting += Job_JobExecuting; _cronJobs.Add(job); }
public void Remove(ICronJob job) { _cronJobs.Remove(job); }
public void AddJob(ICronJob Job) { CronJobs.Add(Job); }
public NCron.Logging.ILog GetLogForJob(ICronJob job) { var internalLogger = LogManager.GetLogger(job.GetType()); return(new LogAdapter(internalLogger)); }
public void AddJob(ICronJob job) { cron_jobs.Add (job); }
public JobEventArgs(ICronJob job) { Job = job; }
protected Thread ExecuteTask(ICronJob cronjob, object parameter) { if (cronjob == null) { return(null); } try { switch (cronjob.CheckStatus((ISchemaInfo)parameter)) { case EComponentStatus.DISABLED: { if (LogLevel == ELogLevel.ALL) { MyLogger.LogText(string.Format("{0} DISABLED", cronjob.ID), "ServiceBase::ExecuteTask"); } return(null); } case EComponentStatus.WORKING: { if (LogLevel == ELogLevel.ALL || LogLevel == ELogLevel.WARN) { MyLogger.LogText(string.Format("{0} STILL BUSY", cronjob.ID), "ServiceBase::ExecuteTask"); } return(null); } case EComponentStatus.UNAVAILABLE: { if (LogLevel == ELogLevel.ALL) { MyLogger.LogText(string.Format("{0} OUT OF DATE ", cronjob.ID), "ServiceBase::ExecuteTask"); } return(null); } case EComponentStatus.READY: { if (LogLevel == ELogLevel.ALL || LogLevel == ELogLevel.WARN) { MyLogger.LogText(string.Format("{0} STARTED ", cronjob.ID), "ServiceBase::ExecuteTask"); } #if DEBUG cronjob.Run(parameter); return(null); #else Thread taskthread = new Thread(new ParameterizedThreadStart(cronjob.Run)); taskthread.Start(parameter); return(taskthread); #endif } } return(null); } catch (Exception ex) { MyLogger.LogException(ex, "ServiceBase::ExecuteTask"); return(null); } }