예제 #1
0
 /// <summary>
 /// Get the number of execute requests that have no response yet, and that
 /// we can reasonably expect one for (meaning, not abandoned)
 /// </summary>
 /// <returns></returns>
 public int GetPendingExecuteCount()
 {
     lock (ExecuteLogSync)
     {
         return(ExecuteLog.Count(x => (!x.Value.Complete && !x.Value.Abandoned)));
     }
 }
예제 #2
0
        /// <summary>
        /// Is there an execution error that we have tracked in our execution log.  We also
        /// consider abandonment as a form of error.
        /// </summary>
        /// <returns>true if there is at least one execution error, false otherwise</returns>
        public bool HasExecuteError()
        {
            lock (ExecuteLogSync)
            {
                // If we have nothing in the execution log, there can't be an error.
                if (ExecuteLog == null || ExecuteLog.Count == 0)
                {
                    return(false);
                }

                return(ExecuteLog.Any(
                           x => x.Value.Abandoned || x.Value.Response.Any(y => y.HasError())));
            }
        }
예제 #3
0
        /// <summary>
        /// 获取数据表集合
        /// </summary>
        /// <returns></returns>
        public DataSet XmlReadDataSet()
        {
            DataSet dataSet = new DataSet();

            foreach (var config in configs)
            {
                current = config;
                try
                {
                    dataSet.Tables.Add(XmlReadDataTable());
                }
                catch (Exception ex)
                {
                    ExecuteLog.Add($"[ReadDataTable]:[{ex.Message}]");
                }
            }
            return(dataSet);
        }
예제 #4
0
        /// <summary>
        /// Return a formatted string containing all of the error messages
        /// </summary>
        /// <returns></returns>
        public List <string> GetExecuteErrors()
        {
            lock (ExecuteLogSync)
            {
                // If we have nothing in the execution log, there can't be an error.
                if (ExecuteLog == null || ExecuteLog.Count == 0)
                {
                    return(null);
                }

                var errors =
                    ExecuteLog.SelectMany(x => x.Value.Response.Select(y => y.GetError()))
                    .Where(x => !string.IsNullOrWhiteSpace(x))
                    .ToList();
                if (ExecuteLog.Any(x => x.Value.Abandoned))
                {
                    errors.Add(ABANDONED_CODE_ERROR_MESSAGE);
                }
                return(errors);
            }
        }
예제 #5
0
        /// <summary>
        /// Send a chunk of code (it can be more than one command, separated by delimiters and/or
        /// newlines) to the kernel.  Because of the asynchronous nature of execution, this merely
        /// sends off the code to be executed - there is no immediate response.
        /// </summary>
        /// <param name="code"></param>
        public void Execute(string code)
        {
            var message = ClientSession.CreateMessage(MessageType.ExecuteRequest);

            message.Content               = new ExpandoObject();
            message.Content.code          = code;
            message.Content.silent        = false;
            message.Content.store_history = true;
            message.Content.allow_stdin   = false;
            message.Content.stop_on_error = true;
            _ShellChannel.Send(message);

            // We start tracking an execute request once it is sent on the shell channel.
            lock (ExecuteLogSync)
            {
                ExecuteLog.Add(message.Header.Id, new ExecutionEntry()
                {
                    Request = message
                });
            }
        }
예제 #6
0
        private void Run(DelayTask task)
        {
            var hc = _httpClientFactory.CreateClient();

            hc.Timeout = task.TimeoutSeconds == 0
                ? TimeSpan.FromSeconds(15)
                : TimeSpan.FromSeconds(task.TimeoutSeconds);

            Task <HttpResponseMessage> requestTask;

            if (task.Method.Equals("GET", StringComparison.OrdinalIgnoreCase))
            {
                requestTask = hc.GetAsync(task.Url);
            }
            else
            {
                StringContent stringContent = new StringContent(task.PostData, Encoding.UTF8);
                requestTask = hc.PostAsync(task.Url, stringContent);
            }

            requestTask.ContinueWith(reqTask =>
            {
                var log = new ExecuteLog()
                {
                    PostData   = task.PostData,
                    TaskId     = task.Id,
                    TaskName   = task.Name,
                    TaskUrl    = task.Url,
                    TaskType   = 2,
                    TaskMethod = task.Method,
                };

                if (reqTask.IsFaulted)
                {
                    log.Status  = 2;
                    log.Message = reqTask.Exception.GetBaseException().StackTrace;
                    _executeLogDao.Insert(log);
                    _logger.LogError(reqTask.Exception.InnerExceptions.FirstOrDefault(),
                                     "执行DelayTask异常,id:{0},name:{1},url:{2}", task.Id, task.Name, task.Url);
                    ReJoin(task);
                }
                else if (reqTask.IsCanceled)
                {
                    log.Status  = 3;
                    log.Message = "timeout";
                    _executeLogDao.Insert(log);
                    ReJoin(task);
                }
                else
                {
                    reqTask.Result.Content.ReadAsStringAsync().ContinueWith(readTask =>
                    {
                        if (readTask.Result == task.SuccessFlag)
                        {
                            //删除数据
                            _delayTaskDao.DeleteById(task.Id);
                            log.Status  = 1;
                            log.Message = "success";
                            _executeLogDao.Insert(log);
                        }
                        else
                        {
                            log.Status  = 2;
                            log.Message = readTask.Result;

                            _logger.LogError("执行DelayTask出错,id:{0},name:{1},url:{2},msg:{3}", task.Id, task.Name,
                                             task.Url, readTask.Result);
                            _executeLogDao.Insert(log);
                            ReJoin(task);
                        }
                    });
                }
            });
        }
예제 #7
0
        private void EventLoop(IChannel channel)
        {
            try
            {
                while (this.IsAlive)
                {
                    // Try to get the next response message from the kernel.  Note that we are using the non-blocking,
                    // so the IsAlive check ensures we continue to poll for results.
                    var nextMessage = channel.TryReceive();
                    if (nextMessage == null)
                    {
                        continue;
                    }

                    // If this is our first message, we need to set the session id.
                    if (ClientSession == null)
                    {
                        throw new NullReferenceException("The client session must be established, but is null");
                    }
                    else if (string.IsNullOrEmpty(ClientSession.SessionId))
                    {
                        ClientSession.SessionId = nextMessage.Header.Session;
                    }

                    // From the message, check to see if it is related to an execute request.  If so, we need to track
                    // that a response is back.
                    var  messageType    = nextMessage.Header.MessageType;
                    bool isExecuteReply = messageType.Equals(MessageType.ExecuteReply);
                    bool hasData        = nextMessage.IsDataMessageType();
                    if (isExecuteReply || hasData)
                    {
                        lock (ExecuteLogSync)
                        {
                            // No parent header means we can't confirm the message identity, so we will skip the result.
                            if (nextMessage.ParentHeader == null)
                            {
                                continue;
                            }

                            var messageId = nextMessage.ParentHeader.Id;
                            if (ExecuteLog.ContainsKey(messageId))
                            {
                                ExecuteLog[messageId].Response.Add(nextMessage);

                                // If we have an execution reply, we can get the execution index from the message
                                if (isExecuteReply)
                                {
                                    ExecuteLog[messageId].Complete       = true;
                                    ExecuteLog[messageId].ExecutionIndex = nextMessage.Content.execution_count;
                                }
                            }
                        }
                    }
                }
            }
            catch (ProtocolViolationException ex)
            {
                Logger.Write("Protocol violation when trying to receive next ZeroMQ message: {0}", ex.Message);
            }
            catch (ThreadInterruptedException tie)
            {
                // We anticipate this and don't want to do anything
            }
            catch (SocketException se)
            {
                Logger.Write("Socket exception {0}", se.Message);
            }
            finally
            {
                // If we get here, we aren't expecting any more responses from the communication channel.  Hopefully
                // we're in the happy path and everything is done, but if not we will abandon outstanding requests
                // that haven't finished.
                AbandonOutstandingExecuteLogEntries();
            }
        }