Documente Academic
Documente Profesional
Documente Cultură
Hangfire...................................................................................................................... 1
Client...................................................................................................................... 2
Server....................................................................................................................... 3
Add a job............................................................................................................... 3
and relax.............................................................................................................. 4
Hangfire
Three main components:
Client,
Storage and
Server.
MILO DAVITKOVI 1
Client
Background jobs:
A static member is shared by all objects of the class. All static data is
initialized to zero when the first object is created, if no other initialization is present
A static member function can only access static data member, other static
member functions and any other functions from outside the class.
Static member functions have a class scope and they do not have access to
the this pointer of the class. You could use a static member function to determine
whether some objects of the class have been created or not.
MILO DAVITKOVI 2
There is also more easy way to create background jobs the BackgroundJob
class that allows you to use static methods to perform the creation task.
Job Storage
Hangfire keeps background jobs and other information that relates to the
processing inside a persistent storage. Persistence helps background jobs to survive
on application restarts, server reboots, etc.
Server
Background jobs are processed by Hangfire Server. It is implemented as a set
of dedicated (not thread pools) background threads that fetch jobs from a storage
and process them. Server is also responsible to keep the storage clean and remove
old data automatically.
All you need is to create an instance of the BackgroundJobServer class and start the
processing:
Console.ReadLine();
MILO DAVITKOVI 3
with the runtime, depending on the hosting model you choose (OwinHost, IIS, and
IIS-Express).
Add a job
Hangfire handles different types of background jobs, and all of them are invoked on
a separate execution context.
Fire-and-forget
This is the main background job type, persistent message queues are used to
handle it. Once you create a fire-and-forget job, it is saved to its queue ("default" by
default, but multiple queues supported). The queue is listened by a couple of
dedicated workers that fetch a job and perform it.
These jobs are executed only once and almost immediately after they fired.
Delayed
If you want to delay the method invocation for a certain type, call the following
method. After the given delay the job will be put to its queue and invoked as a
regular fire-and-forget job.
MILO DAVITKOVI 4
BackgroundJob.Schedule(() => Console.WriteLine("Delayed"),
TimeSpan.FromDays(1));
Delayed jobs are executed only once too, but not immediately only after the specified
time interval.
Recurring
To call a method on a recurrent basis (hourly, daily, etc), use the RecurringJob class.
You are able to specify the schedule using CRON expressions to handle more
complex scenarios.
RecurringJob.AddOrUpdate(
() => Console.WriteLine("Recurring!"),
Cron.Daily);
Continuations
MILO DAVITKOVI 5
var id = BackgroundJob.Enqueue(() => Console.WriteLine("Hello, "));
BackgroundJob.ContinueWith(
jobId,
() => Console.WriteLine("Continuation!"));
Batches
Batch Continuations
Batch continuation is fired when all background jobs in a parent batch finished.
Batch.ContinueWith(batchId, x =>
{
MILO DAVITKOVI 6
x.Enqueue(() => Console.WriteLine("Last Job"));
});
and relax
Hangfire saves your jobs into persistent storage and processes them in a
reliable way. It means that you can abort Hangfire worker threads, unload
application domain or even terminate the process, and your jobs will be processed
anyway. Hangfire flags your job as completed only when the last line of your code
was performed, and knows that the job can fail before this last line. It contains
different auto-retrying facilities, that can handle either storage errors or errors
inside your code.
This is very important for generic hosting environment, such as IIS Server.
They can contain different optimizations, timeouts and error-handling code (that
may cause process termination) to prevent bad things to happen. If you are not
using the reliable processing and auto-retrying, your job can be lost. And your end
user may wait for its email, report, notification, etc. indefinitely.
Background Process
Use them when you need to run background processes continuously throught
the lifetime of your application.
MILO DAVITKOVI 7
Directory.CleanUp(Directory.GetTempDirectory());
context.Wait(TimeSpan.FromHours(1));
}
}
Configuring Authorization
Hangfire Dashboard exposes sensitive information about your background jobs,
including method names and serialized arguments as well as gives you an
opportunity to manage them by performing different actions retry, delete, trigger,
etc. So it is really important to restrict access to the Dashboard.
Multiple Dashboards
You can also map multiple dashboards that show information about different
storages.
Fire-and-forget method invocation has never been simpler. As you already know
from the Quick start guide, you only need to pass a lambda expression with the
corresponding method and its arguments:
MILO DAVITKOVI 8
BackgroundJob.Enqueue(() => Console.WriteLine("Hello, world!"));
The Enqueue method does not call the target method immediately, it runs the
following steps instead:
BackgroundJob.Schedule(
TimeSpan.FromDays(1));
MILO DAVITKOVI 9
TimeSpan.FromSeconds(1) 1 second after
RecurringJob.RemoveIfExists("some-id");
Trigger
To run a recurring job now, call the Trigger method. The information about
triggered invocation will not be recorded in the recurring job itself, and its next
execution time will not be recalculated from this running. For example, if you have a
weekly job that runs on Wednesday, and you manually trigger it on Friday it will run
on the following Wednesday.
RecurringJob.Trigger("some-id");
1
MILO DAVITKOVI
0
Using cancellation tokens
Hangfire provides support for cancellation tokens for your jobs to let them
know when a shutdown request was initiated, or job performance was aborted. In
the former case the job will be automatically put back to the beginning of its queue,
allowing Hangfire to process it after restart.
Noticed problems:
With Hangfire
1
MILO DAVITKOVI
1
You can:
o Run Fire and Forget background jobs
o Run Delayed jobs
o Run Recurring jobs
o Make Continues jobs
o Manipulating with recurring jobs
You cant:
o Set up recurring jobs less then a minute
1
MILO DAVITKOVI
2