How to schedule job with queue name?


#1

Hello

I’ve been trying to figure out how to schedule a job with specific queue name.
Tried with
BackgroundJob.Schedule( () => client.Create(() = > some method, state), timespan);

the compiled all good, but when executed, throwing exception:
Anonymous functions delegates and lambda expressions aren’t supported in job method parameters: it’s very hard to serialize them and all their scope in general.

Tried with:
Client.Schedule(() => client.Create(() = > some method, state), timespan);

the compiled all good, but when executed, throwing exception:
Anonymous functions delegates and lambda expressions aren’t supported in job method parameters: it’s very hard to serialize them and all their scope in general.

Anyone can help please?
Many thanks
Jimmy


#2

This works for me

BackgroundJobClient client = new BackgroundJobClient();
client.Create<IMyInterface>(myInterface => myInterface.MyMethod(), new EnqueuedState("queueName"));

#3

Would using an attribute on the job method be a way?

[Queue("critical")]
public void SomeMethod() { }

http://docs.hangfire.io/en/latest/background-processing/configuring-queues.html

The errors you get, though, is because of something different. I’m not quite sure what you are trying to do, but I’ve never seen the client.Schedule(() => client.create(() => ....)) form of creating a background job before.

The way I know the framework, client.create(.....) is the ‘raw’ way of creating a background job. the client.Schedule(....) is an extension method that you call instead of calling Create. So, you would either call

client.Create(Job.FromExpression(() => MyMethod(), state)

where state is a pre-initialized state (which will include the timespan part and also the queue part), or

client.Schedule(() => MyMethod(), delay)

which will call Create for you.

So the Schedule() method expects an expression - the method to be called on the background server, so your code is basically trying to schedule the creation of a new background job in the background. A job creating a job. Since the expression you give to create could be capturing local variables, it is (near) impossible to serialize it and then deserialize it and make it do what you expected it to do.

So…

Hope that helped a little…