Accessing job method argument values from IElectStateFilter

job-filters
Tags: #<Tag:0x00007f8b9e8e6f30>

#1

Hello.

I have a few jobs, where I need to trigger some sort of functionality, if the job has failed (completely, after all retries). My issue is, that I need to gather some of the arguments passed into the job’s method.

From what I can read various places, that means creating an attribute that derives from JobFilterAttribute. As I need IoC, I followed the a SO talking about passive attributes.

Consider the following job:

[FailedJobTrigger]
public class MyJob
{
	private readonly DatabaseContext context;
	private readonly IMediator mediator;

	public Myjob(DatabaseContext context, IMediator mediator)
	{
		this.context = context;
		this.mediator = mediator;
	}
	
	public async Task Run(int id1, int id2, int id3)
	{
		//... do some logic
	}
}

Now, when this fails, after all retries, I need to trigger some functionality so update stuff in my database.

Consider the following JobFilterAttribute

public class FailedJobTriggerFilter : IElectStateFilter
{
	private readonly DatabaseContext dbContext;
	private readonly IMediator mediator;
	
	public FailedJobTriggerFilter(DatabaseContext dbContext, IMediator mediator)
	{
		this.dbContext = dbContext;
		this.mediator = mediator;			
	}
	
	public void OnStateElection(ElectStateContext context)
	{
		try
		{
			var failedState = context.CandidateState as FailedState;
			if(failedState == null) return;
			
			//... get all argument values from job method Run(int, int, int)
			var id1 = ?;
			var id2 = ?;
			var id3 = ?;
		}
		catch(Exception)
		{
			//... to not enter loop
		}
	}
}

As you can see, I wish to get the value from all three argument. How would I go about that?