public IEnumerable <JobDescriptionViewModel> GetList() { var plugIns = PlugInLocator.Search(new ScheduledPlugInAttribute()).ToList(); // get actual activated jobs (ran at least once) var actual = from job in _repo.List() let type = Type.GetType($"{job.TypeName}, {job.AssemblyName}") select new JobDescriptionViewModel { Id = -1, InstanceId = job.ID, Name = job.Name, Exists = false, TypeName = job.TypeName, AssemblyName = job.AssemblyName }; // get all scheduled jobs (even inactive) var plugins = (from plugin in plugIns let job = _repo.List().FirstOrDefault(j => j.TypeName == plugin.TypeName && j.AssemblyName == plugin.AssemblyName) let attr = plugin.GetAttribute <ScheduledPlugInAttribute>() let lastLog = _logRepo.GetAsync(job.ID, 0, 1).GetAwaiter().GetResult().PagedResult.FirstOrDefault() select new JobDescriptionViewModel { Id = plugin.ID, InstanceId = job != null ? job.ID : Guid.Empty, Name = attr.DisplayName, Description = attr.Description, IsEnabled = job != null && job.IsEnabled, Interval = job != null ? $"{job.IntervalLength} ({job.IntervalType})" : "", IsLastExecuteSuccessful = job != null && !job.HasLastExecutionFailed ? true : (bool?)null, LastExecute = job != null ? (job.LastExecution != DateTime.MinValue ? job.LastExecution : (DateTime?)null) : null, LastMessage = job.LastExecutionMessage, LastDuration = lastLog != null ? (lastLog.Duration.HasValue ? $"{lastLog.Duration.Value.Milliseconds}ms" : string.Empty) : string.Empty, AssemblyName = plugin.AssemblyName, TypeName = plugin.TypeName, IsRunning = job != null && job.IsRunning, IsRestartable = job.Restartable }).OrderBy(j => j.Name).ToList(); // return distinct list return(plugins.Concat(actual) .GroupBy(j => j.TypeName) .Select(g => g.First()) .ToList()); }
private List <ScheduledJob> GetGhostJobs(IScheduledJobRepository scheduledJobRepository) { var scheduledJobs = PlugInLocator.Search(new ScheduledPlugInAttribute()); var scheduledJobDescriptors = PlugInDescriptor.GetAttributeArray( scheduledJobs, typeof(ScheduledPlugInAttribute)); var ghostJobs = (from job in scheduledJobRepository.List() let attribute = scheduledJobDescriptors.FirstOrDefault(x => string.Equals(x.PlugInType.FullName, job.TypeName, StringComparison.InvariantCultureIgnoreCase)) select new ScheduledJob { Id = job.ID, FromCode = attribute != null, Name = job.Name, AssemblyName = job.AssemblyName }) .Where(x => !x.FromCode) .ToList(); return(ghostJobs); }