public void ShouldReturnCorrectJobTemplateUri()
        {
            NodeUriBuilderHelperToTest = new NodeUriBuilderHelper(UriToTest);

            Assert.IsTrue(NodeUriBuilderHelperToTest.GetJobTemplateUri() == JobTemplateUri);
        }
Exemplo n.º 2
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();
                            }
                        }
                    }
                }
            }
        }