示例#1
0
        void innerStart()
        {
            while (!toStop1)
            {
                running = false;
                idleTimes++;
                try
                {
                    // to check toStop signal, we need cycle, so wo cannot use queue.take(), instand of poll(timeout)
                    var result = triggerQueue.TryTake(out TriggerParam triggerParam, 3 * 1000);


                    if (triggerParam != null)
                    {
                        running   = true;
                        idleTimes = 0;
                        triggerLogIdSet.Remove(triggerParam.logId);

                        // parse param
                        string[] handlerParams = (triggerParam.executorParams != null && triggerParam.executorParams.Trim().Length > 0)
                                ? triggerParam.executorParams.Split(',') : null;

                        // handle job
                        ReturnT <String> executeResult = null;
                        try
                        {
                            // log filename: yyyy-MM-dd/9999.log
                            String logFileName = XxlJobFileAppender.makeLogFileName(TimeUtil.ToTime(triggerParam.logDateTim), triggerParam.logId);

                            XxlJobFileAppender.contextHolder.Value = (logFileName);
                            ShardingUtil.setShardingVo(new ShardingUtil.ShardingVO(triggerParam.broadcastIndex, triggerParam.broadcastTotal));
                            XxlJobLogger.log("<br>----------- xxl-job job execute start -----------<br>----------- Params:" + string.Join(",", handlerParams ?? new[] { "" }));

                            executeResult = handler.execute(handlerParams);
                            if (executeResult == null)
                            {
                                executeResult = ReturnT <string> .FAIL;
                            }

                            XxlJobLogger.log("<br>----------- xxl-job job execute end(finish) -----------<br>----------- ReturnT:" + executeResult);
                        }
                        catch (Exception e)
                        {
                            if (toStop1)
                            {
                                XxlJobLogger.log("<br>----------- JobThread toStop, stopReason:" + stopReason);
                            }

                            //StringWriter stringWriter = new StringWriter();
                            //e.printStackTrace(new PrintWriter(stringWriter));
                            String errorMsg = e.ToString();
                            executeResult = new ReturnT <String>(ReturnT <string> .FAIL_CODE, errorMsg);

                            XxlJobLogger.log("<br>----------- JobThread Exception:" + errorMsg + "<br>----------- xxl-job job execute end(error) -----------");
                        }

                        // callback handler info
                        if (!toStop1)
                        {
                            // commonm
                            TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.logId, executeResult));
                        }
                        else
                        {
                            // is killed
                            ReturnT <String> stopResult = new ReturnT <String>(ReturnT <string> .FAIL_CODE, stopReason + " [业务运行中,被强制终止]");
                            TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.logId, stopResult));
                        }
                    }
                    else
                    {
                        if (idleTimes > 30)
                        {
                            XxlJobExecutor.removeJobThread(jobId, "excutor idel times over limit.");
                        }
                    }
                }
                catch (Exception e)
                {
                    if (toStop1)
                    {
                        XxlJobLogger.log("<br>----------- xxl-job toStop, stopReason:" + stopReason);
                    }


                    String errorMsg = e.ToString();
                    XxlJobLogger.log("----------- xxl-job JobThread Exception:" + errorMsg);
                }
            }

            // callback trigger request in queue
            while (triggerQueue != null && triggerQueue.Count > 0)
            {
                triggerQueue.TryTake(out TriggerParam triggerParam);
                if (triggerParam != null)
                {
                    // is killed
                    ReturnT <String> stopResult = new ReturnT <String>(ReturnT <string> .FAIL_CODE, stopReason + " [任务尚未执行,在调度队列中被终止]");
                    TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.logId, stopResult));
                }
            }

            logger.Info(string.Format(">>>>>>>>>>>> xxl-job JobThread stoped, hashCode:{0}", Thread.CurrentThread));
        }