In my dev environment, i need add and execute a job in a specific server, because i has 2 ou 3 developer using the same server but job was execute in inapropiated machine.
How i do this?
I’ve had the same issue. First, I’ve tried specifying the queue name at runtime (adding machine name to the queue name) when adding a job - the problem with that solution was, that retries always went to the default queue.
Now, I’ve added my own JobFilterAttribute implementation (actually just a minor variation from QueueAttribute):
public class DynamicQueueAttribute : JobFilterAttribute, IElectStateFilter
{
public DynamicQueueAttribute(string queue)
{
Queue = queue;
}
public string Queue { get; private set; }
public void OnStateElection(ElectStateContext context)
{
var enqueuedState = context.CandidateState as EnqueuedState;
if (enqueuedState != null)
{
enqueuedState.Queue = AppendMachineNameIfDebug(Queue);
}
}
private string AppendMachineNameIfDebug(string queueName)
{
#if DEBUG
queueName = string.Format("{0}_{1}", queueName, System.Environment.MachineName.ToLowerInvariant());
#endif
return queueName;
}
}
Instead of using [Queue(“somequeue”)], I’m now simply using [DynamicQueue(“somequeue”)] on the job interface.
Of course, you need to specify the appropriate queue name (including the _) when setting up the server and you should not deploy a debug build to production…
If someone has another idea how to solve this, I’d be interested to hear it, too.
Hi Dusty, thank you for help.
My solution is more simple (and need manual intervention)
var env = ConfigurationManager.AppSettings.Get(“env”);
var backgroundServerOptions = new BackgroundJobServerOptions();
if (env.Equals(Constants.Env.Debug))
{
backgroundServerOptions = new BackgroundJobServerOptions
{
//Queues = new {Environment.MachineName.ToLower(), “default”}
Queues = new { Environment.MachineName.ToLower()}
};
}
app.UseHangfireServer(backgroundServerOptions);
And i use [Queue(“mymachinenameinlowercase”)] in all class that i need develop.
I dont undestand your example to use customized queue class, because I cant anotate my class with dynamic name of queue.
s
Well, if you can annotate your class with [Queue(“queuename”)], why can’t you annotate it with a custom attribute [DynamicQueue(“queuename”)] instead?
If you do that, the name of the queue will be queuename_ if you’re running a debug build and just ‘queuename’ if it’s no debug build.
You can add the same AppendMachineNameIfDebug function for the server setup:
backgroundServerOptions = new BackgroundJobServerOptions
{
Queues = new[] { AppendMachineNameIfDebug("default")}
};
The DynamicQueueAttribute class is simply a replacement for the standard QueueAttribute of hangfire.
Now i undestand, thank you! I will try your solution.