public async Task <JobExecutionStatus> ExecuteAsync(ICronusJob <object> job, CancellationToken cancellationToken = default) { tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); CancellationToken ct = tokenSource.Token; _jobName = job.Name; try { CronusJobState jobState = await GetJobStateAsync(ct).ConfigureAwait(false); if (jobState == CronusJobState.UpForGrab) { await job.SyncInitialStateAsync(this, ct).ConfigureAwait(false); bool iAmKing = await BecomeKingAsync(job.Data, ct).ConfigureAwait(false); if (iAmKing) { using (Timer pinger = new Timer(PingTimerMethod, tokenSource, TimeSpan.Zero, TimeSpan.FromSeconds(10))) { jobState = await GetJobStateAsync(ct).ConfigureAwait(false); if (jobState == CronusJobState.UpForGrab) { return(await job.RunAsync(this, ct).ConfigureAwait(false)); } } } else // How to reproduce this case: 1. schedule a job twice for after 30 seconds (double click rebuild for example) and then kill and start the application { // A thread got here a millisecond before you, snatched your job and started running it , so you are not the king. return(JobExecutionStatus.Running); } } if (jobState == CronusJobState.Running) { return(JobExecutionStatus.Running); } } catch (Exception) { return(JobExecutionStatus.Failed); } return(JobExecutionStatus.Completed); }
public async Task HandleAsync(RebuildIndexInternal sagaTimeout) { ICronusJob <object> job = null; // we need to redesign the job factories var theId = sagaTimeout.EventStoreIndexRequest.Id.Id; if (theId.Equals(typeof(EventToAggregateRootId).GetContractId(), StringComparison.OrdinalIgnoreCase)) { job = jobFactory.CreateJob(sagaTimeout.EventStoreIndexRequest.Timebox); } else if (theId.Equals(typeof(MessageCounterIndex).GetContractId(), StringComparison.OrdinalIgnoreCase)) { job = messageCounterJobFactory.CreateJob(sagaTimeout.EventStoreIndexRequest.Timebox); } else { return; } JobExecutionStatus result = await jobRunner.ExecuteAsync(job).ConfigureAwait(false); if (result == JobExecutionStatus.Running) { RequestTimeout(new RebuildIndexInternal(sagaTimeout.EventStoreIndexRequest, DateTime.UtcNow.AddSeconds(30))); } else if (result == JobExecutionStatus.Failed) { // log error RequestTimeout(new RebuildIndexInternal(sagaTimeout.EventStoreIndexRequest, DateTime.UtcNow.AddSeconds(30))); } else if (result == JobExecutionStatus.Completed) { var finalize = new FinalizeEventStoreIndexRequest(sagaTimeout.EventStoreIndexRequest.Id); commandPublisher.Publish(finalize); } }
public Task <JobExecutionStatus> ExecuteAsync(ICronusJob <object> job, CancellationToken cancellationToken = default) { return(job.RunAsync(clusterOperations, cancellationToken)); }