private static List <string> GetDirectoriesToScan(SchedulerContext schedCtxt, JobDataMap mergedJobDataMap)
        {
            IDirectoryProvider directoryProvider = new DefaultDirectoryProvider();
            var explicitDirProviderName          = mergedJobDataMap.GetString(DirectoryScanJob.DirectoryProviderName);

            if (explicitDirProviderName != null)
            {
                schedCtxt.TryGetValue(explicitDirProviderName, out var temp);
                IDirectoryProvider explicitProvider = (IDirectoryProvider)temp;
                directoryProvider = explicitProvider ?? throw new JobExecutionException("IDirectoryProvider named '" +
                                                                                        explicitDirProviderName + "' not found in SchedulerContext");
            }

            return(directoryProvider.GetDirectoriesToScan(mergedJobDataMap).ToList());
        }
        private static IDirectoryScanListener GetListener(JobDataMap mergedJobDataMap, SchedulerContext schedCtxt)
        {
            var listenerName = mergedJobDataMap.GetString(DirectoryScanJob.DirectoryScanListenerName);

            if (listenerName == null)
            {
                throw new JobExecutionException("Required parameter '" +
                                                DirectoryScanJob.DirectoryScanListenerName + "' not found in merged JobDataMap");
            }

            schedCtxt.TryGetValue(listenerName, out var temp);
            IDirectoryScanListener listener = (IDirectoryScanListener)temp;

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

            return(listener);
        }
示例#3
0
        /// <summary>
        /// This is the main entry point for job execution. The scheduler will call this method on the
        /// job once it is triggered.
        /// </summary>
        /// <param name="context">The <see cref="IJobExecutionContext"/> that
        /// the job will use during execution.</param>
        public void Execute(IJobExecutionContext context)
        {
            JobDataMap       mergedJobDataMap = context.MergedJobDataMap;
            SchedulerContext schedCtxt        = null;

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

            string dirName      = mergedJobDataMap.GetString(DIRECTORY_NAME);
            string listenerName = mergedJobDataMap.GetString(DIRECTORY_SCAN_LISTENER_NAME);

            if (dirName == null)
            {
                throw new JobExecutionException("Required parameter '" +
                                                DIRECTORY_NAME + "' not found in merged JobDataMap");
            }
            if (listenerName == null)
            {
                throw new JobExecutionException("Required parameter '" +
                                                DIRECTORY_SCAN_LISTENER_NAME + "' not found in merged JobDataMap");
            }

            object temp;

            schedCtxt.TryGetValue(listenerName, out temp);
            IDirectoryScanListener listener = (IDirectoryScanListener)temp;

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

            DateTime lastDate = DateTime.MinValue;

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

            long minAge = 5000;

            if (mergedJobDataMap.ContainsKey(MINIMUM_UPDATE_AGE))
            {
                minAge = mergedJobDataMap.GetLong(MINIMUM_UPDATE_AGE);
            }
            DateTime maxAgeDate = DateTime.Now.AddMilliseconds(minAge);

            FileInfo[] updatedFiles = GetUpdatedOrNewFiles(dirName, lastDate, maxAgeDate);

            if (updatedFiles == null)
            {
                return;
            }

            DateTime latestMod = DateTime.MinValue;

            foreach (FileInfo updFile in updatedFiles)
            {
                DateTime lm = updFile.LastWriteTime;
                latestMod = (lm > latestMod) ? lm : latestMod;
            }

            if (updatedFiles.Length > 0)
            {
                // notify call back...
                listener.FilesUpdatedOrAdded(updatedFiles);
            }

            // It is the JobDataMap on the JobDetail which is actually stateful
            context.JobDetail.JobDataMap.Put(LAST_MODIFIED_TIME, latestMod);
        }