private void StartJob(CancellationToken cancellationToken, KeyValuePair <string, Type> job)
        {
            _logger.LogInformation("Job {jobKey} is triggered", job.Key);
            var jobInstance = CreateJobInstance(job);

            if (jobInstance == null)
            {
                _logger.LogWarning("Cannot create service! Skipping job {jobKey}", job.Key);
                return;
            }

            _logger.LogTrace("Job {jobKey} is starting", job.Key);
            try
            {
                var jobTask = jobInstance.Start(cancellationToken);
                // We don't pass cancellation token to ContinueWith to allow record of result
                var jobRun = jobTask
                             // ReSharper disable once MethodSupportsCancellation
                             .ContinueWith(task => ResolveTaskEnd(job.Key, task));

                _runningJobs[job.Key] = jobRun;
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error starting job {jobKey}", job.Key);
                JobHistory.Add(new JobHistoryEntry(_clock.GetNow(), job.Key, JobResult.Failure,
                                                   "JobStartFailed: " + e.Message));
            }
        }
        private JobHistoryEntry AddJobHistoryEntry(JobResult jobResult, TimeSpan timeBeforeNow)
        {
            var executionTime   = DateTime.UtcNow.Subtract(timeBeforeNow);
            var jobHistoryEntry = new JobHistoryEntry(executionTime, JobKey, jobResult, "SomeString");

            _jobHistory.Add(jobHistoryEntry);
            return(jobHistoryEntry);
        }
Exemple #3
0
        // provide override for IsFull (Full at 1200 Calories)

        public override void Work(IWork item)
        {
            if (this.IsDone == false)
            {
                this.minutesWorked += item.Minutes;

                JobHistory.Add(item);
                Console.WriteLine($"Newb {item.Name}!");
                if (item is WasteTime)
                {
                    this.timeWasted += item.Minutes;
                }
                if (item.Name == "got coffee")
                {
                    DrankCoffee = true;
                }
                if (DrankCoffee == true)
                {
                    this.minutesWorked -= 10;
                }
                if (item.IsClerical == true)
                {
                    this.minutesWorked += 40;
                    // Console.WriteLine($"{item.Name} was clerical!");
                }
                if (item.IsTechnical == true)
                {
                    //   Console.WriteLine($"{item.Name} was technical!");
                    this.minutesWorked += 40;
                }
                if (OfficeSupply == 1)
                {
                    this.minutesWorked -= 10;
                }
                if (OfficeSupply == 2)
                {
                    this.timeWasted += 10;
                }
                if (OfficeSupply == 3)
                {
                    this.timeWasted    += 5;
                    this.minutesWorked -= 5;
                }
            }
            else
            {
                Console.WriteLine($"Cannot do {item.Name}, it's time to go home!!");
            }
        }
        /// <summary>
        /// Method which is executed when a job finishes execution.
        /// </summary>
        private async void ResolveTaskEnd(string jobKey, Task <object> task)
        {
            try
            {
                var result = await task;
                _logger.LogInformation("Job {jobKey} finished: {result}", jobKey, result);
                JobHistory.Add(new JobHistoryEntry(_clock.GetNow(), jobKey, JobResult.Success, result));
            }
            catch (OperationCanceledException e)
            {
                _logger.LogInformation(e, "Job {jobKey} was cancelled", jobKey);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error during execution of job {jobKey}", jobKey);
                JobHistory.Add(new JobHistoryEntry(_clock.GetNow(), jobKey, JobResult.Failure,
                                                   "JobFailed: " + e.Message));
            }
            finally
            {
                bool removed;
                do
                {
                    removed = _runningJobs.TryRemove(jobKey, out _);
                    if (!removed)
                    {
                        _logger.LogWarning("Unable to remove running job from ConcurrentDictionary: {jobKey} ... Retry...", jobKey);
                        await Task.Delay(TimeSpan.FromMilliseconds(50));
                    }
                } while (removed == false);

                if (task is IDisposable disposable)
                {
                    disposable.Dispose();
                }
            }
        }