.net core任务调度_探索.NET Core库Coravel以进行任务调度,缓存,邮件发送等

.net core任务调度

.net core任务调度

Coravel claims it is a "Near-zero config .NET Core library that makes Task Scheduling, Caching, Queuing, Mailing, Event Broadcasting (and more) a breeze!" A lovely claim, that is, in fact, true! It's open source and on Github at https://github.com/jamesmh/coravel so give Coravel a star!

Coravel声称这是一个“近零配置.NET Core库,使任务调度,缓存,排队,邮件,事件广播(以及更多)变得轻而易举!” 一个不错的主张,就是事实,是真的! 它是开源的,可以在Github上的https://github.com/jamesmh/coravel上找到,所以给Coravel一颗星星吧!

Coravel is available on NuGet as a package - as are all things  - or you can also install it's helper CLI with a simple dotnet tool install --global coravel-cli. After this, using coravel easy, early, and often is as simple as:

在NuGet上, Coravel可以作为软件包使用(包括所有功能),或者您也可以使用简单的dotnet tool install --global coravel-cli安装它的帮助程序CLI。 在此之后,使用coravel轻松,早期且通常很简单:

coravel install

A nice teach that makes it easy, the coravel CLI adds the package reference, restores your project, and reminds you to set it up in ConfigureServices() in Startup.cs. A nice example of a thoughtful library that is trying to make onboarding simpler.

coravel CLI很好地教了它,使它变得容易,它添加了程序包引用,还原了项目,并提醒您在Startup.cs的ConfigureServices()中进行设置。 一个深思熟虑的库的一个很好的示例,它试图简化入门过程。

The Coravel CLI is also a nice scaffolder to get you started with item templates:

Coravel CLI还是一个不错的脚手架,可让您开始使用项目模板:

> coravel
Usage: coravel [options] [command]

Options:
-?|-h|--help Show help information

Commands:
event
install
invocable
mail

那是什么 (But what is it?)

Coravel

With a somewhat vague name and a list of cool features that may not seem related, you may find yourself wondering WHAT is this and WHY do I need it?

由于名称含糊不清,并列出了一些似乎并不相关的很酷的功能,您可能会想知道这是什么,为什么我需要它?

When you start thinking about layering and responsibilities of real production software, you'll note that there are arguably some gaps in the BCL (Base Class Libraries) that .NET makes available, particularly as you move up into the Application Development space.

当您开始考虑实际生产软件的分层和职责时,您会注意到,.NET提供的BCL(基本类库)中可能存在一些空白,特别是当您进入应用程序开发空间时。

Scheduled jobs and tasks, simple emailing with Razor Templates, a lightweight event dispatcher, easily queueable background tasks are just some of the higher level primitives you'll find yourself wanting when creating business apps. Coravel collects those buildable elements and allows you to string them together very quickly.

计划的作业和任务,使用Razor模板发送的简单电子邮件,轻量级的事件调度程序,易于排队的后台任务,只是创建业务应用程序时会想要的一些高级基元。 Coravel收集了这些可构建元素,并允许您非常快速地将它们串在一起。

For example, I'll create an "Invocable." Basically just a method that is more 'job-like.' It has business scope and I need to have it invoked later by some schedule or process within my app.

例如,我将创建一个“ Invocable”。 基本上只是一种更“像工作”的方法。 它具有业务范围,我需要稍后在我的应用程序中通过某些计划或流程来调用它。

Here I'll register one in my Startup.cs.

在这里,我将在Startup.cs中注册一个。

services.AddScoped<SendNightlyReportsEmailJob>();

So I need to send a nightly report. That's an invocable thing, and it's also an IMailer because it mails things. Note the injected IMailer in the constructor. All very natural in ASP.NET Core, using Dependency Injection.

因此,我需要发送每晚报告。 这是一个难以理解的事情,也是一个IMailer,因为它可以邮寄东西。 请注意在构造函数中注入的IMailer。 使用依赖注入,在ASP.NET Core中一切都很自然。

public class SendNightlyReportsEmailJob : IInvocable
{
private IMailer _mailer;
public SendNightlyReportsEmailJob(IMailer mailer)
{
this._mailer = mailer;
}

public async Task Invoke()
{
Console.WriteLine("NightlyReportMailable Started....");
await Task.Delay(10000);

// You could grab multiple users from a DB query ;)
var mailable = new NightlyReportMailable(new UserModel
{
Name = "Coravel is lovely!",
Email = "test@test.com"
});
await this._mailer.SendAsync(mailable);
Console.WriteLine($"NightlyReportMailable was sent at {DateTime.UtcNow}.");
}
}

Then I can have this mailed every evening with the Coravel Scheduler:

然后,我可以每天晚上将此邮件发送给Coravel Scheduler:

scheduler.Schedule<SendNightlyReportsEmailJob>().Daily();

But when, right? Easy:

但是什么时候吧? 简单:

scheduler
.Schedule<SendNightlyReportsEmailJob>()
.DailyAt(1, 30)
.Zoned(TimeZoneInfo.Local);

What if you have a task that needs to happen, but maybe it's either long-running or happening often. You don't want two tasks going at the same time, so PreventOverlapping! Clever.

如果您有一项需要执行的任务,但是可能是长期运行或经常发生,该怎么办。 您不希望同时执行两个任务,因此,PreventOverlapping! 聪明。

scheduler
.Schedule<DoAThingOften>()
.EveryMinute()
.PreventOverlapping("DoAThingOften");

And that's just the scheduler. That mail you have to send? You can use Razor Pages to create reach HTML emails! That makes New User Sign Up, or Completed Order very easy to create. All self-contained in your app. I dig it.

那只是调度程序。 您必须发送该邮件? 您可以使用Razor Pages创建覆盖HTML电子邮件! 这使得新用户注册或已完成订单非常容易创建。 所有内容都包含在您的应用程序中。 我喜欢。

Finally note that there's Pro paid version of Coravel that gives you a very professional UI for Jobs and Invocables, and allows you to visually configure your back-end job schedules. A nice way to support open source - especially when you start using it and really depending on it - is to explore very reasonable Pro licenses like those that Pro Coravel has. Impressive stuff, truly.

最后请注意,Pro Pro付费版本的Coravel为您提供了一个非常专业的Jobs和Invocables UI,并允许您直观地配置后端工作计划。 支持开源的一个好方法-特别是当您开始使用并真正依赖它时-探索非常合理的Pro许可证,例如Pro Coravel拥有的许可证。 确实令人印象深刻的东西。

*I have no relationship with Coravel the project or the Pro licenses, I'm just a fan.

*我与Coravel项目或Pro许可证没有关系,我只是一个粉丝。

翻译自: https://www.hanselman.com/blog/exploring-the-net-core-library-coravel-for-task-scheduling-caching-mailing-and-more

.net core任务调度

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值