I’m trying to requeue a job automatically with certain condition.
Here is my code :
public void OnStateElection(ElectStateContext context)
{
//only if the job end in success
var succeededState = context.CandidateState as SucceededState;
if (succeededState == null)
{
// This filter accepts only success job state.
return;
}
this.repository = ObjectStore.Instance.Get<IBuildDBRepository>();
this.configuration = ObjectStore.Instance.Get<ConfigurationManagerWrap>();
//get transfer info from job argument
var data = context.BackgroundJob.Job.Args[0] as Dictionary<string, object>;
if (data != null)
{
//extract transfer
object transferIdValue = 0;
if (data.TryGetValue(TransferContext.TransferIdParameter, out transferIdValue))
{
//fetch transfer info
var transferId = Convert.ToInt32(transferIdValue);
var transfer = this.repository.GetInterStudioTransfers().FirstOrDefault(it => it.TransferID == transferId);
//oh its in collision
if (transfer.Status == InterStudioTransferStatusValue.InCollision)
{
context.CandidateState = new ScheduledState(TimeSpan.FromSeconds(2))
{
Reason = $"Transfer with id {transfer.TransferID} is in collision.Requeuing with delay {this.requeueDelay}."
};
// BackgroundJob.Enqueue<TransferContext>(x => x.Execute(data, JobCancellationToken.Null));
}
}
}
}
What the code does is that when the job is success if fetch the argument and look for the state and reschedule the job through the CandidateState
In my test i’m trying for the requeue to occurss 5 times. The code above work for the first time and then its never called on the next run.
I did some changes and tried the enqueue and it worked fine. There seem that the filter is not called on consecutive requeue. HAve any idea why this happen ?