Scheduled tasks limitations (or how tasks persiste

2019-04-23 13:08发布

I've started to read Hangfire documentation, and found nothing about task limitations.

As declared, tasks (or jobs) are stored somewhere.

Since they are just delegates, the only thing could be stored, as far as I understand, is a delegate "body" (IL?). But there could be closures, that provides some context for the task, e.g., some external services, that can require to load extra assemblies to run their code, etc.

How Hangfire deals with this?
Can task contain any instructions in its body, or there are any limitations?

标签: c# hangfire
2条回答
贪生不怕死
2楼-- · 2019-04-23 13:54

When you create a job it calls Job.FromExpression, if you pass it anything other than a method call expression it throws an exception. So the only thing you can pass in to BackgroundJob.Enqueue is a single line where that line calls a function.

It then serializes they type of the object and all passed in parameters in to JSON using JobHelper.ToJson. When you pass in a instance of a class the instance is not serialized, only the type is, if the execution crosses process boundaries it will loose internal state.

You may want to read up on the blog article on the old hangfire blog site "Are your methods ready to run in background?"

查看更多
聊天终结者
3楼-- · 2019-04-23 14:06

It seems the mechanism is based on Expression for scheduling operations and the library is intended for "internal" (in process) execution mainly in ASP.Net websites.

Meaning, all the assemblies needed for execution of a scheduled operation should be already loaded into the web applications memory space, because they were needed to schedule the job (the application wouldn't have compiled if it was missing a Type from an assembly that was not referenced).

Hope it makes things a little bit clearer!

查看更多
登录 后发表回答