Exemplo n.º 1
0
        private void SetNodeStatusTimer(TrySendStatusToManagerTimer newTrySendStatusToManagerTimer,
                                        JobQueueItemEntity jobQueueItemEntity)
        {
            // Stop and dispose old timer.
            if (_trySendStatusToManagerTimer != null)
            {
                _trySendStatusToManagerTimer.Stop();

                // Remove event handler.
                _trySendStatusToManagerTimer.TrySendStatusSucceded -=
                    TrySendStatusToManagerTimer_TrySendStatus;

                _trySendStatusToManagerTimer = null;
            }

            // Set new timer, if exists.
            if (newTrySendStatusToManagerTimer != null)
            {
                _trySendStatusToManagerTimer = newTrySendStatusToManagerTimer;

                _trySendStatusToManagerTimer.JobQueueItemEntity = jobQueueItemEntity;

                _trySendStatusToManagerTimer.TrySendStatusSucceded +=
                    TrySendStatusToManagerTimer_TrySendStatus;

                _trySendStatusToManagerTimer.Start();
            }
            else
            {
                _trySendStatusToManagerTimer = null;
            }
        }
Exemplo n.º 2
0
        public IHttpActionResult PrepareToStartJob(JobQueueItemEntity jobQueueItemEntity)
        {
            var isValidRequest = _workerWrapper.ValidateStartJob(jobQueueItemEntity);

            if (!isValidRequest.IsSuccessStatusCode)
            {
                return(ResponseMessage(isValidRequest));
            }
            if (_workerWrapper.IsWorking)
            {
                return(Conflict());
            }
            Task.Factory.StartNew(() =>
            {
                try
                {
                    var stopwatch = new Stopwatch();
                    stopwatch.Start();

                    while (stopwatch.Elapsed.Seconds <= 60 && !_workerWrapper.IsWorking)
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(200));
                    }
                }
                catch
                {
                    // ignored
                }
            });
            return(Ok());
        }
Exemplo n.º 3
0
        public void Setup()
        {
            var parameters = new TestJobParams("Test Job",
                                               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());
            _jobDetailSender = new JobDetailSender(_nodeConfigurationFake, new FakeHttpSender());
            _trySendJobDetailToManagerTimer =
                new TrySendJobDetailToManagerTimer(_nodeConfigurationFake,
                                                   _jobDetailSender);

            _pingToManagerFake = new PingToManagerFake();

            _sendJobDoneTimer = new SendJobDoneTimerFake(_nodeConfigurationFake,
                                                         _jobDetailSender,
                                                         new FakeHttpSender());

            _sendJobCanceledTimer = new SendJobCanceledTimerFake(_nodeConfigurationFake,
                                                                 _jobDetailSender,
                                                                 new FakeHttpSender());

            _sendJobFaultedTimer = new SendJobFaultedTimerFake(_nodeConfigurationFake,
                                                               _jobDetailSender,
                                                               new FakeHttpSender());
        }
Exemplo n.º 4
0
 private void TrySendStatusToManagerTimer_TrySendStatus(object sender,
                                                        EventArgs e)
 {
     // Dispose timer.
     SetNodeStatusTimer(null, null);
     _currentMessageToProcess = null;
     IsWorking = false;
 }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
        public HttpResponseMessage ValidateStartJob(JobQueueItemEntity jobQueueItemEntity)
        {
            lock (_startJobLock)
            {
                if (_currentMessageToProcess != null || IsWorking)
                {
                    var responseMsg = new HttpResponseMessage(HttpStatusCode.Conflict)
                    {
                        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 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));
        }
Exemplo n.º 8
0
        public void PrepareToStartJobShouldReturnConflictWhenAlreadyProcessingJob()
        {
            _workerWrapper = new WorkerWrapper(new ShortRunningInvokeHandlerFake(),
                                               _nodeConfigurationFake,
                                               _nodeStartupNotification,
                                               _pingToManagerFake,
                                               _sendJobDoneTimer,
                                               _sendJobCanceledTimer,
                                               _sendJobFaultedTimer,
                                               _trySendJobDetailToManagerTimer,
                                               _jobDetailSender);

            _nodeController = new NodeController(_workerWrapper)
            {
                Request       = new HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };

            var parameters = new TestJobParams("Test Job",
                                               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);
        }
        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 exception)
            {
                _exceptionLoggerHandler.LogError("Error in TrySendJobFaultedTimer.", exception);
                throw;
            }
        }
        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)
            {
                var currentScopeMessage =
                    LoggerExtensions.GetFormattedLogMessage("Error in TrySendStatus.");
                _exceptionLoggerHandler.LogError(currentScopeMessage, exp);
                throw;
            }
        }
Exemplo n.º 11
0
 public void ResetCurrentMessage()
 {
     _currentMessageToProcess = null;
 }
Exemplo n.º 12
0
        public void StartJob(JobQueueItemEntity jobQueueItemEntity)
        {
            if (IsWorking)
            {
                return;
            }
            IsWorking = true;
            CancellationTokenSource = new CancellationTokenSource();
            IEnumerable <object> returnObjects = null;

            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,
                                ref returnObjects);
            },
                            CancellationTokenSource.Token);

            Task.ContinueWith(t =>
            {
                switch (t.Status)
                {
                case TaskStatus.RanToCompletion:

                    SetNodeStatusTimer(_trySendJobDoneStatusToManagerTimer,
                                       _currentMessageToProcess);

                    if (returnObjects != null)
                    {
                        SpinWait.SpinUntil(() => _currentMessageToProcess == null);
                        LoopReturnObjects(returnObjects);
                    }

                    break;


                case TaskStatus.Canceled:

                    SetNodeStatusTimer(_trySendJobCanceledStatusToManagerTimer,
                                       _currentMessageToProcess);

                    if (returnObjects != null)
                    {
                        SpinWait.SpinUntil(() => _currentMessageToProcess == null);
                        LoopReturnObjects(returnObjects);
                    }

                    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);

                    if (returnObjects != null)
                    {
                        SpinWait.SpinUntil(() => _currentMessageToProcess == null);
                        LoopReturnObjects(returnObjects);
                    }

                    break;
                }
            }, TaskContinuationOptions.LongRunning);

            Task.Start();
        }