public void StartShallRunThrough() { var mockClient = new Mock <IJobsClient>(); var mockLogger = new Mock <ILogger <ClaraJobsApi> >(); var job = new Job { JobId = "valid-job-id", PayloadId = "payloadid" }; JobId.TryParse(job.JobId, out JobId jobId); mockClient.Setup(p => p.StartJob(It.IsAny <JobId>(), It.IsAny <List <KeyValuePair <string, string> > >())) .ReturnsAsync(new JobToken { JobId = jobId, JobState = JobState.Pending, JobStatus = Nvidia.Clara.Platform.JobStatus.Healthy }); var service = new ClaraJobsApi( mockClient.Object, mockLogger.Object); service.Start(job).Wait(); mockClient.Verify( p => p.StartJob(It.IsAny <JobId>(), It.IsAny <List <KeyValuePair <string, string> > >()), Times.Exactly(1)); mockLogger.VerifyLogging(LogLevel.Error, Times.Never()); mockLogger.VerifyLogging(LogLevel.Information, Times.Once()); }
public async Task Create_ShallReturnAJob() { var mockClient = new Mock <IJobsClient>(); var mockLogger = new Mock <ILogger <ClaraJobsApi> >(); JobId.TryParse("jobid", out JobId jobId); PayloadId.TryParse("payloadid", out PayloadId payloadId); PipelineId.TryParse("pipelineid", out PipelineId pipelineId); mockClient.Setup(p => p.CreateJob(It.IsAny <PipelineId>(), It.IsAny <string>(), It.IsAny <JobPriority>())) .ReturnsAsync(new JobInfo { Name = "bla bla job", JobId = jobId, PayloadId = payloadId, PipelineId = pipelineId }); var service = new ClaraJobsApi( mockClient.Object, mockLogger.Object); var job = await service.Create(Guid.NewGuid().ToString(), "bla bla", JobPriority.Higher); Assert.Equal(jobId.ToString(), job.JobId); Assert.Equal(payloadId.ToString(), job.PayloadId); mockClient.Verify( p => p.CreateJob(It.IsAny <PipelineId>(), It.IsAny <string>(), JobPriority.Higher), Times.Exactly(1)); mockLogger.VerifyLogging(LogLevel.Information, Times.Once()); mockLogger.VerifyLogging(LogLevel.Error, Times.Never()); }
public void AddMetadata_ShallRunThrough() { var mockClient = new Mock <IJobsClient>(); var mockLogger = new Mock <ILogger <ClaraJobsApi> >(); var jobId = Guid.NewGuid().ToString("N"); var jobDate = DateTime.UtcNow; JobId.TryParse(jobId, out JobId jobIdObj); mockClient.Setup(p => p.AddMetadata(It.IsAny <JobId>(), It.IsAny <Dictionary <string, string> >())); var service = new ClaraJobsApi( mockClient.Object, mockLogger.Object); var job = new Job { JobId = Guid.NewGuid().ToString("N"), PayloadId = Guid.NewGuid().ToString("N") }; service.AddMetadata(job, new Dictionary <string, string>()).Wait(); mockClient.Verify( p => p.AddMetadata(It.IsAny <JobId>(), It.IsAny <Dictionary <string, string> >()), Times.Exactly(1)); mockLogger.VerifyLogging(LogLevel.Error, Times.Never()); mockLogger.VerifyLogging(LogLevel.Information, Times.Once()); }
public void StatusShallRunThrough() { var mockClient = new Mock <IJobsClient>(); var mockLogger = new Mock <ILogger <ClaraJobsApi> >(); var jobId = Guid.NewGuid().ToString("N"); var jobDate = DateTime.UtcNow; JobId.TryParse(jobId, out JobId jobIdObj); mockClient.Setup(p => p.GetStatus(It.IsAny <JobId>())) .ReturnsAsync(new JobDetails { JobId = jobIdObj, JobState = JobState.Pending, JobStatus = Nvidia.Clara.Platform.JobStatus.Healthy, DateCreated = jobDate, DateStarted = jobDate, DateStopped = jobDate, JobPriority = JobPriority.Higher, Name = "name" }); var service = new ClaraJobsApi( mockClient.Object, mockLogger.Object); service.Status(jobId).Wait(); mockClient.Verify( p => p.GetStatus(It.IsAny <JobId>()), Times.Exactly(1)); mockLogger.VerifyLogging(LogLevel.Error, Times.Never()); mockLogger.VerifyLogging(LogLevel.Information, Times.Once()); }
public async Task AddMetadata(Job job, IDictionary <string, string> metadata) { await Policy.Handle <Exception>() .WaitAndRetryAsync( 1, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, retryCount, context) => { _logger.Log(LogLevel.Error, exception, "Exception while adding job metadata."); }) .ExecuteAsync(async() => { if (!JobId.TryParse(job.JobId, out JobId jobId)) { throw new ArgumentException($"Invalid JobId provided: {job.JobId}"); } var response = await _jobsClient.AddMetadata(jobId, metadata); _logger.Log(LogLevel.Information, "Clara Job.AddMetadata API called successfully."); }).ConfigureAwait(false); }
public async Task <JobDetails> Status(string jobId) { return(await Policy.Handle <Exception>() .WaitAndRetryAsync( 1, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, retryCount, context) => { _logger.Log(LogLevel.Error, exception, "Exception while checking statuf of a job."); }) .ExecuteAsync(async() => { if (!JobId.TryParse(jobId, out JobId jobIdObj)) { throw new ArgumentException($"Invalid JobId provided: {jobId}"); } var response = await _jobsClient.GetStatus(jobIdObj); _logger.Log(LogLevel.Information, "Clara Job.GetStatus API called successfully."); return response; }).ConfigureAwait(false)); }
public async Task Start(Job job) { await Policy.Handle <Exception>() .WaitAndRetryAsync( 3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, retryCount, context) => { _logger.Log(LogLevel.Error, "Exception while starting a new job: {exception}", exception); }) .ExecuteAsync(async() => { if (!JobId.TryParse(job.JobId, out JobId jobId)) { throw new ApplicationException($"Invalid JobId provided: {job.JobId}"); } var response = await _jobsClient.StartJob(jobId, null); _logger.Log(LogLevel.Information, "Clara Job.Start API called successfully with state={0}, status={1}", response.JobState, response.JobStatus); }).ConfigureAwait(false); }