public static async Task PutServiceAsync(ApiGatewayRequest request, McmaApiResponse response) { Logger.Debug(nameof(PutServiceAsync)); Logger.Debug(request.ToMcmaJson().ToString()); var service = request.JsonBody?.ToMcmaObject <Service>(); if (service == null) { response.StatusCode = (int)HttpStatusCode.BadRequest; response.StatusMessage = "Missing request body."; return; } var table = new DynamoDbTable(request.StageVariables["TableName"]); var serviceId = request.StageVariables["PublicUrl"] + request.Path; service.Id = serviceId; service.DateModified = DateTime.UtcNow; if (!service.DateCreated.HasValue) { service.DateCreated = service.DateModified; } await table.PutAsync <Service>(serviceId, service); response.JsonBody = service.ToMcmaJson(); }
internal static async Task ProcessNotificationAsync(WorkflowServiceWorkerRequest @event) { var jobAssignmentId = @event.JobAssignmentId; var notification = @event.Notification; var notificationJobPayload = notification.Content.ToMcmaObject <JobBase>(); var table = new DynamoDbTable(@event.Request.StageVariables["TableName"]); var jobAssignment = await table.GetAsync <JobAssignment>(jobAssignmentId); jobAssignment.Status = notificationJobPayload.Status; jobAssignment.StatusMessage = notificationJobPayload.StatusMessage; if (notificationJobPayload.Progress != null) { jobAssignment.Progress = notificationJobPayload.Progress; } jobAssignment.JobOutput = notificationJobPayload.JobOutput; jobAssignment.DateModified = DateTime.UtcNow; await table.PutAsync <JobAssignment>(jobAssignmentId, jobAssignment); var resourceManager = @event.Request.GetAwsV4ResourceManager(); await resourceManager.SendNotificationAsync(jobAssignment, jobAssignment.NotificationEndpoint); }
public static async Task AddServiceAsync(ApiGatewayRequest request, McmaApiResponse response) { Logger.Debug(nameof(AddServiceAsync)); Logger.Debug(request.ToMcmaJson().ToString()); var service = request.JsonBody?.ToMcmaObject <Service>(); if (service == null) { response.StatusCode = (int)HttpStatusCode.BadRequest; response.StatusMessage = "Missing request body."; return; } var serviceId = request.StageVariables["PublicUrl"] + "/services/" + Guid.NewGuid(); service.Id = serviceId; service.DateCreated = DateTime.UtcNow; service.DateModified = service.DateCreated; var table = new DynamoDbTable(request.StageVariables["TableName"]); await table.PutAsync <Service>(serviceId, service); response.StatusCode = (int)HttpStatusCode.Created; response.Headers["Location"] = service.Id; response.JsonBody = service.ToMcmaJson(); Logger.Debug(response.ToMcmaJson().ToString()); }
protected override async Task ExecuteAsync(WorkerRequest @event, ProcessNotificationRequest notificationRequest) { var jobAssignmentId = notificationRequest.JobAssignmentId; var notification = notificationRequest.Notification; var notificationJobPayload = notification.Content.ToMcmaObject <JobBase>(); var table = new DynamoDbTable <JobAssignment>(@event.TableName()); var jobAssignment = await table.GetAsync(jobAssignmentId); jobAssignment.Status = notificationJobPayload.Status; jobAssignment.StatusMessage = notificationJobPayload.StatusMessage; if (notificationJobPayload.Progress != null) { jobAssignment.Progress = notificationJobPayload.Progress; } jobAssignment.JobOutput = notificationJobPayload.JobOutput; jobAssignment.DateModified = DateTime.UtcNow; await table.PutAsync(jobAssignmentId, jobAssignment); var resourceManager = @event.GetAwsV4ResourceManager(); await resourceManager.SendNotificationAsync(jobAssignment, jobAssignment.NotificationEndpoint); }
public static async Task PutBmContentAsync(ApiGatewayRequest request, McmaApiResponse response) { Logger.Debug(nameof(PutBmContentAsync)); Logger.Debug(request.ToMcmaJson().ToString()); var bmContent = request.JsonBody?.ToMcmaObject <BMContent>(); if (bmContent == null) { response.StatusCode = (int)HttpStatusCode.BadRequest; response.StatusMessage = "Missing request body."; return; } var table = new DynamoDbTable(request.StageVariables["TableName"]); var bmContentId = request.StageVariables["PublicUrl"] + request.Path; bmContent.Id = bmContentId; bmContent.DateModified = DateTime.UtcNow; if (!bmContent.DateCreated.HasValue) { bmContent.DateCreated = bmContent.DateModified; } await table.PutAsync <BMContent>(bmContentId, bmContent); response.JsonBody = bmContent.ToMcmaJson(); }
internal static async Task ProcessNotificationAsync(JobRepositoryWorkerRequest @event) { var jobId = @event.JobId; var notification = @event.Notification; var notificationJob = notification.Content.ToMcmaObject <JobBase>(); var table = new DynamoDbTable(@event.Request.StageVariables["TableName"]); var job = await table.GetAsync <Job>(jobId); // not updating job if it already was marked as completed or failed. if (job.Status == "COMPLETED" || job.Status == "FAILED") { Logger.Warn("Ignoring update of job that tried to change state from " + job.Status + " to " + notificationJob.Status); return; } job.Status = notificationJob.Status; job.StatusMessage = notificationJob.StatusMessage; job.Progress = notificationJob.Progress; job.JobOutput = notificationJob.JobOutput; job.DateModified = DateTime.UtcNow; await table.PutAsync <Job>(jobId, job); var resourceManager = @event.Request.GetAwsV4ResourceManager(); await resourceManager.SendNotificationAsync(job, job.NotificationEndpoint); }
public static async Task AddBmContentAsync(ApiGatewayRequest request, McmaApiResponse response) { Logger.Debug(nameof(AddBmContentAsync)); Logger.Debug(request.ToMcmaJson().ToString()); var bmContent = request.JsonBody?.ToMcmaObject <BMContent>(); if (bmContent == null) { response.StatusCode = (int)HttpStatusCode.BadRequest; response.StatusMessage = "Missing request body."; return; } var bmContentId = request.StageVariables["PublicUrl"] + "/bm-contents/" + Guid.NewGuid(); bmContent.Id = bmContentId; bmContent.Status = "NEW"; bmContent.DateCreated = DateTime.UtcNow; bmContent.DateModified = bmContent.DateCreated; var table = new DynamoDbTable(request.StageVariables["TableName"]); await table.PutAsync <BMContent>(bmContentId, bmContent); response.StatusCode = (int)HttpStatusCode.Created; response.Headers["Location"] = bmContent.Id; response.JsonBody = bmContent.ToMcmaJson(); Logger.Debug(response.ToMcmaJson().ToString()); }
public static async Task ProcessNotificationAsync(WorkerRequest request, ProcessNotificationRequest @event) { var jobProcessId = @event.JobProcessId; var notification = @event.Notification; var notificationJobData = notification.Content.ToMcmaObject <JobBase>(); var table = new DynamoDbTable <JobProcess>(request.TableName()); var jobProcess = await table.GetAsync(jobProcessId); // not updating job if it already was marked as completed or failed. if (jobProcess.Status == JobStatus.Completed || jobProcess.Status == JobStatus.Failed) { Logger.Warn("Ignoring update of job process that tried to change state from " + jobProcess.Status + " to " + notificationJobData.Status); return; } jobProcess.Status = notificationJobData.Status; jobProcess.StatusMessage = notificationJobData.StatusMessage; jobProcess.Progress = notificationJobData.Progress; jobProcess.JobOutput = notificationJobData.JobOutput; jobProcess.DateModified = DateTime.UtcNow; await table.PutAsync(jobProcessId, jobProcess); var resourceManager = request.GetAwsV4ResourceManager(); await resourceManager.SendNotificationAsync(jobProcess, jobProcess.NotificationEndpoint); }
private static async Task PutJobAssignmentAsync(ResourceManager resourceManager, DynamoDbTable table, string jobAssignmentId, JobAssignment jobAssignment) { jobAssignment.DateModified = DateTime.UtcNow; await table.PutAsync <JobAssignment>(jobAssignmentId, jobAssignment); if (resourceManager != null) { await resourceManager.SendNotificationAsync(jobAssignment, jobAssignment.NotificationEndpoint); } }
public static async Task AddJobAssignmentAsync(ApiGatewayRequest request, McmaApiResponse response) { Logger.Debug(nameof(AddJobAssignmentAsync)); Logger.Debug(request.ToMcmaJson().ToString()); var jobAssignment = request.JsonBody?.ToMcmaObject <JobAssignment>(); if (jobAssignment == null) { response.StatusCode = (int)HttpStatusCode.BadRequest; response.StatusMessage = "Missing request body."; return; } var jobAssignmentId = request.StageVariables["PublicUrl"] + "/job-assignments/" + Guid.NewGuid(); jobAssignment.Id = jobAssignmentId; jobAssignment.Status = "NEW"; jobAssignment.DateCreated = DateTime.UtcNow; jobAssignment.DateModified = jobAssignment.DateCreated; var table = new DynamoDbTable(request.StageVariables["TableName"]); await table.PutAsync <JobAssignment>(jobAssignmentId, jobAssignment); response.StatusCode = (int)HttpStatusCode.Created; response.Headers["Location"] = jobAssignment.Id; response.JsonBody = jobAssignment.ToMcmaJson(); Logger.Debug(response.ToMcmaJson().ToString()); // invoking worker lambda function that will create a jobAssignment assignment for this new jobAssignment var lambdaClient = new AmazonLambdaClient(); var invokeRequest = new InvokeRequest { FunctionName = request.StageVariables["WorkerLambdaFunctionName"], InvocationType = "Event", LogType = "None", Payload = new { action = "ProcessJobAssignment", stageVariables = request.StageVariables, jobAssignmentId = jobAssignmentId }.ToMcmaJson().ToString() }; await lambdaClient.InvokeAsync(invokeRequest); }
internal static async Task CreateJobProcessAsync(JobRepositoryWorkerRequest @event) { var jobId = @event.JobId; var table = new DynamoDbTable(@event.Request.StageVariables["TableName"]); var job = await table.GetAsync <Job>(jobId); var resourceManager = @event.Request.GetAwsV4ResourceManager(); try { var jobProcess = new JobProcess { Job = jobId, NotificationEndpoint = new NotificationEndpoint { HttpEndpoint = jobId + "/notifications" } }; jobProcess = await resourceManager.CreateAsync(jobProcess); job.Status = "QUEUED"; job.JobProcess = jobProcess.Id; } catch (Exception error) { Logger.Error("Failed to create JobProcess."); Logger.Exception(error); job.Status = "FAILED"; job.StatusMessage = $"Failed to create JobProcess due to error '{error}'"; } job.DateModified = DateTime.UtcNow; await table.PutAsync <Job>(jobId, job); await resourceManager.SendNotificationAsync(job, job.NotificationEndpoint); }
public static async Task CreateJobAssignmentAsync(WorkerRequest request, CreateJobAssignmentRequest createRequest) { var resourceManager = request.GetAwsV4ResourceManager(); var table = new DynamoDbTable <JobProcess>(request.TableName()); var jobProcessId = createRequest.JobProcessId; var jobProcess = await table.GetAsync(jobProcessId); try { // retrieving the job var job = await resourceManager.ResolveAsync <Job>(jobProcess.Job); // retrieving the jobProfile var jobProfile = await resourceManager.ResolveAsync <JobProfile>(job.JobProfile); // validating job.JobInput with required input parameters of jobProfile var jobInput = job.JobInput; //await resourceManager.ResolveAsync<JobParameterBag>(job.JobInput); if (jobInput == null) { throw new Exception("Job is missing jobInput"); } if (jobProfile.InputParameters != null) { foreach (var parameter in jobProfile.InputParameters) { if (!jobInput.HasProperty(parameter.ParameterName)) { throw new Exception("jobInput is missing required input parameter '" + parameter.ParameterName + "'"); } } } // finding a service that is capable of handling the job type and job profile var services = await resourceManager.GetAsync <Service>(); Service selectedService = null; ResourceEndpointClient jobAssignmentResourceEndpoint = null; foreach (var service in services) { var serviceClient = new ServiceClient(service, AwsEnvironment.GetDefaultAwsV4AuthProvider()); jobAssignmentResourceEndpoint = null; if (service.JobType == job.Type) { jobAssignmentResourceEndpoint = serviceClient.GetResourceEndpoint <JobAssignment>(); if (jobAssignmentResourceEndpoint == null) { continue; } if (service.JobProfiles != null) { foreach (var serviceJobProfile in service.JobProfiles) { if (serviceJobProfile == job.JobProfile) { selectedService = service; break; } } } } if (selectedService != null) { break; } } if (jobAssignmentResourceEndpoint == null) { throw new Exception("Failed to find service that could execute the " + job.GetType().Name); } var jobAssignment = new JobAssignment { Job = jobProcess.Job, NotificationEndpoint = new NotificationEndpoint { HttpEndpoint = jobProcessId + "/notifications" } }; jobAssignment = await jobAssignmentResourceEndpoint.PostAsync <JobAssignment>(jobAssignment); jobProcess.Status = "SCHEDULED"; jobProcess.JobAssignment = jobAssignment.Id; } catch (Exception error) { Logger.Error("Failed to create job assignment"); Logger.Exception(error); jobProcess.Status = JobStatus.Failed; jobProcess.StatusMessage = error.ToString(); } jobProcess.DateModified = DateTime.UtcNow; await table.PutAsync(jobProcessId, jobProcess); await resourceManager.SendNotificationAsync(jobProcess, jobProcess.NotificationEndpoint); }