Exemple #1
0
        public ActionResult Sweep()
        {
            if (!_taskScheduler.VerifyAuthToken(Request.Headers["X-AUTH-TOKEN"]))
            {
                return(new HttpUnauthorizedResult());
            }

            var pendingTasks  = _scheduledTaskService.GetPendingTasks();
            var prevTaskStart = DateTime.UtcNow;
            var count         = 0;

            foreach (var task in pendingTasks)
            {
                var elapsedSincePrevTask = DateTime.UtcNow - prevTaskStart;
                if (elapsedSincePrevTask >= TimeSpan.FromMinutes(_taskScheduler.SweepIntervalMinutes))
                {
                    // the previous Task execution in this loop took longer
                    // than the scheduler's sweep interval. Most likely a subsequent
                    // Sweep call executed it already in another HTTP request.
                    // To be able to determine this, we need to reload the entity from the database.
                    // The TaskExecutor will exit when the task should be in running state then.
                    _services.DbContext.ReloadEntity(task);
                }

                if (task.IsPending)
                {
                    prevTaskStart = DateTime.UtcNow;
                    _taskExecutor.Execute(task);
                    count++;
                }
            }

            return(Content("{0} tasks executed".FormatInvariant(count)));
        }
        public ActionResult Sweep()
        {
            if (!_taskScheduler.VerifyAuthToken(Request.Headers["X-AUTH-TOKEN"]))
            {
                return(new HttpUnauthorizedResult());
            }

            var pendingTasks  = _scheduledTaskService.GetPendingTasks();
            var prevTaskStart = DateTime.UtcNow;
            var count         = 0;

            for (var i = 0; i < pendingTasks.Count; i++)
            {
                var task = pendingTasks[i];

                if (i > 0)
                {
                    // Maybe a subsequent Sweep call or another machine in a webfarm executed
                    // successive tasks already.
                    // To be able to determine this, we need to reload the entity from the database.
                    // The TaskExecutor will exit when the task should be in running state then.
                    _services.DbContext.ReloadEntity(task);
                }

                if (task.IsPending)
                {
                    prevTaskStart = DateTime.UtcNow;
                    _taskExecutor.Execute(task);
                    count++;
                }
            }

            return(Content("{0} of {1} pending tasks executed".FormatInvariant(count, pendingTasks.Count)));
        }
Exemple #3
0
        public BatchExecutionResult ExecutePendingTasks()
        {
            var pendingQueuedTasks = _taskQueuer.GetPendingQueuedTasks();

            var tasksToExecute = _taskBuilder.GetTasksToExecute(pendingQueuedTasks);

            return(_taskExecutor.Execute(tasksToExecute));
        }
Exemple #4
0
        public BatchExecutionResult ExecutePendingTasks()
        {
            _taskResetter.ResetHungTasks();

            IList <QueuedTask>    pendingQueuedTasks    = _taskQueuer.GetPendingQueuedTasks();
            IList <ScheduledTask> pendingScheduledTasks = _taskQueuer.GetPendingScheduledTasks();

            IList <IExecutableTask> tasksToExecute = _taskBuilder.GetTasksToExecute(pendingQueuedTasks, pendingScheduledTasks);

            return(_taskExecutor.Execute(tasksToExecute));
        }
Exemple #5
0
 private async void ClockOnTick(object?sender, TickArg e)
 {
     if (toExecutes.TryDequeue(out Action toExecute))
     {
         await executor.Execute(toExecute);
     }
 }
        /// <summary>
        /// Method <c>ProcessArgs</c> is used for processing arguments and executing corresponding functions specified in task1 of altex soft lab course.
        /// </summary>
        private void ProcessArgs(string[] args)
        {
            string path        = args.Length > 1 ? args[1] : "";
            string secondParam = args.Length > 2 ? args[2] : "";

            switch (args[0])
            {
            case "func1":
                taskExecutor = new FirstTaskFileExecutor(txtHolder);
                break;

            case "func2":
                taskExecutor = new SecondTaskFileExecutor(txtHolder);
                break;

            case "func3":
                taskExecutor = new ThirdTaskFileExecutor(txtHolder);
                break;

            case "func4":
                taskExecutor = new FourthTaskDirectoryExecutor();
                break;

            default:
                Console.WriteLine("There is no such function!");
                return;
            }
            taskExecutor.Execute(path, secondParam); // execute desired function
        }
Exemple #7
0
 public virtual Task <bool> RoleNameExist(string roleName, ITaskExecutor svc)
 {
     return(svc.Execute(async() =>
     {
         return await mappingNameToUUID.ExistsAsync(roleName);
     }));
 }
        /// <summary>
        /// @see AbstractPartitionHandler#DoHandle .
        /// </summary>
        /// <param name="masterStepExecution"></param>
        /// <param name="partitionStepExecutions"></param>
        /// <returns></returns>
        protected override ICollection <StepExecution> DoHandle(StepExecution masterStepExecution, ICollection <StepExecution> partitionStepExecutions)
        {
            Assert.NotNull(Step, "A Step must be provided.");
            HashSet <Task <StepExecution> > tasks  = new HashSet <Task <StepExecution> >();
            HashSet <StepExecution>         result = new HashSet <StepExecution>();

            foreach (StepExecution stepExecution in partitionStepExecutions)
            {
                Task <StepExecution> task = CreateTask(Step, stepExecution);
                try
                {
                    _taskExecutor.Execute(task);
                    tasks.Add(task);
                }
                catch (TaskRejectedException)
                {
                    // couldn't execute one of the tasks
                    ExitStatus exitStatus = ExitStatus.Failed.AddExitDescription("TaskExecutor rejected the task for this step.");

                    // Set the status in case the caller is tracking it through the
                    // JobExecution.
                    stepExecution.BatchStatus = BatchStatus.Failed;
                    stepExecution.ExitStatus  = exitStatus;
                    result.Add(stepExecution);
                }
            }

            foreach (Task <StepExecution> task in tasks)
            {
                // Accessing Result is blocking (waits for asynchronous execution to complete)
                result.Add(task.Result);
            }

            return(result);
        }
Exemple #9
0
        /// <summary>
        /// Build all the tasks for this node
        /// </summary>
        /// <param name="Job">Information about the current job</param>
        /// <param name="TagNameToFileSet">Mapping from tag names to the set of files they include. Should be set to contain the node inputs on entry.</param>
        /// <returns>Whether the task succeeded or not. Exiting with an exception will be caught and treated as a failure.</returns>
        public bool Build(JobContext Job, Dictionary <string, HashSet <FileReference> > TagNameToFileSet)
        {
            // Run each of the tasks in order
            HashSet <FileReference> BuildProducts = TagNameToFileSet[DefaultOutput.TagName];

            for (int Idx = 0; Idx < Tasks.Count; Idx++)
            {
                ITaskExecutor Executor = Tasks[Idx].GetExecutor();
                if (Executor == null)
                {
                    // Execute this task directly
                    try
                    {
                        Tasks[Idx].Execute(Job, BuildProducts, TagNameToFileSet);
                    }
                    catch (Exception Ex)
                    {
                        ExceptionUtils.AddContext(Ex, "while executing task {0}", Tasks[Idx].GetTraceString());
                        if (Tasks[Idx].SourceLocation != null)
                        {
                            ExceptionUtils.AddContext(Ex, "at {0}({1})", GetReadablePathForDiagnostics(Tasks[Idx].SourceLocation.Item1), Tasks[Idx].SourceLocation.Item2);
                        }
                        throw;
                    }
                }
                else
                {
                    // The task has a custom executor, which may be able to execute several tasks simultaneously. Try to add the following tasks.
                    int FirstIdx = Idx;
                    while (Idx + 1 < Tasks.Count && Executor.Add(Tasks[Idx + 1]))
                    {
                        Idx++;
                    }
                    try
                    {
                        Executor.Execute(Job, BuildProducts, TagNameToFileSet);
                    }
                    catch (Exception Ex)
                    {
                        for (int TaskIdx = FirstIdx; TaskIdx <= Idx; TaskIdx++)
                        {
                            ExceptionUtils.AddContext(Ex, "while executing {0}", Tasks[TaskIdx].GetTraceString());
                        }
                        if (Tasks[FirstIdx].SourceLocation != null)
                        {
                            ExceptionUtils.AddContext(Ex, "at {0}({1})", GetReadablePathForDiagnostics(Tasks[FirstIdx].SourceLocation.Item1), Tasks[FirstIdx].SourceLocation.Item2);
                        }
                        throw;
                    }
                }
            }

            // Remove anything that doesn't exist, since these files weren't explicitly tagged
            BuildProducts.RemoveWhere(x => !FileReference.Exists(x));
            return(true);
        }
Exemple #10
0
        public ExitCode Execute(ITaskInteraction interaction)
        {
            _usageTracker.RecordUsage(TaskFeatureIdentifier);
            ITaskExecutor executor = _task.GetService <ITaskExecutor>();

            if (executor != null)
            {
                return(executor.Execute(interaction));
            }

            return(ExitCode.Success);
        }
 /// <summary>
 /// 开始运行。
 /// </summary>
 public void Start()
 {
     if (_timer == null)
     {
         _timer = new Timer(o =>
         {
             using var scope = _context.ServiceProvider.TryCreateScope();
             var context     = new TaskExecuteContext(scope.ServiceProvider, _context.Arguments, _context.CancellationToken);
             _executor.Execute(context);
         }, null, _delay, _period);
     }
 }
Exemple #12
0
        /// <summary>
        /// Executes the given task
        /// </summary>
        public static ExitCode Execute(this ITask task, ITaskInteraction interaction)
        {
            ITaskExecutor executor = task.GetService <ITaskExecutor>();

            if (executor != null)
            {
                return(executor.Execute(interaction));
            }
            else
            {
                return(ExitCode.Success);
            }
        }
Exemple #13
0
        public ActionResult Sweep()
        {
            if (!_taskScheduler.VerifyAuthToken(Request.Headers["X-AUTH-TOKEN"]))
            {
                return(new HttpUnauthorizedResult());
            }

            var pendingTasks   = _scheduleTaskService.GetPendingTasks();
            var count          = 0;
            var taskParameters = QueryString.Current.ToDictionary();

            if (pendingTasks.Count > 0)
            {
                Virtualize(taskParameters);
            }

            for (var i = 0; i < pendingTasks.Count; i++)
            {
                var task = pendingTasks[i];

                if (i > 0 /*&& (DateTime.UtcNow - _sweepStart).TotalMinutes > _taskScheduler.SweepIntervalMinutes*/)
                {
                    // Maybe a subsequent Sweep call or another machine in a webfarm executed
                    // successive tasks already.
                    // To be able to determine this, we need to reload the entity from the database.
                    // The TaskExecutor will exit when the task should be in running state then.
                    _services.DbContext.ReloadEntity(task);
                    task.LastHistoryEntry = _scheduleTaskService.GetLastHistoryEntryByTaskId(task.Id);
                }

                if (task.IsPending)
                {
                    _taskExecutor.Execute(task, taskParameters);
                    count++;
                }
            }

            return(Content("{0} of {1} pending tasks executed".FormatInvariant(count, pendingTasks.Count)));
        }
Exemple #14
0
 public virtual Task <string[]> GetRoleUUIDByDigitAsync(string digit, ITaskExecutor svc)
 {
     return(svc.Execute(mappingDigitToUUID.GetAsync(digit).ContinueWith <string[]>(t =>
     {
         if (t.IsCompleted)
         {
             var exist = t.GetResultToString();
             if (exist != null)
             {
                 return exist.Split(',');
             }
         }
         return null;
     })));
 }
Exemple #15
0
 /// <summary>
 /// Execute the given <see cref="T:Quartz.IThreadRunnable"/> in the next
 /// available <see cref="T:System.Threading.Thread"/>.
 /// </summary>
 /// <param name="runnable"></param>
 /// <returns></returns>
 /// <remarks>
 /// The implementation of this interface should not throw exceptions unless
 /// there is a serious problem (i.e. a serious misconfiguration). If there
 /// are no available threads, rather it should either queue the Runnable, or
 /// block until a thread is available, depending on the desired strategy.
 /// </remarks>
 public virtual bool RunInThread(IThreadRunnable runnable)
 {
     if (runnable == null)
     {
         return(false);
     }
     try
     {
         taskExecutor.Execute(runnable.Run);
         return(true);
     }
     catch (TaskRejectedException ex)
     {
         logger.Error("Task has been rejected by TaskExecutor", ex);
         return(false);
     }
 }
Exemple #16
0
 public virtual Task <bool> RoleChangeNameMappingAsync(string roleUUID, string newName, string curName, ITaskExecutor svc)
 {
     return(svc.Execute(async() =>
     {
         if (await mappingNameToUUID.SetAsync(newName, roleUUID, When.NotExists))
         {
             //删除旧的名字.
             await mappingNameToUUID.DeleteAsync(curName);
             //删除旧的UUID关联.
             await mappingUUIDToName.DeleteAsync(roleUUID);
             //设置新的UUID关联.
             await mappingUUIDToName.SetAsync(roleUUID, newName);
             return true;
         }
         return false;
     }));
 }
Exemple #17
0
        private void PollForWork()
        {
            Log.Info("Requesting work");

            var response = _serverChannel.RequestWork();

            if (!response.Payload.NoWorkAvailable)
            {
                Log.Info("Work Available {Task}".FormatWith(new { Task = response.Payload.Task.ToString() }));
                _executor.Execute(response.Payload.Task);
            }
            else
            {
                Log.Info("No work available");
                Log.Info("Sleeping for {_pollingInterval}".FormatWith(new { _pollingInterval }));
                Thread.Sleep(_pollingInterval);
            }
        }
Exemple #18
0
        /// <inheritdoc />
        public virtual bool RunInThread(Func <Task> runnable)
        {
            if (runnable == null)
            {
                return(false);
            }

            try
            {
                taskExecutor.Execute(() => runnable().ConfigureAwait(false).GetAwaiter().GetResult());
                return(true);
            }
            catch (TaskRejectedException ex)
            {
                Logger.Error("Task has been rejected by TaskExecutor", ex);
                return(false);
            }
        }
Exemple #19
0
 public virtual Task <string> TryRegistRoleNameMappingAsync(string roleUUID, string roleName, ITaskExecutor svc)
 {
     return(svc.Execute(async() =>
     {
         if (await mappingNameToUUID.SetAsync(roleName, roleUUID, When.NotExists))
         {
             await mappingUUIDToName.SetAsync(roleUUID, roleName);
             var digitID = Instance.GenDigitID(roleUUID);
             if (await mappingDigitToUUID.SetAsync(digitID, roleUUID, When.NotExists) == false)
             {
                 var exist = await mappingDigitToUUID.GetAsync(digitID);
                 await mappingDigitToUUID.SetAsync(digitID, $"{exist},{roleUUID}");
             }
             await mappingUUIDToDigit.SetAsync(roleUUID, digitID);
             return digitID;
         }
         return null;
     }));
 }
        /// <summary>
        /// Execute system command and map its exit code to ExitStatus using <see cref="SystemProcessExitCodeMapper"/>.
        /// </summary>
        /// <param name="contribution"></param>
        /// <param name="chunkContext"></param>
        /// <returns></returns>
        public RepeatStatus Execute(StepContribution contribution, Scope.Context.ChunkContext chunkContext)
        {
            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource())
            {
                CancellationToken cancellationToken = cancellationTokenSource.Token;
                using (Task <int> systemCommandTask = new Task <int>(ExecuteCommand, cancellationToken))
                {
                    long t0 = DateTime.Now.Ticks;
                    _taskExecutor.Execute(systemCommandTask);

                    while (true)
                    {
                        Thread.Sleep(new TimeSpan(_checkInterval));

                        CheckStoppingState(chunkContext);

                        if (systemCommandTask.IsCompleted)
                        {
                            return(HandleCompletion(contribution, systemCommandTask));
                        }
                        else if (new TimeSpan(DateTime.Now.Ticks - t0).TotalMilliseconds > _timeout)
                        {
                            cancellationTokenSource.Cancel();
                            throw new SystemCommandException(
                                      "Execution of system command did not finish within the timeout");
                        }
                        else if (_execution.TerminateOnly)
                        {
                            cancellationTokenSource.Cancel();
                            throw new JobInterruptedException(
                                      string.Format("Job interrupted while executing system command '{0}'", Command));
                        }
                        else if (_stopped)
                        {
                            cancellationTokenSource.Cancel();
                            contribution.ExitStatus = ExitStatus.Stopped;
                            return(RepeatStatus.Finished);
                        }
                    }
                }
            }
        }
        private void ExecuteBatch(ITaskExecutor taskExecutor, PipeInfo pipeInfo, RedisPipeBatch messageBatch)
        {
            foreach (var redisMessage in messageBatch.RedisPipeValues)
            {
                try
                {
                    taskExecutor.Execute(pipeInfo, redisMessage);
                    _taskFunnel.AfterExecute(redisMessage, true);
                }
                catch (Exception e)
                {
                    _logger.LogError(e,
                                     $"Error handling from {pipeInfo.ParentPipeName}/{pipeInfo.ChildPipeName}");
                    //this will resubmit the message
                    _taskFunnel.AfterExecute(redisMessage, false);
                }
            }

            _taskFunnel.AfterExecuteBatch(messageBatch);
        }
Exemple #22
0
 public override void OnAppStarted(App app)
 {
     using (var work = app.BeginWork("DoTask"))
     {
         ILogger logger = work.CreateComponent <ILogger>();
         taskInfos.ForEach(taskInfo =>
         {
             ITaskExecutor taskExecutor = (ITaskExecutor)work.CreateComponent(taskInfo.TaskType);
             try
             {
                 logger.Info(string.Format("开始执行 [{0}]", taskInfo.TaskAttribute.Name));
                 taskExecutor.Execute();
                 logger.Info(string.Format("[{0}] 执行完成", taskInfo.TaskAttribute.Name));
             }
             catch (System.Exception ex)
             {
                 logger.Error(string.Format("[{0}] 执行失败 : {1}", taskInfo.TaskAttribute.Name, ex.Message));
             }
         });
     }
 }
Exemple #23
0
        /// <summary>
        /// @see IState#Handle .
        /// </summary>
        /// <param name="executor"></param>
        /// <returns></returns>
        public override FlowExecutionStatus Handle(IFlowExecutor executor)
        {
            ICollection <Task <FlowExecution> > tasks = new List <Task <FlowExecution> >();

            foreach (IFlow flow in _flows)
            {
                IFlow aFlow = flow;
                Task <FlowExecution> task = new Task <FlowExecution>(() => aFlow.Start(executor));
                tasks.Add(task);
                try
                {
                    _taskExecutor.Execute(task);
                }
                catch (TaskRejectedException)
                {
                    throw new FlowExecutionException("TaskExecutor rejected task for flow=" + flow.GetName());
                }
            }
            ICollection <FlowExecution> results = tasks.Select(task => task.Result).ToList();

            return(DoAggregation(results, executor));
        }
        /// <summary>
        /// Runs the provided job with the given <see cref="JobParameters"/>. The
        /// JobParameters will be used to determine if this is an execution
        /// of an existing job instance, or if a new one should be created.
        /// </summary>
        /// <param name="job"></param>
        /// <param name="jobParameters"></param>
        /// <returns></returns>
        /// <exception cref="JobRestartException">&nbsp; if the execution would be a re-start, but a re-start is either not allowed or not needed.</exception>
        /// <exception cref="JobExecutionAlreadyRunningException">&nbsp;if the JobInstance already exists and has an execution already running</exception>
        /// <exception cref="JobInstanceAlreadyCompleteException">&nbsp; if this instance has already completed successfully</exception>
        /// <exception cref="JobParametersInvalidException">&nbsp;if given parameters do not pass validation process</exception>
        public JobExecution Run(IJob job, JobParameters jobParameters)
        {
            Assert.NotNull(job, "The job must be not null");
            Assert.NotNull(jobParameters, "The job parameters must be not null");
            JobExecution lastExecution = _jobRepository.GetLastJobExecution(job.Name, jobParameters);

            //manage last execution if needed
            HandleLastExecution(job, lastExecution);

            //validate Parameters
            job.JobParametersValidator.Validate(jobParameters);

            //create new job execution
            var jobExecution = _jobRepository.CreateJobExecution(job.Name, jobParameters);

            //make an creation to be able to create a task, that will be executed by given TaskExecutor as expected
            var jobAction = CreateJobAction(job, jobParameters, jobExecution);

            using (var jobTask = new Task(jobAction))
            {
                try
                {
                    _taskExecutor.Execute(jobTask);
                    //in case of asynchronous executor ...
                    jobTask.Wait();
                }
                catch (InvalidOperationException exception)
                {
                    jobExecution.UpgradeStatus(BatchStatus.Failed);
                    if (jobExecution.ExitStatus.Equals(ExitStatus.Unknown))
                    {
                        jobExecution.ExitStatus = ExitStatus.Failed.AddExitDescription(exception);
                    }
                    _jobRepository.Update(jobExecution);
                }
            }
            return(jobExecution);
        }
Exemple #25
0
        /// <summary>
        /// Build all the tasks for this node
        /// </summary>
        /// <param name="Job">Information about the current job</param>
        /// <param name="TagNameToFileSet">Mapping from tag names to the set of files they include. Should be set to contain the node inputs on entry.</param>
        /// <returns>Whether the task succeeded or not. Exiting with an exception will be caught and treated as a failure.</returns>
        public bool Build(JobContext Job, Dictionary <string, HashSet <FileReference> > TagNameToFileSet)
        {
            // Run each of the tasks in order
            HashSet <FileReference> BuildProducts = TagNameToFileSet[DefaultOutput.TagName];

            for (int Idx = 0; Idx < Tasks.Count; Idx++)
            {
                ITaskExecutor Executor = Tasks[Idx].GetExecutor();
                if (Executor == null)
                {
                    // Execute this task directly
                    if (!Tasks[Idx].Execute(Job, BuildProducts, TagNameToFileSet))
                    {
                        CommandUtils.Log("Failed to execute task.");
                        return(false);
                    }
                }
                else
                {
                    // The task has a custom executor, which may be able to execute several tasks simultaneously. Try to add the following tasks.
                    while (Idx + 1 < Tasks.Count && Executor.Add(Tasks[Idx + 1]))
                    {
                        Idx++;
                    }
                    if (!Executor.Execute(Job, BuildProducts, TagNameToFileSet))
                    {
                        CommandUtils.Log("Failed to execute task.");
                        return(false);
                    }
                }
            }

            // Remove anything that doesn't exist, since these files weren't explicitly tagged
            BuildProducts.RemoveWhere(x => !FileReference.Exists(x));
            return(true);
        }
Exemple #26
0
 public TaskResult basic_processing()
 {
     return(_executor.Execute(_task));
 }
Exemple #27
0
 public virtual Task <string> GetRoleUUIDByNameAsync(string roleName, ITaskExecutor svc)
 {
     return(svc.Execute(mappingNameToUUID.GetAsync(roleName).ContinueWith <string>(t => t.GetResultToString())));
 }
Exemple #28
0
 public virtual Task <string> GetRoleDigitByUUIDAsync(string roleUUID, ITaskExecutor svc)
 {
     return(svc.Execute(mappingUUIDToDigit.GetAsync(roleUUID).ContinueWith <string>(t => t.GetResultToString())));
 }
Exemple #29
0
 public void OnFileSelect(string path)
 {
     ImagePath = path;
     TimeInfoContainerIsVisible = false;
     imageLoadingExecutor.Execute(GetFileReadingTask(path));
 }
Exemple #30
0
 private void ProcessImage() => imageProcessingExecutor.Execute(GetProcessingTask());