public void Cancel(JobCancelMessage jobCancelMessage) { Trace.Info("Job cancellation request {0} received.", jobCancelMessage.JobId); JobDispatcherItem worker; lock (_lock) { if (!_jobsInProgress.TryGetValue(jobCancelMessage.JobId, out worker)) { Trace.Error("Received cancellation for invalid job id {0}.", jobCancelMessage.JobId); } else { worker.Token.Cancel(); } } }
public async void TestCancel() { //Arrange using (var hc = new TestHostContext(this)) using (var workerManager = new WorkerManager()) { hc.EnqueueInstance <IJobDispatcher>(_jobDispatcher.Object); workerManager.Initialize(hc); JobRequestMessage jobMessage = CreateJobRequestMessage(); JobCancelMessage cancelMessage = CreateJobCancelMessage(jobMessage.JobId); bool started = false; Task jobTask = null; _jobDispatcher.Setup(x => x.RunAsync(jobMessage, It.IsAny <CancellationToken>())) .Returns(async(JobRequestMessage message, CancellationToken token) => { jobTask = Task.Delay(5000, token); started = true; await jobTask; return(0); }); workerManager.Run(jobMessage); int i = 20; while (i > 0 && (!started)) { await Task.Delay(10); i--; } Assert.True(started); //Act //send cancel message workerManager.Cancel(cancelMessage); //Assert //wait up to 2 sec for cancellation to be processed Task[] taskToWait = { jobTask, Task.Delay(2000) }; await Task.WhenAny(taskToWait); _jobDispatcher.Verify(x => x.RunAsync(jobMessage, It.IsAny <CancellationToken>()), $"{nameof(_jobDispatcher.Object.RunAsync)} not invoked"); Assert.True(jobTask.IsCompleted); Assert.True(jobTask.IsCanceled); } }
public bool Cancel(JobCancelMessage jobCancelMessage) { Trace.Info($"Job cancellation request {jobCancelMessage.JobId} received."); WorkerDispatcher workerDispatcher; if (!_jobInfos.TryGetValue(jobCancelMessage.JobId, out workerDispatcher)) { Trace.Verbose($"Job request {jobCancelMessage.JobId} is not a current running job, ignore cancllation request."); return false; } else { if (workerDispatcher.Cancel()) { Trace.Verbose($"Fired cancellation token for job request {workerDispatcher.JobId}."); } return true; } }
public bool Cancel(JobCancelMessage jobCancelMessage) { Trace.Info($"Job cancellation request {jobCancelMessage.JobId} received."); WorkerDispatcher workerDispatcher; if (!_jobInfos.TryGetValue(jobCancelMessage.JobId, out workerDispatcher)) { Trace.Verbose($"Job request {jobCancelMessage.JobId} is not a current running job, ignore cancllation request."); return(false); } else { if (workerDispatcher.Cancel()) { Trace.Verbose($"Fired cancellation token for job request {workerDispatcher.JobId}."); } return(true); } }
public bool Cancel(JobCancelMessage jobCancelMessage) { ArgUtil.NotNull(jobCancelMessage, nameof(jobCancelMessage)); Trace.Info($"Job cancellation request {jobCancelMessage.JobId} received, cancellation timeout {jobCancelMessage.Timeout.TotalMinutes} minutes."); WorkerDispatcher workerDispatcher; if (!_jobInfos.TryGetValue(jobCancelMessage.JobId, out workerDispatcher)) { Trace.Verbose($"Job request {jobCancelMessage.JobId} is not a current running job, ignore cancllation request."); return(false); } else { if (workerDispatcher.Cancel(jobCancelMessage.Timeout)) { Trace.Verbose($"Fired cancellation token for job request {workerDispatcher.JobId}."); } return(true); } }
private JobCancelMessage CreateJobCancelMessage() { var message = new JobCancelMessage(Guid.NewGuid(), TimeSpan.FromSeconds(0)); return(message); }
private JobCancelMessage CreateJobCancelMessage(Guid jobId) { var message = new JobCancelMessage(jobId, TimeSpan.FromSeconds(0)); return(message); }
private JobCancelMessage CreateJobCancelMessage() { var message = new JobCancelMessage(Guid.NewGuid(), TimeSpan.FromSeconds(0)); return message; }