protected async Task <ActionResult <T> > Execute <T>(Func <IOperation, Task <T> > action, [CallerMemberName] string callerName = "") { try { var operationContext = OperationContext.Builder().SetName(GetType().FullName, callerName).SetUserId(UserId).Create(); return(await operationService.Make(operationContext, action)); } catch (BadRequestException e) { return(ApiControllerUtils.HandleException(BadRequest, e)); } catch (NotFoundException e) { return(ApiControllerUtils.HandleException(NotFound, e)); } catch (AuthenticationException e) when(e.StatusCode == StatusCodes.Status401Unauthorized) { return(ApiControllerUtils.HandleException(Unauthorized, e)); } catch (AuthenticationException e) when(e.StatusCode == StatusCodes.Status403Forbidden) { return(ApiControllerUtils.HandleException(ApiControllerUtils.Forbidden, e)); } catch (OperationException e) { return(ApiControllerUtils.HandleException(BadRequest, e)); } catch (Exception e) { return(ApiControllerUtils.HandleException(ApiControllerUtils.InternalServerError, e)); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { const string scope = "VXDS_SNS"; // Portal properties services.SetupProperties <PortalProperties>(Configuration); // Stores services.AddSingleton <ILoggerStore>(factory => { var logStoreConnectionString = factory.GetService <PortalProperties>().DatabaseConnectionProperties.LogStoreConnectionString; return(new LoggerStore(logStoreConnectionString, scope)); }); services.AddScoped <IUserRoleStore, UserRoleStore>(); services.AddScoped <INoteFolderStore, NoteFolderStore>(); // Services services.AddScoped <IOperationService>(factory => { var loggerStore = factory.GetService <ILoggerStore>(); var dataStoreConnectionString = factory.GetService <PortalProperties>().DatabaseConnectionProperties.DataStoreConnectionString; return(new OperationService(loggerStore, dataStoreConnectionString, scope)); }); services.AddScoped <ISyrinxCamundaClientService>(factory => new SyrinxCamundaClientService(factory.GetService <PortalProperties>().SyrinxProperties)); services.AddScoped <ISyrinxAuthenticationClientService>(factory => new SyrinxAuthenticationClientService(factory.GetService <PortalProperties>().SyrinxProperties)); services.AddScoped <INoteFolderService, NoteFolderService>(); // Permission group (workaround) var provider = services.BuildServiceProvider(); var operationService = provider.GetService <IOperationService>(); var userRoleStore = provider.GetService <IUserRoleStore>(); var context = OperationContext.Builder() .SetName(GetType().FullName, "Startup") .SetUserId(null, true) .Create(); PortalPermissionKey.PermissionGroupId = operationService.Make(context, async operation => { const string name = "Simple Note Service Access & Management"; var permissionGroupId = await userRoleStore.GetUserRolePermission(operation, name); if (!permissionGroupId.HasValue) { throw CommonExceptions.PermissionGroupWasNotFound(operation, name); } return(permissionGroupId.Value); }).Result; services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); services.AddRouting(options => options.LowercaseUrls = true); services.ConfigureSwaggerDocument("1.0", "Simple Note Service"); }
public CamundaWorkersBuilder AddWorker <TCamundaWorker>() where TCamundaWorker : CamundaWorker { var camundaWorkerType = typeof(TCamundaWorker); var operationContext = OperationContext.Builder() .SetName(camundaWorkerType.FullName, GetTopicName(camundaWorkerType), "Background") .SetUserId(null, true) .SetIsolationLevel(IsolationLevel.Unspecified) .Create(); RunnableTasks.Add(workers => workers.OperationService.Make(operationContext, workers.Fetch <TCamundaWorker>)); ServiceCollection.AddScoped <TCamundaWorker>(); return(this); }
public void Downgrade() { var context = OperationContext.Builder() .SetName(GetType().FullName, nameof(Downgrade)) .SetUserId(null, true) .Create(); operationService.Make(context, async operation => { var logger = operation.Logger <CamundaDeploymentService>(); await logger.Info("Downgrading of all Camunda workflows is started"); var settings = GetSettings(operation); var deployments = await GetDeployments(operation, logger, settings); foreach (var deployment in deployments) { await DeleteDeployment(operation, logger, deployment); } }).Wait(); }
public void DowngradeToPrevious() { var context = OperationContext.Builder() .SetName(GetType().FullName, nameof(DowngradeToPrevious)) .SetUserId(null, true) .Create(); operationService.Make(context, async operation => { var logger = operation.Logger <CamundaDeploymentService>(); await logger.Info("Downgrading to previous deployment of Camunda workflows is started"); var settings = GetSettings(operation); var deployments = await GetDeployments(operation, logger, settings); if (deployments.Count > 0) { await DeleteDeployment(operation, logger, deployments[0]); } }).Wait(); }
public void Upgrade() { var context = OperationContext.Builder() .SetName(GetType().FullName, nameof(Upgrade)) .SetUserId(null, true) .Create(); operationService.Make(context, async operation => { var logger = operation.Logger <CamundaDeploymentService>(); await logger.Info("Upgrading to latest version of Camunda workflows is started"); var settings = GetSettings(operation); var files = GetFiles(operation); for (var i = 0; i < files.Count; i++) { await logger.Info($"– [{i + 1}] Camunda workflow file: {files[i].FileName}"); } var response = await new Deployment.CreateRequest { DeploymentName = settings.VersionName, DeploymentSource = settings.ProjectName, EnableDuplicateFiltering = true, DeployChangedOnly = true, Files = files }.SendRequest(operation, camundaClient); if (response.IsWithoutErrors()) { await logger.Info($"Camunda deployment was created:\n{JsonConvert.SerializeObject(response.Response, Formatting.Indented)}"); } else { await logger.Error($"Camunda deployment wasn't created:\n{JsonConvert.SerializeObject(response.Errors, Formatting.Indented)}"); throw CommonExceptions.FailedToDeployCamundaWorkflows(operation); } }).Wait(); }
public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { var operationContext = OperationContext.Builder() .SetName(GetType().FullName, "OnAuthorization") .SetUserId(null, true) .Create(); try { await operationService.Make(operationContext, async operation => await ValidateToken(context, operation)); } catch (AuthenticationException e) when(e.StatusCode == StatusCodes.Status401Unauthorized) { context.Result = ApiControllerUtils.HandleException(result => new UnauthorizedObjectResult(result), e); } catch (AuthenticationException e) when(e.StatusCode == StatusCodes.Status403Forbidden) { context.Result = ApiControllerUtils.HandleException(ApiControllerUtils.Forbidden, e); } catch (Exception e) { context.Result = ApiControllerUtils.HandleException(ApiControllerUtils.InternalServerError, e); } }
private async Task Fetch <TCamundaWorker>(IOperation operation) where TCamundaWorker : CamundaWorker { var logger = operation.Logger <TCamundaWorker>(); var topics = new CamundaTopics { new CamundaTopic { TopicName = GetTopicName(typeof(TCamundaWorker)), LockDuration = Properties.LockDuration, Variables = CamundaWorker.InputVariableNames(typeof(TCamundaWorker)) } }; var retries = Properties.CountOfRetriesWhenFetchIsUnsuccessful; while (retries > 0) { await logger.Trace($"[Operation: {operation.ComplexOperationId}] Fetching task"); var response = await new ExternalTask.FetchAndLockRequest { AsyncResponseTimeout = Properties.FetchTimeout, UsePriority = Properties.UsePriority, MaxTasks = 1, WorkerId = $"w-{Properties.WorkerKeyword}-{DateTime.Today:yyyyMMdd}", Topics = topics }.SendRequest(operation, CamundaClient); if (!response.IsWithoutErrors()) { retries--; var retryAfter = TimeSpan.FromMilliseconds(Properties.RetryAfterFetchTimeout); await logger.Error($"[Operation: {operation.ComplexOperationId}] Failed to fetch task, retry after {retryAfter}... (remaining attempts: {retries})", response.ToLog()); if (retries != 0) { await Task.Delay(retryAfter); } } else if (response.Response.Count > 0) { await logger.Trace($"[Operation: {operation.ComplexOperationId}] Executing task"); try { var camundaWorkerType = typeof(TCamundaWorker); var operationContext = OperationContext.Builder() .SetName(camundaWorkerType.FullName, GetTopicName(camundaWorkerType), "TaskRunner") .SetUserId(null, true) .SetParentOperation(operation) .Create(); await OperationService.Make(operationContext, async innerOperation => await Run <TCamundaWorker>(innerOperation, logger, response.Response[0])); } catch { // ignored } await Task.Delay(TimeSpan.FromSeconds(1)); } } }