Invocables
Invocables represent a self-contained job within your system.
Coravel leverages them to make your code much easier to write, compose and maintain.
Here's an example of scheduling an invocable:
scheduler.Schedule<ReIndexDatabase>()
.DailyAtHour(01)
.Weekday();
Creating An Invocable
Creating an invocable uses the shared interface Coravel.Invocable.IInvocable
.
Using .NET Core's dependency injection services, your invocables will have all their dependencies injected whenever they are executed.
CLI
You may use the Coravel Cli to generate a new invocable.
Manually
Implement the interface
Coravel.Invocable.IInvocable
in your class.In your invocable's constructor, inject any types that are available from your application's service provider.
Make sure that your invocable itself is available in the service container (usually done in your
Startup.cs
file).
services.AddTransient<SendDailyStatsReport>();
services.AddTransient<SomeOtherInvocable>();
That's it!
Examples
#1 Generating A Daily Report And Emailing To Users
In this example, SendDailyReportsEmailJob
is an invocable that was created by us. It handles getting data (via some repository that was injected via DI), generating an e-mail, etc.
A sample implementation of the SendDailyReportsEmailJob
class might look something like this (which is using Coravel's Mailer to send email):
#2 Trigger Long Running Calculations In Background
You might have a use-case where an HTML button on an admin screen fires off an expensive process which then stores the result in a database. You don't want your users waiting for it to finish, since it takes a while.
Assuming you created an invocable DoExpensiveCalculationAndStoreInDB
, you could run it like this: