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);
        }
        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;
        }