2 unstable releases

0.2.0 Dec 22, 2024
0.1.0 Dec 22, 2024

#99 in Concurrency

Download history 177/week @ 2024-12-19 12/week @ 2024-12-26

189 downloads per month

GPL-3.0-or-later

30KB
217 lines

Patrolilo

This is probably the 4th attempt I've made at putting something together like this, and it seems like I've finally hit on a winner. Simple YAML definition of scheduled tasks with integrated mobile notifications of script failure through NTFY.

Prerequisite: NTFY

Using ntfy.sh or your self-hosted NTFY instance, configure a user with limited access and an API key. Command-line directions are shown here for self-hosting users.

$ # Add a user to publish notifications
$ ntfy user add patrolilo
$ # Don't allow that user's credentials to be used to do anything but publish to your desired channel
$ ntfy access patrolilo '*' deny
$ ntfy access patrolilo patrolilo write-only
$ # Generate an API token
$ ntfy token patrolilo
tk_zSuperSECRETtoken8jFW17GPoVTX

Save the API token to put into your config file.

Usage

Configuration

Install the binary, either with cargo install or by downloading it from the releases page.

Create a config file at $XDG_CONFIG_HOME/patrolilo/config.yml like this:

jobs:
  - name: Test
    script: |
      if docker ps | grep -qv unhealthy
      then
        echo all services healthy
      else
        # print some diagnostic info about what's wrong for the notification
        docker ps | grep unhealthy
        exit 1
      fi
    #     S M H D M DoW
    when: 0 * * * * *
  - name: Test fail
    script: |
      print("This is an example which uses a python script and always fails")
      exit(1)
    when: 0 */5 * * * *
    shell: python
notifications:
  server: ntfy.example.com
  channel: patrolilo
  auth key: tk_zSuperSECRETtoken8jFW17GPoVTX

By default all scripts are interpreted by the program at $SHELL, but a different shell may be specified per-job using the shell option. Note that the "when" field accepts an optional seconds field.

Systemd Service

To get the tool to run at boot, create a systemd user service. The simplest way is to run these commands:

$ mkdir -p $HOME/.config/systemd/user
$ curl https://codeberg.org/dscottboggs/patrolilo/src/branch/main/patrolilo.service \
  > $HOME/.config/systemd/user/patrolilo.service
$ systemctl --user enable --now patrolilo

The last command will immediately start the service and tell systemd to start it again on reboot. Run systemctl --user status patrolilo to make sure it is running well, and journalctl -xeu patrolilo to view the logs.

Under the hood

I'd like to give most of the credit for this to the tools that made this so simple to build, so let's take a peek under the hood.

Everything centers around the tokio-cron-scheduler crate, which gives us a super simple interface to create a list of scripts and tell them when to be run.

Of course, the NTFY notification service has been an incredible tool for me, a real game changer. No more SMTP or bots, just a simple reqwest.

If you have the time, I encourage you to take a look through main.rs, it's less than 100 lines and very straightforward. It's worth taking the time to appreciate all the wonderful tools this ecosystem has to offer.

Dependencies

~5.5–8MB
~121K SLoC