Skip to content

Instantly share code, notes, and snippets.

Created October 31, 2017 09:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/caeb25f3e7e4430d94b353ebea82e3e2 to your computer and use it in GitHub Desktop.
Save anonymous/caeb25f3e7e4430d94b353ebea82e3e2 to your computer and use it in GitHub Desktop.
internal class UseTenantNameAsQueueAttribute : JobFilterAttribute, IElectStateFilter
{
public string TenantName;
public UseTenantNameAsQueueAttribute()
{
this.TenantName = Settings.Default.TenantName.ToLower();
}
public void OnStateElection(ElectStateContext context)
{
if (context.CandidateState is ScheduledState s)
{
try
{
var jobid = context.BackgroundJob.Id;
Storage.SaveJobQueue(jobid, TenantName)
}
catch (Exception ex)
{
//logging
}
}
else if (context.CandidateState is EnqueuedState e)
{
try
{
var jobid = context.BackgroundJob.Id;
e.Queue = Storage.LoadJobQueue(jobid) ?? TenantName
}
catch (Exception ex)
{
e.Queue = TenantName;
//logging
}
}
}
}
@jrt324
Copy link

jrt324 commented Feb 3, 2018

Storage.SaveJobQueue(jobid, TenantName) ?
SaveJobQueue is not Hangfire API
how to implement SaveJobQueue ?

@pallu
Copy link

pallu commented Sep 24, 2019

Storage.SaveJobQueue(jobid, TenantName) ?
SaveJobQueue is not Hangfire API
how to implement SaveJobQueue ?

I was able to do this:
context.Connection.SetJobParameter(context.BackgroundJob.Id, "QueueName", TenantName)
and
e.Queue = context.Connection.GetJobParameter(context.BackgroundJob.Id, "QueueName") ?? TenantName

I tried it both with Hangfire.MemoryStorage and the regular Sql Storage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment