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); }
internal static async Task ProcessNotificationAsync(WorkerRequest @event, ProcessNotificationRequest notificationRequest) { var jobId = notificationRequest.JobId; var notification = notificationRequest.Notification; var notificationJob = notification.Content.ToMcmaObject <JobBase>(); var table = new DynamoDbTable <Job>(@event.TableName()); var job = await table.GetAsync(jobId); // not updating job if it already was marked as completed or failed. if (job.Status == JobStatus.Completed || job.Status == JobStatus.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(jobId, job); var resourceManager = @event.GetAwsV4ResourceManager(); await resourceManager.SendNotificationAsync(job, job.NotificationEndpoint); }
public static async Task DeleteJobAssignmentAsync(WorkerRequest request, DeleteJobAssignmentRequest deleteRequest) { var jobAssignmentId = deleteRequest.JobAssignmentId; try { var resourceManager = request.GetAwsV4ResourceManager(); await resourceManager.DeleteAsync <JobAssignment>(jobAssignmentId); } catch (Exception error) { Logger.Exception(error); } }
internal static async Task CreateJobProcessAsync(WorkerRequest @event, CreateJobProcessRequest createRequest) { var jobId = createRequest.JobId; var table = new DynamoDbTable <Job>(@event.TableName()); var job = await table.GetAsync(jobId); var resourceManager = @event.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 = JobStatus.Failed; job.StatusMessage = $"Failed to create JobProcess due to error '{error}'"; } job.DateModified = DateTime.UtcNow; await table.PutAsync(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); }