Esempio n. 1
0
        public async Task <WorkableTask> Execute([FromBody] ExecuteScriptParameters parameters)
        {
            logger.LogInformation($"Executing {parameters.Id?.ToString() ?? parameters.Name} with parameters '{string.Join(";", parameters.Parameters?.Select(p => $"{p.Key}={p.Value}")??new string[0])}'");

            try {
                if (parameters.Id.HasValue)
                {
                    if (!string.IsNullOrEmpty(parameters.Name))
                    {
                        throw new ArgumentException("Either id or name has to be set, not both");
                    }
                    return(await executionservice.Execute(parameters.Id.Value, parameters.Revision, parameters.Parameters, parameters.Wait));
                }

                if (!string.IsNullOrEmpty(parameters.Name))
                {
                    return(await executionservice.Execute(parameters.Name, parameters.Revision, parameters.Parameters, parameters.Wait));
                }

                if (parameters.Code != null)
                {
                    return(await executionservice.Execute(parameters.Code, parameters.Parameters, parameters.Wait));
                }
            }
            catch (Exception e) {
                WorkableTask failtask = new WorkableTask {
                    Id       = Guid.NewGuid(),
                    Status   = TaskStatus.Failure,
                    Started  = DateTime.Now,
                    Finished = DateTime.Now,
                    Log      = new List <string>(new[] {
                        e.ToString()
                    }),
                    Parameters = parameters.Parameters,
                    Type       = WorkableType.Script,
                };
                await taskservice.StoreTask(failtask);

                return(failtask);
            }

            throw new ArgumentException("Script id/name or scoped code to execute is required");
        }
Esempio n. 2
0
        async Task CheckTasks()
        {
            List <ScheduledTask> tasks  = new List <ScheduledTask>();
            ScheduledTaskFilter  filter = new ScheduledTaskFilter {
                DueTime = DateTime.Now
            };

            while (true)
            {
                Page <ScheduledTask> page = await scheduledtaskservice.List(filter);

                tasks.AddRange(page.Result);
                if (page.Result.Length == 0 || !page.Continue.HasValue)
                {
                    break;
                }
                filter.Continue = page.Continue;
            }

            foreach (ScheduledTask task in tasks)
            {
                switch (task.WorkableType)
                {
                case WorkableType.Workflow:
                    await workflowexecutor.Execute(await workflowcompiler.BuildWorkflow(task.WorkableName), new Dictionary <string, object>(), false);

                    break;

                case WorkableType.Script:
                    await scriptexecutor.Execute(task.WorkableName, task.WorkableRevision);

                    break;
                }

                try {
                    await scheduledtaskservice.UpdateExecution(task.Id, task.NextExecutionTime());
                }
                catch (Exception e) {
                    logger.LogError(e, $"Unable to reschedule task '{task.Id}'");
                }
            }
        }