Exemplo n.º 1
0
        public bool PingWorkerNode(Uri workerNodeUri)
        {
            var builderHelper = new NodeUriBuilderHelper(workerNodeUri);
            var urijob        = builderHelper.GetPingTemplateUri();
            HttpResponseMessage response;

            try
            {
                response = _httpSender.GetAsync(urijob).Result;
            }
            catch (AggregateException)
            {
                return(false);
            }

            return(response != null && response.IsSuccessStatusCode);
        }
Exemplo n.º 2
0
        private void CancelJobByJobIdWorker(Guid jobId)
        {
            try
            {
                using (var sqlConnection = new SqlConnection(_connectionString))
                {
                    sqlConnection.OpenWithRetry(_retryPolicy);
                    using (var sqlTransaction = sqlConnection.BeginTransaction())
                    {
                        var sentToWorkerNodeUri = _jobRepositoryCommandExecuter.SelectWorkerNode(jobId, sqlConnection, sqlTransaction);
                        if (sentToWorkerNodeUri == null)
                        {
                            return;
                        }

                        var builderHelper = new NodeUriBuilderHelper(sentToWorkerNodeUri);
                        var uriCancel     = builderHelper.GetCancelJobUri(jobId);
                        var response      = _httpSender.DeleteAsync(uriCancel).Result;

                        if (response != null && response.IsSuccessStatusCode)
                        {
                            _jobRepositoryCommandExecuter.UpdateResult(jobId, "Canceled", DateTime.UtcNow, sqlConnection, sqlTransaction);
                            _jobRepositoryCommandExecuter.DeleteJobFromJobQueue(jobId, sqlConnection, sqlTransaction);

                            sqlTransaction.Commit();
                        }
                        else
                        {
                            this.Log().ErrorWithLineNumber("Could not send cancel to node. JobId: " + jobId);
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                this.Log().ErrorWithLineNumber(exp.Message, exp);
                throw;
            }
        }
 public void ShouldThrowExceptionWhenConstructorArgumentStringIsIvalidUri()
 {
     Assert.Throws <UriFormatException>(() => NodeUriBuilderHelperToTest = new NodeUriBuilderHelper("invalid uri"));
 }
 public void ShouldThrowExceptionWhenConstructorArgumentIsStringEmpty()
 {
     Assert.Throws <UriFormatException>(() => NodeUriBuilderHelperToTest = new NodeUriBuilderHelper(string.Empty));
 }
        public void ShouldReturnCorrectJobTemplateUri()
        {
            NodeUriBuilderHelperToTest = new NodeUriBuilderHelper(UriToTest);

            Assert.IsTrue(NodeUriBuilderHelperToTest.GetJobTemplateUri() == JobTemplateUri);
        }
        public void ShouldReturnCorrectCancelJobUri()
        {
            NodeUriBuilderHelperToTest = new NodeUriBuilderHelper(UriToTest);

            Assert.IsTrue(NodeUriBuilderHelperToTest.GetCancelJobUri(Guid) == CancelJobUri);
        }
        public void ShouldInstantiateWhenConstructorArgumentStringIsIvalidUri()
        {
            NodeUriBuilderHelperToTest = new NodeUriBuilderHelper(UriToTest);

            Assert.IsNotNull(NodeUriBuilderHelperToTest);
        }
Exemplo n.º 8
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();
                            }
                        }
                    }
                }
            }
        }