示例#1
0
        public async Task ExecuteJobAsync(ICronJob cronJob)
        {
            _logger.LogInformation($"Executing {cronJob}");
            await cronJob.ExecuteAsync(_token);

            _logger.LogInformation($"Finished {cronJob}");
        }
示例#2
0
 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);
 }
示例#3
0
        /// <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));
        }
示例#4
0
 /// <summary>
 /// 移除任务
 /// </summary>
 /// <param name="job">被移除任务</param>
 /// <returns></returns>
 public bool RemoveJob(ICronJob job)
 {
     lock (JobLock)
     {
         job.abort();
         return(_cronJobs.Remove(job));
     }
 }
示例#5
0
 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);
            }
        }
示例#7
0
        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);
                }
            }
        }
示例#8
0
        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);
                }
            }
        }
示例#9
0
 public ILog GetLogForJob(ICronJob job)
 {
     var nlogger = LogManager.GetLogger(job.GetType().FullName);
     return new LogAdapter(nlogger);
 }
示例#10
0
        public ILog GetLogForJob(ICronJob job)
        {
            var nlogger = LogManager.GetLogger(job.GetType().FullName);

            return(new LogAdapter(nlogger));
        }
示例#11
0
        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);
 }
示例#13
0
 /// <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;
 }
示例#14
0
 public void Add(ICronJob job)
 {
     job.JobExecuted  += Job_JobExecuted;
     job.JobExecuting += Job_JobExecuting;
     _cronJobs.Add(job);
 }
示例#15
0
 public void Remove(ICronJob job)
 {
     _cronJobs.Remove(job);
 }
示例#16
0
 public void AddJob(ICronJob Job)
 {
     CronJobs.Add(Job);
 }
示例#17
0
        public NCron.Logging.ILog GetLogForJob(ICronJob job)
        {
            var internalLogger = LogManager.GetLogger(job.GetType());

            return(new LogAdapter(internalLogger));
        }
示例#18
0
 public void AddJob(ICronJob job)
 {
     cron_jobs.Add (job);
 }
示例#19
0
 public JobEventArgs(ICronJob job)
 {
     Job = job;
 }
示例#20
0
 /// <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;
 }
示例#21
0
        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);
            }
        }