Beispiel #1
0
        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" />
        /// fires that is associated with the <see cref="IJob" />.
        /// <para>
        /// The implementation may wish to set a  result object on the
        /// JobExecutionContext before this method exits.  The result itself
        /// is meaningless to Quartz, but may be informative to
        /// <see cref="IJobListener" />s or
        /// <see cref="ITriggerListener" />s that are watching the job's
        /// execution.
        /// </para>
        /// </summary>
        /// <param name="context">The execution context.</param>
        /// <seealso cref="IJob">
        /// </seealso>
        public virtual void Execute(IJobExecutionContext context)
        {
            JobDataMap       mergedJobDataMap = context.MergedJobDataMap;
            SchedulerContext schedCtxt;

            try
            {
                schedCtxt = context.Scheduler.Context;
            }
            catch (SchedulerException e)
            {
                throw new JobExecutionException("Error obtaining scheduler context.", e, false);
            }

            string fileName     = mergedJobDataMap.GetString(FileName);
            string listenerName = mergedJobDataMap.GetString(FileScanListenerName);

            if (fileName == null)
            {
                throw new JobExecutionException(string.Format(CultureInfo.InvariantCulture, "Required parameter '{0}' not found in JobDataMap", FileName));
            }
            if (listenerName == null)
            {
                throw new JobExecutionException(string.Format(CultureInfo.InvariantCulture, "Required parameter '{0}' not found in JobDataMap", FileScanListenerName));
            }

            IFileScanListener listener = (IFileScanListener)schedCtxt[listenerName];

            if (listener == null)
            {
                throw new JobExecutionException(string.Format(CultureInfo.InvariantCulture, "FileScanListener named '{0}' not found in SchedulerContext", listenerName));
            }

            DateTime lastDate = DateTime.MinValue;

            if (mergedJobDataMap.ContainsKey(LastModifiedTime))
            {
                lastDate = mergedJobDataMap.GetDateTime(LastModifiedTime);
            }

            long minAge = 5000;

            if (mergedJobDataMap.ContainsKey(MinimumUpdateAge))
            {
                minAge = mergedJobDataMap.GetLong(MinimumUpdateAge);
            }

            DateTime maxAgeDate = DateTime.Now.AddMilliseconds(minAge);

            DateTime newDate = GetLastModifiedDate(fileName);

            if (newDate == DateTime.MinValue)
            {
                return;
            }

            if (lastDate != DateTime.MinValue && (newDate != lastDate && newDate < maxAgeDate))
            {
                // notify call back...
                listener.FileUpdated(fileName);
            }

            context.JobDetail.JobDataMap.Put(LastModifiedTime, newDate);
        }
Beispiel #2
0
        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" />
        /// fires that is associated with the <see cref="IJob" />.
        /// <para>
        /// The implementation may wish to set a  result object on the
        /// JobExecutionContext before this method exits.  The result itself
        /// is meaningless to Quartz, but may be informative to
        /// <see cref="IJobListener" />s or
        /// <see cref="ITriggerListener" />s that are watching the job's
        /// execution.
        /// </para>
        /// </summary>
        /// <param name="context">The execution context.</param>
        /// <seealso cref="IJob">
        /// </seealso>
        public virtual async Task Execute(IJobExecutionContext context)
        {
            JobDataMap       mergedJobDataMap = context.MergedJobDataMap;
            SchedulerContext schedCtxt;

            try
            {
                schedCtxt = context.Scheduler.Context;
            }
            catch (SchedulerException e)
            {
                throw new JobExecutionException("Error obtaining scheduler context.", e, false);
            }

            string fileName     = mergedJobDataMap.GetString(FileName);
            string listenerName = mergedJobDataMap.GetString(FileScanListenerName);

            if (fileName == null)
            {
                throw new JobExecutionException($"Required parameter '{FileName}' not found in JobDataMap");
            }
            if (listenerName == null)
            {
                throw new JobExecutionException($"Required parameter '{FileScanListenerName}' not found in JobDataMap");
            }

            IFileScanListener listener = (IFileScanListener)schedCtxt[listenerName];

            if (listener == null)
            {
                throw new JobExecutionException($"FileScanListener named '{listenerName}' not found in SchedulerContext");
            }

            DateTime lastDate = DateTime.MinValue;

            if (mergedJobDataMap.ContainsKey(LastModifiedTime))
            {
                lastDate = mergedJobDataMap.GetDateTime(LastModifiedTime);
            }

            long minAge = 5000;

            if (mergedJobDataMap.ContainsKey(MinimumUpdateAge))
            {
                minAge = mergedJobDataMap.GetLong(MinimumUpdateAge);
            }

            DateTime maxAgeDate = DateTime.Now.AddMilliseconds(minAge);

            DateTime newDate = GetLastModifiedDate(fileName);

            if (newDate == DateTime.MinValue)
            {
                Log.Warn($"File '{fileName}' does not exist.");
                return;
            }

            if (lastDate != DateTime.MinValue && (newDate != lastDate && newDate < maxAgeDate))
            {
                // notify call back...
                Log.Info($"File '{fileName}' updated, notifying listener.");
                await listener.FileUpdated(fileName).ConfigureAwait(false);
            }
            else
            {
                Log.Debug($"File '{fileName}' unchanged.");
            }

            context.JobDetail.JobDataMap.Put(LastModifiedTime, newDate);
        }
        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a <see cref="Trigger" />
        /// fires that is associated with the <see cref="IJob" />.
        /// <p>
        /// The implementation may wish to set a  result object on the
        /// JobExecutionContext before this method exits.  The result itself
        /// is meaningless to Quartz, but may be informative to
        /// <see cref="IJobListener" />s or
        /// <see cref="ITriggerListener" />s that are watching the job's
        /// execution.
        /// </p>
        /// </summary>
        /// <param name="context">The execution context.</param>
        /// <seealso cref="IJob">
        /// </seealso>
        public virtual void Execute(JobExecutionContext context)
        {
            JobDataMap       data = context.MergedJobDataMap;
            SchedulerContext schedCtxt;

            try
            {
                schedCtxt = context.Scheduler.Context;
            }
            catch (SchedulerException e)
            {
                throw new JobExecutionException("Error obtaining scheduler context.", e, false);
            }

            string fileName     = data.GetString(FileName);
            string listenerName = data.GetString(FileScanListenerName);

            if (fileName == null)
            {
                throw new JobExecutionException(string.Format(CultureInfo.InvariantCulture, "Required parameter '{0}' not found in JobDataMap", FileName));
            }
            if (listenerName == null)
            {
                throw new JobExecutionException(string.Format(CultureInfo.InvariantCulture, "Required parameter '{0}' not found in JobDataMap", FileScanListenerName));
            }

            IFileScanListener listener = (IFileScanListener)schedCtxt[listenerName];

            if (listener == null)
            {
                throw new JobExecutionException(string.Format(CultureInfo.InvariantCulture, "FileScanListener named '{0}' not found in SchedulerContext", listenerName));
            }

            DateTime lastDate = DateTime.MinValue;

            if (data.Contains(LastModifiedTime))
            {
                lastDate = data.GetDateTime(LastModifiedTime);
            }

            DateTime newDate = GetLastModifiedDate(fileName);

            if (newDate == DateTime.MinValue)
            {
                Log.Warn(string.Format(CultureInfo.InvariantCulture, "File '{0}' does not exist.", fileName));
                return;
            }

            if (lastDate != DateTime.MinValue && (newDate != lastDate))
            {
                // notify call back...
                Log.Info(string.Format(CultureInfo.InvariantCulture, "File '{0}' updated, notifying listener.", fileName));
                listener.FileUpdated(fileName);
            }
            else
            {
                Log.Debug(string.Format(CultureInfo.InvariantCulture, "File '{0}' unchanged.", fileName));
            }

            context.JobDetail.JobDataMap.Put(LastModifiedTime, newDate);
        }