Example #1
0
        public override void Execute()
        {
            var callbackAttributes = Attribute.GetCustomAttributes(ExecutingTask.GetType(), typeof(ExecutionCallbacksAttribute));

            for (var i = 0; i < callbackAttributes.Length; i++)
            {
                ((ExecutionCallbacksAttribute)callbackAttributes[i]).BeforeExecute(ExecutingTask);
            }

            try
            {
                Invocation.Proceed();
            }
            catch (Exception exception)
            {
                for (var i = callbackAttributes.Length - 1; i >= 0; i--)
                {
                    ((ExecutionCallbacksAttribute)callbackAttributes[i]).OnError(ExecutingTask, exception);
                }

                throw;
            }

            for (var i = callbackAttributes.Length - 1; i >= 0; i--)
            {
                ((ExecutionCallbacksAttribute)callbackAttributes[i]).AfterExecute(ExecutingTask);
            }
        }
Example #2
0
        /// <summary>
        /// Launch the processing of the project
        /// </summary>
        /// <param name="project">The project to process</param>
        /// <returns>false : The project is already processing else true</returns>
        public Task <TJob> ExecuteTask <TJob>(TJob job)
            where TJob : IJob
        {
            var executingTask = new ExecutingTask();

            Task        task      = null;
            Task <TJob> finalTask = null;

            // Check if this project is not currently processing
            lock (_syncObject)
            {
                if (_processingJobs.ContainsKey(job.IdJob))
                {
                    _logger.LogDebug($"Try to start a job that is already running");
                    return(null); // Already processing
                }
                // Create a task to start the processing
                var tokenSource = new CancellationTokenSource();
                executingTask.CancellationTokenSource = tokenSource;
                _logger.LogInformation($"Start the Job {job.IdJob}");
                task               = new Task(() => job.ExecuteAsync(executingTask.CancellationTokenSource.Token).Wait());
                finalTask          = task.ContinueWith(t => EndProcessingJob(job));
                executingTask.Task = task;
                if (_processingJobs.Count >= _maxJobInParallel)
                {
                    // Start process later
                    _waitingJobs.Enqueue(executingTask);
                }
                else
                {
                    // Start process now
                    _processingJobs.Add(job.IdJob, executingTask);
                    executingTask.Task.Start();
                }
            }
            return(finalTask);
        }