Esempio n. 1
0
        private object CreateHandler(BackgroundTask task)
        {
            try
            {
                var handlerInfo = Serializer.Deserialize <HandlerInfo>(task.Handler);

                var typeName = $"{handlerInfo.Namespace}.{handlerInfo.Entrypoint}";
                var type     = _typeResolver.FindByFullName(typeName) ?? _typeResolver.FindFirstByName(typeName);
                return(type == null ? null : _backgroundServices.AutoResolve(type));
            }
            catch (Exception e)
            {
                _logger.Error(() => "Error creating handler for task", e);
                throw;
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> CreateBackgroundTask([FromBody] CreateBackgroundTaskModel model)
        {
            if (!this.TryValidateModelOrError(model, ErrorEvents.ValidationFailed, ErrorStrings.ValidationFailed,
                                              out var error))
            {
                return(error);
            }

            if (!string.IsNullOrWhiteSpace(model.TaskType))
            {
                var type = _typeResolver.FindByFullName(model.TaskType) ??
                           _typeResolver.FindFirstByName(model.TaskType);
                if (type == null)
                {
                    return(this.BadRequestError(ErrorEvents.ResourceMissing, "No task type found with name {0}",
                                                model.TaskType));
                }

                var(success, task) = await _host.TryScheduleTaskAsync(type, model.Data, t =>
                {
                    if (model.Tags?.Length > 0)
                    {
                        t.Tags.AddRange(model.Tags);
                    }

                    if (!string.IsNullOrWhiteSpace(model.Expression))
                    {
                        t.Expression = model.Expression;
                    }
                });

                if (!success)
                {
                    return(this.NotAcceptableError(ErrorEvents.CouldNotAcceptWork,
                                                   "Task was not accepted by the server"));
                }

                return(Accepted(new Uri($"{Request.Path}/{task.Id}", UriKind.Relative)));
            }

            return(this.NotImplemented());
        }