public void GetScheduledJobsSucceeds()
        {
            // Arrange
            var endpoints = Mock.Create<AppclusiveEndpoints>();
            var sut = new ScheduledJobsManager(endpoints);

            // Act
            var result = sut.GetJobs();
            
            // Assert
            Assert.IsTrue(0 <= result.Count);
        }
Esempio n. 2
0
 public void Include(ScheduledJobsManager vm)
 {
     var vm1 = new ScheduledJobsManager();
 }
        // The state object is necessary for a TimerCallback.
        public void RunJobs(object stateObject)
        {
            Contract.Assert(isInitialised);

            var fn = Method.fn();

            // we set "Now" to the current minute + 1 ms (as we have some strange behaviour when scheduling exactly on the minute)
            var now = DateTimeOffset.Now;
            now = new DateTimeOffset(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, 1, now.Offset);

            if (!IsActive)
            {
                Trace.WriteLine("{0}: IsActive: {1}. Nothing to do.", fn, IsActive);
                
                goto Success;
            }

            // copy jobs into local instance specific list
            List<ScheduledJob> scheduledJobs;
            lock(_lock)
            {
                scheduledJobs = _scheduledJobs.ToList();
            }

            if (0 >= scheduledJobs.Count)
            {
                Trace.WriteLine("{0}: No scheduled jobs found. Nothing to do.", fn, "");
                    
                goto Success;
            }

            var defaultPlugin = configuration.Plugins.FirstOrDefault
            (
                p => p.Metadata.Type.Equals(Public.Constants.PLUGIN_TYPE_DEFAULT)
            );
                
            Trace.WriteLine("{0}: Processing {1} ScheduledJobs ...", fn, scheduledJobs.Count);
            foreach (var job in scheduledJobs)
            {
                try
                {
                    var jobScheduler = new ScheduledJobScheduler(job);
                    if (!jobScheduler.IsScheduledToRun(now))
                    {
                        continue;
                    }

                    // get plugin for ScheduledJob
                    var plugin = configuration.Plugins.FirstOrDefault(p => p.Metadata.Type.Equals(job.Action));
                    if(null == plugin)
                    {
                        Trace.WriteLine("No plugin for Job.Id '{0}' with Job.Action '{1}' found. Using 'Default' plugin ...", 
                            job.Id, job.Action);
                        plugin = defaultPlugin;
                    }
                    if(null == plugin)
                    {
                        Trace.WriteLine(
                            "No plugin for Job.Id '{0}' with Job.Action '{1}' found and no '{2}' plugin found either. Skipping.", 
                            job.Id, job.Action, biz.dfch.CS.Appclusive.Scheduler.Public.Constants.PLUGIN_TYPE_DEFAULT);

                        continue;
                    }

                    // invoke plugin
                    var invocationResult = new NonSerialisableJobResult();
                        
                    var scheduledJobsManager = new ScheduledJobsManager(endpoints);
                    var parameters = scheduledJobsManager.ConvertJobParameters(job.Action, job.ScheduledJobParameters);
                    parameters.Add("JobId", job.Id);

                    if(default(Guid) == System.Diagnostics.Trace.CorrelationManager.ActivityId)
                    {
                        System.Diagnostics.Trace.CorrelationManager.ActivityId = Guid.NewGuid();
                    }

                    configuration.Logger.WriteLine("Invoking {0} with Job.Action '{1}' [ActivityId {2}] ...", job.Id, job.Action, System.Diagnostics.Trace.CorrelationManager.ActivityId);
                    plugin.Value.Invoke(parameters, invocationResult);

                    if(invocationResult.Succeeded)
                    {
                        configuration.Logger.Info("Invoking {0} with Job.Action '{1}' [ActivityId {2}] SUCCEEDED.", job.Id, job.Action, System.Diagnostics.Trace.CorrelationManager.ActivityId);
                    }
                    else
                    {
                        configuration.Logger.Error("Invoking {0} with Job.Action '{1}' [ActivityId {2}] FAILED.", job.Id, job.Action, System.Diagnostics.Trace.CorrelationManager.ActivityId);
                    }
                }
                catch (Exception ex)
                {
                    var message = string.Format("Invoking {0} with Job.Action '{1}' [ActivityId {2}] FAILED.", job.Id, job.Action, System.Diagnostics.Trace.CorrelationManager.ActivityId);
                    Trace.WriteException(message, ex);
                }
            }
            Trace.WriteLine("{0}: Processing {1} ScheduledJobs COMPLETED.", fn, scheduledJobs.Count);
Success:
            if (configuration.UpdateIntervalInMinutes <= (now - lastUpdated).TotalMinutes)
            {
                var result = GetScheduledJobs();
                Contract.Assert(result);
            }

            return;
        }
        public bool GetScheduledJobs()
        {
            var result = false;

            if (isInitialised && !IsActive)
            {
                configuration.Logger.Warn("Scheduler is not initialised [{0}] or not active [{1}]. Skip loading of ScheduledJobs.", isInitialised, IsActive);
                goto Success;
            }
            
            // load ScheduledJob entities
            try
            {
                // connect to Appclusive server
                var baseUri = new Uri(string.Format("{0}api", configuration.Uri.AbsoluteUri));
                endpoints = new AppclusiveEndpoints(baseUri, configuration.Credential);

                configuration.Logger.Info("Loading ScheduledJobs from '{0}' ...", endpoints.Core.BaseUri.AbsoluteUri);
                var scheduledJobsManager = new ScheduledJobsManager(endpoints);

                var allScheduledJobs = scheduledJobsManager.GetJobs();
                var validScheduledJobs = scheduledJobsManager.GetValidJobs(allScheduledJobs);
                Contract.Assert(SCHEDULED_JOBS_WORKER_JOBS_PER_INSTANCE_MAX >= validScheduledJobs.Count);

                lock (_lock)
                {
                    foreach (var job in validScheduledJobs)
                    {
                        var existingJob = _scheduledJobs.FirstOrDefault(e => e.Id == job.Id);
                        if (null == existingJob)
                        {
                            Trace.WriteLine("Added  : Id {0} ('{1}'): '{2}'.", job.Id, job.Crontab, job.Modified.ToString("yyyy-MM-dd HH:mm:sszzz"));
                            continue;
                        }
                        
                        if (!existingJob.Crontab.Equals(job.Crontab))
                        {
                            Trace.WriteLine("Updated: Id {0} ('{1}'): '{2}'.", job.Id, job.Crontab, job.Modified.ToString("yyyy-MM-dd HH:mm:sszzz"));
                        }
                    }

                    _scheduledJobs = validScheduledJobs.ToList();
                }
            
                configuration.Logger.Info("Loading ScheduledJobs from '{0}' SUCCEEDED. [{1}]", endpoints.Core.BaseUri.AbsoluteUri, allScheduledJobs.Count);

                result = true;
            }
            catch(InvalidOperationException ex)
            {
                var message = string.Format("Loading ScheduledJobs from '{0}' FAILED with InvalidOperationException. Check the specified credentials.", endpoints.Core.BaseUri.AbsoluteUri);
                Trace.WriteException(message, ex);

                result = false;
            }
            catch (Exception ex)
            {
                Trace.WriteException(ex.Message, ex);
                
                throw;
            }

Success:
            lastUpdated = DateTimeOffset.Now;
            return result;
        }