/// <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}"); } }); }
/// <summary> /// Remove jobs when they succeed/fail /// </summary> private void ProcessRunningJob(Job job) { if (!RunningJobs.TryGetValue(job, out var scrapeResult)) { return; } lock (lockObject) { if (scrapeResult.IsCompleted) { var result = RunningJobs[job].Result; if (result.Error != null) { job.Status = JobStatus.Failed; job.Result = new Dictionary <string, IEnumerable <string> > { { "Error", new List <string> { scrapeResult.Exception.GetFullExceptionMessage() } } }; } else { job.Status = JobStatus.Completed; job.Result = result.Scrape; } JobRepository.Save(job); RunningJobs.TryRemove(job, out var removedJob); } else if (scrapeResult.IsFaulted) { job.Status = JobStatus.Failed; job.Result = new Dictionary <string, IEnumerable <string> > { { "Error", new List <string> { scrapeResult.Exception.GetFullExceptionMessage() } } }; JobRepository.Save(job); RunningJobs.TryRemove(job, out var removedJob); } } }