npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zero-to-one-schedule

v1.0.0

Published

[![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][codecov-image]][codecov-url] [![David deps][david-image]][david-url] [![Known Vulnerabilities][snyk-image]][snyk-url] [![npm download][download-image]][down

Downloads

1

Readme

egg-schedule

NPM version build status Test coverage David deps Known Vulnerabilities npm download

A schedule plugin for egg. It supports two scheduler types, worker and all, and can be extended by other plugins.

Installation

$ npm i egg-schedule --save

Usage

egg-schedule is a plugin that has been built-in for egg. It is enabled by default.

// {app_root}/config/plugin.js
exports.schedule = {
  package: 'egg-schedule',
};

// {app_root}/app/schedule/cleandb.js
/**
* @property {Object} schedule
*  - {String} type - schedule type, `worker` or `all`
*  - {String} [cron] - cron expression, [see below](#cron-style-scheduling)
*  - {String | Number} [interval] - interval expression in millisecond or express explicitly like '1h'. [see below](#interval-style-scheduling)
*  - {Boolean} [immediate] - To run a scheduler at startup
*  - {Boolean} [disable] - whether to disable a scheduler, usually use in dynamic schedule
*/
exports.schedule = {
  type: 'worker',
  cron: '0 0 3 * * *',
  // interval: '1h',
  // immediate: true,
};

exports.task = function* (ctx) {
  yield ctx.service.db.cleandb();
};

Overview

egg-schedule supports both time-based scheduling and interval-based scheduling.

Schedule decision is being made by agent process. agent triggers a task and sends message to worker process. Then, one or all worker process(es) execute the task based on schedule type.

To setup a schedule task, simply create a job file in {app_root}/app/schedule. A file contains one job and export schedule and task properties.

The rule of thumbs is one job per file.

Task

Task is a generator function, and accept one parameter, ctx. The syntax is, exports.task = function* (ctx) { ... };

When the scheduled task runs, the scheduled job information will be logged and written to a local file in a folder called /logs. The log file contains many useful information, for example,

  • ctx.method: SCHEDULE
  • ctx.path: /__schedule/${schedulePath}. example path: /__schedule?path=/FULL_PATH_TO/cleandb.js&type=worker&interval=3h
  • ctx.query: scheule config(type=worker&cron=*%2F5%20*%20*%20*%20*%20*)

To create a task, it is as simple as write a generator function. For example:

// A simple logger example
exports.task = function* (ctx) {
  ctx.logger.info('Info about your task');
};
// A real world example: wipe out your database.
// Use it with caution. :)
exports.task = function* (ctx) {
  yield ctx.service.db.cleandb();
};

Scheduling

schedule is an object that contains one required property, type, four optional properties, { cron, interval, immediate, disable }.

Cron-style Scheduling

Use cron-parser.

Note: cron-parser support second as optional that is not supported by linux crontab.

@hourly / @daily / @weekly / @monthly / @yearly is also supported.

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, optional)

Example:

// To execute task every 3 hours
exports.schedule = {
  type: 'worker',
  cron: '0 0 */3 * * *',
};

Interval-style Scheduling

To use setInterval, and support ms conversion style

Example:

// To execute task every 3 hours
exports.schedule = {
  type: 'worker',
  interval: '3h',
};

Schedule Type

Build-in support is:

  • worker: will be executed in one random worker when schedule run.
  • all: will be executed in all workers when schedule run.

Custom schedule

To create a custom schedule, simply create a schedule with a type custom and its corresponding method. Inside your custom method, you can schedule the task to be executed by one random worker or all workers with the built-in method sender.one() or sender.all().

// {app_root}/agent.js
const SCHEDULE_HANDLER = Symbol.for('egg#scheduleHandler');

module.exports = agent => {
  // sender.one() - will notify one random worker to execute task
  // sender.all() - will notify all workers
  agent[SCHEDULE_HANDLER].custom = (schedule, sender) =>
    setInterval(sender.one, schedule.interval);
};

// {app_root}/app/schedule/other.js
exports.schedule = {
  type: 'custom',
};

Dynamic schedule

// {app_root}/app/schedule/sync.js
module.exports = app => {
  exports.schedule = {
    interval: 10000,
    type: 'worker',
    // only start task when hostname match
    disable: require('os').hostname() !== app.config.sync.hostname
  };

  exports.task = function* (ctx) {
    yield ctx.sync();
  };

  return exports;
};

Testing

app.runSchedule(scheduleName) is provided by egg-schedule plugin only for test purpose.

Example:

it('test a schedule task', function* () {
  // get app instance
  yield app.runSchedule('clean_cache');
});

Questions & Suggestions

Please open an issue here.

License

MIT