Пример #1
0
 public void Send(CancellationToken cancellationToken)
 {
     if (_jobDetails == null || _jobDetails.Count <= 0)
     {
         return;
     }
     lock (_jobDetails)
     {
         try
         {
             var task = Task.Factory.StartNew(async() =>
             {
                 var httpResponseMessage = await _httpSender.PostAsync(_uriBuilder.Uri, _jobDetails, cancellationToken);
                 if (httpResponseMessage.IsSuccessStatusCode)
                 {
                     //detail.Sent = true;
                 }
             }, cancellationToken);
             task.Wait(cancellationToken);
             _jobDetails.Clear();
         }
         catch (Exception)
         {
             Logger.ErrorWithLineNumber($"Send job progresses to manager failed for job ( jobId ) : ( {_jobDetails.FirstOrDefault().JobId} )");
         }
     }
 }
Пример #2
0
        private async Task <HttpResponseMessage> SendPing(Uri nodeAddress,
                                                          Uri callbackToManagerUri,
                                                          CancellationToken cancellationToken)
        {
            var httpResponseMessage = await _httpSender.PostAsync(callbackToManagerUri,
                                                                  nodeAddress,
                                                                  cancellationToken);

            return(httpResponseMessage);
        }
        public virtual async Task <HttpResponseMessage> TrySendNodeStartUpToManager(Uri nodeAddress,
                                                                                    Uri callbackToManagerUri,
                                                                                    CancellationToken cancellationToken)
        {
            var httpResponseMessage = await _httpSender.PostAsync(callbackToManagerUri,
                                                                  nodeAddress,
                                                                  cancellationToken);

            return(httpResponseMessage);
        }
        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;
            }
        }
Пример #6
0
        private void AssignJobToWorkerNodeWorker(Uri availableNode)
        {
            ManagerLogger.Info("starting the assignment process");
            lock (_assigningJob)
            {
                using (var sqlConnection = new SqlConnection(_connectionString))
                {
                    var          responseCodeOk = false;
                    JobQueueItem jobQueueItem   = null;
                    try
                    {
                        sqlConnection.OpenWithRetry(_retryPolicy);
                        jobQueueItem = _jobRepositoryCommandExecuter.AcquireJobQueueItem(sqlConnection);
                        if (jobQueueItem == null)
                        {
                            sqlConnection.Close();
                            ManagerLogger.Info("no job acquired for node " + availableNode);
                            return;
                        }

                        ManagerLogger.Info("acquired job with id  " + jobQueueItem.JobId + " for node " + availableNode);
                        var builderHelper = new NodeUriBuilderHelper(availableNode);
                        var urijob        = builderHelper.GetJobTemplateUri();
                        ManagerLogger.Info("posting the job to the node");
                        HttpResponseMessage response = null;
                        try
                        {
                            response = _httpSender.PostAsync(urijob, jobQueueItem).Result;
                        }
                        catch (AggregateException aggregateException)
                        {
                            var nodeNotFound =
                                aggregateException.InnerExceptions.FirstOrDefault(e => e is HttpRequestException)?.InnerException is
                                WebException;

                            if (!nodeNotFound)
                            {
                                throw;
                            }
                            ManagerLogger.Info($"Send job to node:{availableNode} failed. {aggregateException.Message}", aggregateException);
                            return;
                        }

                        ManagerLogger.Info(response?.ReasonPhrase + " response from the node");
                        responseCodeOk = response != null &&
                                         (response.IsSuccessStatusCode || response.StatusCode.Equals(HttpStatusCode.BadRequest));
                        if (responseCodeOk)
                        {
                            var sentToWorkerNodeUri = availableNode.ToString();
                            ManagerLogger.Info("node is ok fix the db now");
                            using (var sqlTransaction = sqlConnection.BeginTransaction())
                            {
                                _jobRepositoryCommandExecuter.InsertIntoJob(jobQueueItem, sentToWorkerNodeUri, sqlConnection, sqlTransaction);
                                _jobRepositoryCommandExecuter.DeleteJobFromJobQueue(jobQueueItem.JobId, sqlConnection, sqlTransaction);
                                _jobRepositoryCommandExecuter.InsertJobDetail(jobQueueItem.JobId, "Job Started", sqlConnection, sqlTransaction);
                                sqlTransaction.Commit();
                            }

                            if (!response.IsSuccessStatusCode)
                            {
                                return;
                            }

                            urijob = builderHelper.GetUpdateJobUri(jobQueueItem.JobId);
                            //what should happen if this response is not 200?
                            ManagerLogger.Info("asking the node to start the job");
                            _httpSender.PutAsync(urijob, null);
                        }
                        else
                        {
                            ManagerLogger.Info("response from the node was not ok " + response?.ReasonPhrase);
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Log().Info($"Failed for node {availableNode} {ex.Message}");
                        throw;
                    }
                    finally
                    {
                        if (!responseCodeOk && jobQueueItem != null)
                        {
                            using (var sqlTransaction = sqlConnection.BeginTransaction())
                            {
                                _jobRepositoryCommandExecuter.TagQueueItem(jobQueueItem.JobId, sqlConnection, sqlTransaction);
                                sqlTransaction.Commit();
                            }
                        }
                    }
                }
            }
        }