private void DoExecute(JobExecutionRequest jobExecutionRequest)
        {
            _topLevelJob.Value = jobExecutionRequest;

            Log($"Executing {jobExecutionRequest.JobSummary()}");
            Interlocked.Increment(ref _outstandingCount);
            var task = jobExecutionRequest.ExecuteJob();

            WithLock(() => _outstandingTasks.Add(task),
                     () => Log($"Failed to add job {jobExecutionRequest.JobSummary()} to running list"));
            try
            {
                task.Wait();
            }
            finally
            {
                Log($"Finished executing {jobExecutionRequest.JobSummary()}");
                WithLock(lockObject =>
                {
                    _outstandingTasks.Remove(task);
                    Interlocked.Decrement(ref _outstandingCount);
                    Monitor.Pulse(lockObject);
                },
                         () => Log($"Failed to remove job {jobExecutionRequest.JobSummary()} from running list"));
                if (ReferenceEquals(_topLevelJob.Value, jobExecutionRequest))
                {
                    _topLevelJob.Value = null;
                }
            }
        }
Esempio n. 2
0
        public Task <TResult> SubmitJob <TResult>(Job <TResult> job)
        {
            var jobExecutionRequest = new JobExecutionRequest <TResult>(job, Environment.StackTrace);

            var executionPolicy    = _policies.FirstOrDefault(x => x.AppliesTo(job)) ?? _defaultPolicy;
            var applicableExecutor = executionPolicy.EngineFor(job);

            applicableExecutor.ExecuteJob(jobExecutionRequest);

            return(jobExecutionRequest.ResultTask);
        }
        public bool ExecuteJob(JobExecutionRequest jobExecutionRequest)
        {
            if (!_running)
            {
                Log($"Not enqueing {jobExecutionRequest.JobSummary()} as executionEngine is stopped");
                return(false);
            }

            ExecuteRequest(jobExecutionRequest);
            return(true);
        }
 private void ExecuteRequest(JobExecutionRequest jobExecutionRequest)
 {
     Log($"Executing {jobExecutionRequest.JobSummary()}");
     try
     {
         var executionTask = jobExecutionRequest.ExecuteJob();
         executionTask.Wait();
     }
     catch (Exception e)
     {
         Log($"Job execution {jobExecutionRequest.JobSummary()} failed - {e.Message}");
     }
 }
 private void ExecuteRequest(JobExecutionRequest jobExecutionRequest)
 {
     if (_topLevelJob.Value != null)
     {
         Log($"Executing {jobExecutionRequest.JobSummary()} directly as called within context of existing job");
         jobExecutionRequest.ExecuteJob().Wait();
     }
     else
     {
         Log($"Enqueing {jobExecutionRequest.JobSummary()}");
         _jobTaskFactory.StartNew(() => DoExecute(jobExecutionRequest));
     }
 }
        public bool ExecuteJob(JobExecutionRequest jobExecutionRequest)
        {
            if (_cancellationTokenSource.IsCancellationRequested)
            {
                Log($"Not enqueing {jobExecutionRequest.JobSummary()} as executionEngine is stopped");
                jobExecutionRequest.Cancel();
                return(false);
            }

            WithLock(lockObject =>
            {
                Log($"Enqueuing job {jobExecutionRequest.JobSummary()}");
                _executionQueue.Enqueue(jobExecutionRequest);
                Monitor.Pulse(lockObject);
            },
                     () => throw new Exception(
                         $"Failed to enqueue job {jobExecutionRequest.JobSummary()} as lock could not be obtained in time"));
            return(true);
        }
        private void ExecutionLoop()
        {
            Log("Starting");

            _stopped.Reset();
            var cancellationToken = _cancellationTokenSource.Token;

            var continueToProcessWork = true;

            while (continueToProcessWork)
            {
                JobExecutionRequest jobExecutionRequest = null;
                WithLock(lockObject =>
                {
                    if (_executionQueue.Count > 0)
                    {
                        jobExecutionRequest = _executionQueue.Dequeue();
                    }
                    else
                    {
                        if (!cancellationToken.IsCancellationRequested)
                        {
                            Monitor.Wait(lockObject);
                        }
                        else
                        {
                            continueToProcessWork = false;
                        }
                    }
                },
                         () => { });

                if (jobExecutionRequest != null)
                {
                    ExecuteRequest(jobExecutionRequest);
                }
            }

            Log("Execution loop ending");
            _stopped.Set();
        }
Esempio n. 8
0
 public abstract bool ExecuteJob(JobExecutionRequest jobExecutionRequest);