Exemple #1
0
        private Task <TrackerJobResult> BuildAndExecuteJob(TrackerEnqueuedJob enqueuedJob)
        {
            var targetBuilderType = Type.GetType(enqueuedJob.JobBuilderTypeName);

            if (targetBuilderType == null)
            {
                throw new ApplicationException($"Could not find builder type {enqueuedJob.JobBuilderTypeName}.");
            }
            var jobBuilder = _serviceProvider.GetService(targetBuilderType);

            if (jobBuilder == null)
            {
                throw new ApplicationException($"Could not get instance of {targetBuilderType.ToString()} from DI.");
            }
            (jobBuilder as TrackerJobBuilder).Configure(enqueuedJob.OptionValues);
            var createdJob = (jobBuilder as TrackerJobBuilder).Build();

            _logger.LogInformation($"Starting job {createdJob.Name}.");
            return(Task.Run(async() =>
            {
                try
                {
                    // TODO check access rights when process is executed
                    // TODO check System.Threading.ThreadPool
                    return await createdJob.RunAsync();
                }
                catch (Exception ex)
                {
                    return new TrackerJobResult()
                    {
                        Success = false, StatusMessage = ex.Message, Name = createdJob.Name
                    };
                }
            }));
        }
Exemple #2
0
        public async Task <TrackerEnqueuedJob> EnqueueJobAsync(Type trackerJobBuilderType, IEnumerable <KeyValuePair <string, object> > optionValues, TimeSpan runAfterTime, uint jobPrioritySmallerFirst)
        {
            var jobQueue = await GetJobQueueAsync();

            var result = new TrackerEnqueuedJob()
            {
                OptionValues = new Dictionary <string, object>(optionValues), RunAfterDate = DateTime.Now.Add(runAfterTime), JobBuilderTypeName = trackerJobBuilderType.ToString(), Priority = jobPrioritySmallerFirst
            };

            jobQueue.Enqueue(result);
            _logger.LogDebug($"{result.CreatedDate} Added job to queue (priority {result.Priority}), scheduled for {result.RunAfterDate}.");
            return(result);
        }