@1999/scheduler 中文文档教程

发布于 7年前 浏览 59 项目主页 更新于 3年前

Scheduler

构建状态 依赖状态 DevDependency Status

用于使用 Typescript 编写的定期任务的 Node.js 库。

How to install

npm install @1999/scheduler --save

API

scheduler 的唯一概念是 Task,代表一个周期性运行的任务。 它应该是一个回调,应该返回一个 Promise。

Simple tasks

使用 scheduler.addTask(task: Task, period: number) 函数设置任务周期。

import {
  default as Scheduler,
  Task,
} from '@1999/scheduler';

const task: Task = () => Promise.resolve(2);
const scheduler = new Scheduler();
scheduler.addTask(task, 1000);

scheduler.start();

Named tasks

在这种情况下,您可以在调度程序构造函数中传递任务组。 然后使用 scheduler.addTask(task: Task, periodId: string) 函数将任务分配给任务组。

import {
  default as Scheduler,
  Task,
} from '@1999/scheduler';

const task1: Task = () => got('https://api.facebook/id/1');
const task2: Task = () => got('https://api.facebook/id/2');

const scheduler = new Scheduler({ api: 1000 });
scheduler.addTask(task1, 'api');
scheduler.addTask(task2, 'api');

scheduler.start();

When period should depend on task execution

有时仅仅有任务的执行周期是不够的。 例如,当您有一个允许您每 N 秒发出一次请求的 API 时:在这种情况下,在您收到上一个请求的响应后仅 N 秒发送下一个请求会更安全。 为此,您可以使用 Marker 回调,它是 Task 的唯一参数:

import {
  default as Scheduler,
  Marker,
  Task,
} from '@1999/scheduler';

const task: Task = (marker: Marker) => {
  return got('https://api.facebook/id/1').then(() => {
    // you can run marker function anywhere inside your task
    // and the period pause will be started from this moment
    marker();
  });
};

const scheduler = new Scheduler();
scheduler.addTask(task);
scheduler.start();

Events

Scheduler instance extends node.js EventEmitter。 您可以使用它来订阅 Scheduler 实例中发生的事件:

  • taskCompleted - emits when task is successfully finished. Also emits an object { name: string, execTime: number } where runTime is the task execution time in milliseconds.
  • taskFailed - emits when task execution fails. Also emits an object { err: Error, execTime: number, name: string }

Scheduler

Build Status Dependency Status DevDependency Status

Node.js library for periodical tasks written in Typescript.

How to install

npm install @1999/scheduler --save

API

The only concept of scheduler is a Task which represents a periodically run task. It should be a callback which should return a Promise.

Simple tasks

Use scheduler.addTask(task: Task, period: number) function to set up task period.

import {
  default as Scheduler,
  Task,
} from '@1999/scheduler';

const task: Task = () => Promise.resolve(2);
const scheduler = new Scheduler();
scheduler.addTask(task, 1000);

scheduler.start();

Named tasks

In this case you can pass task groups in scheduler constructor. Then use scheduler.addTask(task: Task, periodId: string) function to assign task to task group.

import {
  default as Scheduler,
  Task,
} from '@1999/scheduler';

const task1: Task = () => got('https://api.facebook/id/1');
const task2: Task = () => got('https://api.facebook/id/2');

const scheduler = new Scheduler({ api: 1000 });
scheduler.addTask(task1, 'api');
scheduler.addTask(task2, 'api');

scheduler.start();

When period should depend on task execution

Sometimes it's not enough to have execution periodicity for tasks. For instance when you have an API which allows you to make requests once per N seconds: in this case it can be safer to send next request only N seconds after you get the response from the previous request. For this purpose you can use Marker callback which is the only argument for Task:

import {
  default as Scheduler,
  Marker,
  Task,
} from '@1999/scheduler';

const task: Task = (marker: Marker) => {
  return got('https://api.facebook/id/1').then(() => {
    // you can run marker function anywhere inside your task
    // and the period pause will be started from this moment
    marker();
  });
};

const scheduler = new Scheduler();
scheduler.addTask(task);
scheduler.start();

Events

Scheduler instance extends node.js EventEmitter. You can use it to subscribe to events happening inside Scheduler instance:

  • taskCompleted - emits when task is successfully finished. Also emits an object { name: string, execTime: number } where runTime is the task execution time in milliseconds.
  • taskFailed - emits when task execution fails. Also emits an object { err: Error, execTime: number, name: string }
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文