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

@khgame/patrol

v0.0.8

Published

Patrol is a handy tool, that allows you to easily schedule running of scripts or programs. It supports both scheduled tasks and continuous work.

Downloads

61

Readme

Patrol

Patrol is a handy tool, that allows you to easily schedule running of scripts or programs. It supports both scheduled tasks and continuous work.

It also provides lot of built-in utilities, such as email notification and log management, which can help you easily master various types of scheduled tasks.

Table of Contents

Installation

  • install with npm npm i -g @khgame/patrol

Quick start

Once the package are installed, you can use patrol start command to start the tool.

In the directory where you currently in, some log files will be generated. Therefore, you may select a better place to start your patrol, such as /var/patrol.

You can also run nohup patrol start & to start patrol on background.

Configs

In the above steps, you have started a patrol process, but there are no tasks have been executed yet.

This is because you haven't told patrol which tasks to perform, and what rules to execute.

Then let's start configuring some tasks.

Config Folders

There are two places where you can place the configuration of the task:

One is the directory where patrol is started, such as /var/patrol mentioned in the above example.

The other is /etc/patrol/conf.d/

e.p.

# ls -a /etc/patrol/conf.d/
ep-continuous.patrol.json
ep-scheduler.patrol.json

Config Files

As you can see, the configuration files are all json files with filenames ending in .patrol.json

The specific configuration of each file is as follows.

# cat /etc/patrol/conf.d/ep-scheduler.patrol.json
{
  "script" : "./scripts/to/run.js",
  "rule": "*/10 * * * * *"
}
  • script:

    The address of a js script or node.js package

    Both relative and absolute addresses are supported. When using relative addresses, it is recommended to put the relevant script or node project in the startup directory of patrol.

  • rule:

    There are to rules are currently supported: cron and continuous

    1. cron:

      If you want to use the cron rule, just fill in the expression of cron directly in the rule. e.p. "0 */10 * * * *"

    2. continuous:

      If you want to use the continuous rule, fill in the rule directly with "continuous:" + sleep time after the end of each cycle. e.p. "continuous:1000"

Any changes of config file will be reloaded in a minute to reload the task, expect for the following two situations:

  1. The task is running.

    If the task is running, the reload action will be performed after the task sleep. Therefore, it is best not to set the sleep time in continuous mode too short, in case the configuration cannot be reloaded.

  2. The config file are removed.

    Obviously, in the case that the configuration file is deleted, the task will not be canceled. (This is especially important when modifying the configuration name) If you need to cancel the task, you can restart the patrol process.

When config file are reloaded, the related package will also be reloaded.

Batch Tasks

You can also config a batch of tasks in a config file.

[
    {
      "script" : "./scripts/to/run.js",
      "rule": "continuous:60000"
    },
    {
      "script" : "./scripts/report/status.js",
      "rule": "0 */2 * * * *"
    }
]

Supported tasks

Js scripts and node packages

To create a script that can be scheduled by patrol, you just need to export a function named patrol in the script or the index file of the node project.

e.p.

"using strict"

module.exports = {

    patrol: (log) => {
        log.info(Date.now())
    }
}

See more examples here.

Other Operations

Check Status

You can use turtle ls -pi command in the startup folder to check the state of patrol/

Restart

You can use turtle restart [-f] <process-name|process-pid> command to restart the patrol process

Logs

Based on the log specification of @khgame/turtle, you can find the corresponding logs of each task in the logs directory under the patrol startup position.

If you have restart the process by turtle restart, you can use command turtle log -pf to follow the latest log.

Clear tasks

For now, you can clear tasks which configs are removed by restart patrol

Monitor APIs

  • /api/v1/core/health : [GET] get health status of the patrol service.
  • /api/v1/panel/scheduler : [GET] get status of all schedulers.