private async Task ExecuteProcess(CancellationToken token) { while (true) { Job job = null; try { _queue.JobsInQueue.Wait(token); if (!_queue.TryGetJob(out job)) { continue; } _queue.ExecuteJob(job, token); await Delay(_executePeriod, token).ConfigureAwait(false); } catch (JobAttemptException exception) { _logger?.Warning(exception.Message); _logger?.Error(exception.InnerException); if (job is null) { return; } await Delay(job.Attempt.Period, token).ConfigureAwait(false); _queue.JobUnWork(job); } catch (JobExecutionException exception) { _logger?.Error(exception); _queue.ArchiveJob(job); await Delay(_executePeriod, token).ConfigureAwait(false); } catch (OperationCanceledException) { _logger?.Information($"Worker {_id} has canceled."); break; } catch (Exception exception) { _logger?.Error(exception); await Delay(_executePeriod, token).ConfigureAwait(false); } } }
private async Task RemoveOldJobsProcess(CancellationToken token) { while (true) { try { await Task.Delay(_options.CleaningInMemoryStoragePeriod, token).ConfigureAwait(false); lock (_locker) { var jobs = _jobs.Where(x => x.IsArchived && DateTime.Now - x.Job.CreationTimestamp > _options.MaximumInMemoryStorageTime).ToArray(); foreach (var job in jobs) { _jobs.Remove(job); } _logger?.Debug($"Removed {jobs.Length} jobs from queue {Name}."); } } catch (OperationCanceledException) { break; } catch (Exception exception) { _logger?.Error(exception); } } }
private static void ReadRepositoryProcess(CancellationToken token) { while (true) { try { var jobs = _repository.Get(); DistributeOnQueues(jobs); ClearRepository(jobs); _logger?.Verbose("Read jobs from the repository."); Task.Delay(_options.PollingPeriod, token).Wait(token); } catch (OperationCanceledException) { _logger?.Information("Scheduler read repository process has stopped."); break; } catch (Exception exception) { _logger?.Error(exception); } } }
/// <inheritdoc /> public TJob Get <TJob>(string jobName) where TJob : Job { if (string.IsNullOrWhiteSpace(jobName)) { throw new ArgumentException("Value cannot be null or whitespace.", nameof(jobName)); } try { using (var db = new LiteDatabase(_connectionString)) { var fullName = typeof(TJob).FullName; if (fullName == null) { return(null); } var collection = db.GetCollection <TJob>(fullName.Replace(".", "_")); return(collection.FindOne(x => x.JobName == jobName)); } } catch (Exception exception) { _logger?.Error(exception); throw; } }
private void Init(string path) { try { var directory = Directory.GetParent(path); if (!directory.Exists) { directory.Create(); } if (File.Exists(path)) { return; } var executeString = @"CREATE TABLE Jobs ( Id INTEGER PRIMARY KEY AUTOINCREMENT, JobName VARCHAR(100) NOT NULL, JobType VARCHAR(100) NOT NULL, Data VARCHAR(10240) NOT NULL ); CREATE INDEX jobName_index ON Jobs ( JobName );"; using (var cn = new SQLiteConnection(ConnectionString)) { cn.Open(); cn.Execute(executeString); } } catch (Exception exception) { _logger?.Error(exception); throw; } }
public async Task Execute(IJobExecutionContext context) { var jobId = context.JobDetail.JobDataMap["id"]; Logger.JobId = jobId == null ? Guid.Empty : Guid.Parse(jobId.ToString()); await Logger.Info("任务开始"); try { await Execute(new JobTaskContext { JobId = Logger.JobId, JobExecutionContext = context }); } catch (Exception ex) { await Logger.Error("任务异常:" + ex); } await Logger.Info("任务结束"); }