protected override Task<HttpResponseMessage> TrySendStatus(JobQueueItemEntity jobQueueItemEntity, CancellationToken cancellationToken) { NumberOfTimeCalled++; Wait.Set(); var response = new HttpResponseMessage(HttpStatusCode.OK); var request = new HttpRequestMessage(HttpMethod.Post, "JobDone"); response.RequestMessage = request; return Task.FromResult(response); }
protected override Task<HttpResponseMessage> TrySendStatus(JobQueueItemEntity jobQueueItemEntity, CancellationToken cancellationToken) { InvokeTriggerTrySendStatusSucceded(); Wait.Set(); var response = new HttpResponseMessage(HttpStatusCode.OK); var request = new HttpRequestMessage(HttpMethod.Post, "JobDoneTrigger"); response.RequestMessage = request; return Task.FromResult(response); }
public HttpResponseMessage ValidateStartJob(JobQueueItemEntity jobQueueItemEntity) { lock (_startJobLock) { if (_currentMessageToProcess != null) { var responseMsg = new HttpResponseMessage(HttpStatusCode.Conflict); responseMsg.Content = new StringContent(WorkerIsAlreadyWorking); return responseMsg; } if (jobQueueItemEntity == null) { return CreateBadRequest(JobToDoIsNull); } if (jobQueueItemEntity.JobId == Guid.Empty) { return CreateBadRequest(JobToDoIdIsInvalid); } if (string.IsNullOrEmpty(jobQueueItemEntity.Name)) { return CreateBadRequest(JobToDoNameIsInvalid); } if (string.IsNullOrEmpty(jobQueueItemEntity.Type)) { return CreateBadRequest(JobToDoTypeIsNullOrEmpty); } var type = _nodeConfiguration.HandlerAssembly.GetType(jobQueueItemEntity.Type); if (type == null) { Logger.WarningWithLineNumber(string.Format( WhoamI + JobToDoTypeCanNotBeResolved, jobQueueItemEntity.Type)); return CreateBadRequest(string.Format(JobToDoTypeCanNotBeResolved, jobQueueItemEntity.Type)); } try { JsonConvert.DeserializeObject(jobQueueItemEntity.Serialized, type); } catch (Exception) { return CreateBadRequest(JobToDoCanNotBeDeserialize); } _currentMessageToProcess = jobQueueItemEntity; return new HttpResponseMessage(HttpStatusCode.OK); } }
protected virtual async Task<HttpResponseMessage> TrySendStatus(JobQueueItemEntity jobQueueItemEntity, CancellationToken cancellationToken) { try { //Use ManagerUriBuilderHelper instead? var uri = new Uri(CallbackTemplateUri.ToString().Replace(ManagerRouteConstants.JobIdOptionalParameter, jobQueueItemEntity.JobId.ToString())); var httpResponseMessage = await _httpSender.PostAsync(uri, null, cancellationToken); return httpResponseMessage; } catch (Exception exp) { Logger.ErrorWithLineNumber("Error in TrySendStatus.", exp); throw; } }
public void Setup() { var parameters = new TestJobParams("Test Job", TimeSpan.FromSeconds(1)); var ser = JsonConvert.SerializeObject(parameters); _jobDefinition = new JobQueueItemEntity { JobId = Guid.NewGuid(), Name = "jobDefinition Name", Serialized = ser, Type = "NodeTest.JobHandlers.TestJobParams" }; _nodeStartupNotification = new NodeStartupNotificationToManagerFake(_nodeConfigurationFake, new FakeHttpSender()); _trySendJobDetailToManagerTimerFake = new TrySendJobDetailToManagerTimerFake(_nodeConfigurationFake, new FakeHttpSender(), 1000); _pingToManagerFake = new PingToManagerFake(); _sendJobDoneTimer = new SendJobDoneTimerFake(_nodeConfigurationFake, _trySendJobDetailToManagerTimerFake, new FakeHttpSender()); _sendJobCanceledTimer = new SendJobCanceledTimerFake(_nodeConfigurationFake, _trySendJobDetailToManagerTimerFake, new FakeHttpSender()); _sendJobFaultedTimer = new SendJobFaultedTimerFake(_nodeConfigurationFake, _trySendJobDetailToManagerTimerFake, new FakeHttpSender()); }
protected override Task<HttpResponseMessage> TrySendStatus(JobQueueItemEntity jobQueueItemEntity, CancellationToken cancellationToken) { try { var payload = new JobFailedEntity { JobId = jobQueueItemEntity.JobId, AggregateException = AggregateExceptionToSend, Created = ErrorOccured }; var response = _httpSender.PostAsync(CallbackTemplateUri, payload, cancellationToken); return response; } catch (Exception exp) { Logger.ErrorWithLineNumber("Error in TrySendJobFaultedTimer.", exp); throw; } }
public IHttpActionResult PrepareToStartJob(JobQueueItemEntity jobQueueItemEntity) { var isValidRequest = _workerWrapper.ValidateStartJob(jobQueueItemEntity); if (!isValidRequest.IsSuccessStatusCode) { return ResponseMessage(isValidRequest); } Task.Factory.StartNew(() => { var task=_workerWrapper.CreateTimeoutCurrentMessageTask(jobQueueItemEntity); try { task.Start(); } catch (Exception) { } }); return Ok(); }
private void SetNodeStatusTimer(TrySendStatusToManagerTimer newTrySendStatusToManagerTimer, JobQueueItemEntity jobQueueItemEntity) { // Stop and dispose old timer. if (_trySendStatusToManagerTimer != null) { _trySendStatusToManagerTimer.Stop(); // Remove event handler. _trySendStatusToManagerTimer.TrySendStatusSucceded -= TrySendStatusToManagerTimer_TrySendStatusSucceded; _trySendStatusToManagerTimer = null; } // Set new timer, if exists. if (newTrySendStatusToManagerTimer != null) { _trySendStatusToManagerTimer = newTrySendStatusToManagerTimer; _trySendStatusToManagerTimer.JobQueueItemEntity = jobQueueItemEntity; _trySendStatusToManagerTimer.TrySendStatusSucceded += TrySendStatusToManagerTimer_TrySendStatusSucceded; _trySendStatusToManagerTimer.Start(); } else { _trySendStatusToManagerTimer = null; } }
public void ResetCurrentMessage() { _currentMessageToProcess = null; }
public void StartJob(JobQueueItemEntity jobQueueItemEntity) { if (isWorking) return; isWorking = true; CancellationTokenSource = new CancellationTokenSource(); _currentMessageToProcess = jobQueueItemEntity; var typ = _nodeConfiguration.HandlerAssembly.GetType(jobQueueItemEntity.Type); var deSer = JsonConvert.DeserializeObject(jobQueueItemEntity.Serialized, typ); //----------------------------------------------------- // Clear faulted timer. //----------------------------------------------------- var faultedTimer = _trySendJobFaultedStatusToManagerTimer as TrySendJobFaultedToManagerTimer; if (faultedTimer != null) { faultedTimer.AggregateExceptionToSend = null; faultedTimer.ErrorOccured = null; } //---------------------------------------------------- // Define task. //---------------------------------------------------- Task = new Task(() => { _handler.Invoke(deSer, CancellationTokenSource, SendJobProgressToManager); }, CancellationTokenSource.Token); Task.ContinueWith(t => { switch (t.Status) { case TaskStatus.RanToCompletion: SetNodeStatusTimer(_trySendJobDoneStatusToManagerTimer, _currentMessageToProcess); break; case TaskStatus.Canceled: SetNodeStatusTimer(_trySendJobCanceledStatusToManagerTimer, _currentMessageToProcess); break; case TaskStatus.Faulted: if (faultedTimer != null) { faultedTimer.AggregateExceptionToSend = t.Exception; faultedTimer.ErrorOccured = DateTime.UtcNow; } if (t.Exception != null) { Logger.ErrorWithLineNumber("Failed", t.Exception); } SetNodeStatusTimer(_trySendJobFaultedStatusToManagerTimer, _currentMessageToProcess); break; } }, TaskContinuationOptions.LongRunning) .ContinueWith(t => { isWorking = false; }); Task.Start(); }
public Task CreateTimeoutCurrentMessageTask(JobQueueItemEntity jobQueueItemEntity) { _currentMessageToProcess = jobQueueItemEntity; CurrentMessageTimeoutTaskCancellationTokenSource = new CancellationTokenSource(); return new Task(() => { var stopwatch = new Stopwatch(); stopwatch.Start(); while (stopwatch.Elapsed.Seconds <= 60) { if (IsCancellationRequested) { CurrentMessageTimeoutTaskCancellationTokenSource.Token.ThrowIfCancellationRequested(); } Thread.Sleep(TimeSpan.FromSeconds(5)); } // Current message timed out. _currentMessageToProcess = null; }, CurrentMessageTimeoutTaskCancellationTokenSource.Token); }
public void PrepareToStartJobShouldReturnConflictWhenAlreadyProcessingJob() { _workerWrapper = new WorkerWrapper(new ShortRunningInvokeHandlerFake(), _nodeConfigurationFake, _nodeStartupNotification, _pingToManagerFake, _sendJobDoneTimer, _sendJobCanceledTimer, _sendJobFaultedTimer, _trySendJobDetailToManagerTimerFake); _nodeController = new NodeController(_workerWrapper, _nodeConfigurationFake) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; var parameters = new TestJobParams("Test Job", TimeSpan.FromSeconds(1)); var ser = JsonConvert.SerializeObject(parameters); var jobToDo2 = new JobQueueItemEntity { JobId = Guid.NewGuid(), Name = "Another name", Serialized = ser, Type = "NodeTest.JobHandlers.TestJobParams" }; _nodeController.PrepareToStartJob(_jobQueueItemEntity); var actionResult = _nodeController.PrepareToStartJob(jobToDo2); Assert.IsTrue(actionResult.ExecuteAsync(new CancellationToken()) .Result.StatusCode == HttpStatusCode.Conflict); }