Ejemplo n.º 1
0
        /// <summary>
        /// 监听触发
        /// </summary>
        /// <param name="connection">连接</param>
        private void OnFire(HubConnection connection)
        {
            connection.On <JobContext, string>("Fire", (context, batchId) =>
            {
                var className = context?.Name;
                try
                {
                    bool shouldFire = false;
                    if (string.IsNullOrWhiteSpace(className) || !ClassNameMapTypes.ContainsKey(className) ||
                        (BypassRunning && RunningJobs.ContainsKey(className)))
                    {
                        return;
                    }

                    if ((context.FireTime - DateTime.Now).TotalSeconds <= 10)
                    {
                        shouldFire = true;
                    }
                    else
                    {
                        Debug.WriteLine($"触发任务超时: {className}");
                    }

                    if (shouldFire)
                    {
                        connection.SendAsync("FireCallback", batchId, context.Id, JobStatus.Running).Wait();

                        var jobType = ClassNameMapTypes[className];
                        RunningJobs.TryAdd(className, null);
                        Task.Factory.StartNew(() =>
                        {
                            bool success = false;
                            try
                            {
                                var jobObject = (IJobProcessor)Activator.CreateInstance(jobType);
                                success       = jobObject.Process(context);
                            }
                            catch
                            {
                                // TODO: 输出日志
                            }
                            finally
                            {
                                connection.SendAsync("FireCallback", batchId, context.Id,
                                                     success ? JobStatus.Success : JobStatus.Fire).Wait();
                            }
                        }).ContinueWith((t) => { RunningJobs.TryRemove(className, out _); });
                    }
                    else
                    {
                        connection.SendAsync("FireCallback", batchId, context.Id, JobStatus.Bypass).Wait();
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine($"触发任务 {className} 失败: {e}");
                }
            });
        }