Skip to content

Commit

Permalink
Events scheduling based on crontab format strings (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilosus committed Jul 31, 2023
1 parent e9b4840 commit 90c9180
Show file tree
Hide file tree
Showing 24 changed files with 1,032 additions and 118 deletions.
3 changes: 0 additions & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ DB__PORT_NUMBER=5432
DB__DATABASE_NAME=dienstplan
DB__USERNAME=dienstplan
DB__PASSWORD=dienstplan
DB__POOL_MIN_IDLE=1
DB__POOL_MAX_SIZE=1
DB__TIMEOUT_MS_CONNECTION=10000

# service: postgres
POSTGRES_DB=dienstplan
Expand Down
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ file. This change log follows the conventions of

## [Unreleased]

## [1.1.89] - 2023-07-31

### Added

- `schedule` command with
[vixie-cron](https://man7.org/linux/man-pages/man5/crontab.5.html)
format for scheduling
([#55](https://github.com/pilosus/dienstplan/issues/55))

### Changed

- Log level highlighting for plain text logging

## [1.0.83] - 2023-07-29

### Fixed
Expand Down Expand Up @@ -230,7 +243,8 @@ documentation website added. Project has made it to the version 1.0.0!
### Added
- Bot app MVP

[Unreleased]: https://github.com/pilosus/dienstplan/compare/1.0.83...HEAD
[Unreleased]: https://github.com/pilosus/dienstplan/compare/1.1.89...HEAD
[1.1.89]: https://github.com/pilosus/dienstplan/compare/1.0.83...1.1.89
[1.0.83]: https://github.com/pilosus/dienstplan/compare/1.0.82...1.0.83
[1.0.82]: https://github.com/pilosus/dienstplan/compare/1.0.81...1.0.82
[1.0.81]: https://github.com/pilosus/dienstplan/compare/1.0.0...1.0.81
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ migrate:
rollback:
docker compose run --rm --no-deps dienstplan clojure -X:rollback

schedule:
docker compose run --rm --no-deps dienstplan clojure -X:schedule

depscheck:
clojure -T:outdated

Expand All @@ -65,3 +68,5 @@ clean:

revcount:
@git rev-list HEAD --count
docs:
mkdocs serve
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# dienstplan: slack duty rotations made easy

[![codecov](https://codecov.io/gh/pilosus/dienstplan/branch/main/graph/badge.svg?token=2ouqzEwhLc)](https://codecov.io/gh/pilosus/dienstplan)
[![Docker version](https://img.shields.io/docker/v/pilosus/dienstplan/1.0.83?logo=docker&label=Docker)](https://hub.docker.com/r/pilosus/dienstplan/tags)
[![Docker version](https://img.shields.io/docker/v/pilosus/dienstplan/1.0.89?logo=docker&label=Docker)](https://hub.docker.com/r/pilosus/dienstplan/tags)

Slack bot for duty rotations.

Expand Down
2 changes: 1 addition & 1 deletion build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

(def lib 'org.pilosus/dienstplan)
(def main 'dienstplan.core)
(def version (format "1.0.%s" (b/git-count-revs nil)))
(def version (format "1.1.%s" (b/git-count-revs nil)))
(def class-dir "target/classes")
(def src-dirs ["src" "resources"])
(def basis (b/create-basis {:project "deps.edn"}))
Expand Down
13 changes: 10 additions & 3 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

{:paths ["src" "resources"]
:deps
{ ;; clojure
{ ;; Clojure
org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/tools.cli {:mvn/version "1.0.219"}

;; web framework
;; Web framework
ring/ring-core {:mvn/version "1.10.0"}
ring/ring-jetty-adapter {:mvn/version "1.10.0"}
ring/ring-json {:mvn/version "0.5.1"}
Expand All @@ -23,7 +23,7 @@
ch.qos.logback.contrib/logback-jackson {:mvn/version "0.1.5"}
com.fasterxml.jackson.core/jackson-databind {:mvn/version "2.15.2"}

;; alerts
;; Alerts
io.sentry/sentry-clj {:mvn/version "6.24.198"}

;; Validation
Expand All @@ -46,6 +46,9 @@
dev.weavejester/ragtime {:mvn/version "0.9.3"}
com.github.seancorfield/honeysql {:mvn/version "2.4.1045"}

;; Cron
org.pilosus/kairos {:mvn/version "0.1.14"}

;; Cryptography
buddy/buddy-core {:mvn/version "1.11.423"}}
:aliases
Expand All @@ -64,6 +67,10 @@
;; See other logging configs under resources/logback.*.xml
:jvm-opts ["-Dlogback.configurationFile=resources/logback.xml"]}

;; clojure -X:schedule
:schedule
{:exec-fn dienstplan.schedule/run}

;; Dev
:dev
{:extra-deps
Expand Down
9 changes: 9 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Dead simple: a few commands to manage on-call duty rotations in your team's Slack channels
- Follows the rule "Do One Thing and Do It Well"
- Plays nicely with Slack [reminders](https://slack.com/resources/using-slack/how-to-use-reminders-in-slack) and [workflows](https://slack.com/features/workflow-automation)
- Supports [crontab](https://man7.org/linux/man-pages/man5/crontab.5.html) for scheduling

## Quick example

Expand Down Expand Up @@ -59,3 +60,11 @@ Second, remind duties to a current on-call person:
```
/remind #my-channel to "@dienstplan who my-rota" every Monday, Tuesday, Wednesday, Thursday, Friday at 10AM UTC
```

If you prefer [crontab](https://en.wikipedia.org/wiki/Cron) format for
schedule description, use `schedule` command instead of `/remind`:

```
@dienstplan schedule create "rotate my-rota" 0 9 * * Mon
@dienstplan schedule create "who my-rota" 0 10 * * Mon-Fri
```
20 changes: 20 additions & 0 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ For database migrations and rollbacks instead of `java -jar app.jar
- `java -jar app.jar --mode migrate`
- `java -jar app.jar --mode rollback`

For schedules processing use:

- `java -jar app.jar --mode schedule`

It's recommended to use a SemVer tag matching the [latest
release](https://github.com/pilosus/dienstplan/releases) for a Docker
image (e.g. `pilosus/dienstplan:X.Y.Z`). Do not rely on the
Expand Down Expand Up @@ -103,6 +107,10 @@ For database migrations and rollbacks instead of `java -jar dienstplan-X.Y.Z-sta
- `java -jar dienstplan-X.Y.Z-standalone.jar --mode migrate`
- `java -jar dienstplan-X.Y.Z-standalone.jar --mode rollback`

For schedules processing use:

- `java -jar dienstplan-X.Y.Z-standalone.jar --mode schedule`

### Ansible Playbook

You can get a full set of installation scripts needed to:
Expand Down Expand Up @@ -133,6 +141,18 @@ Another way to run the app is with Clojure CLI:
clojure M:run
```

Schedules processing can be done:

```bash
make schedule
```

or

```bash
clojure -X:schedule
```

Don't forget to use envs to configure the app properly.

## Extra configs
Expand Down
56 changes: 55 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ a command and its arguments as follows:
`Rotation`, or `rota` - a named duty with a duties description and a
list of user mentions in the order of their duty schedule.

`Duty`, or `on-call person`, or `duty person` - a user who is currently an on-call person, i.e. on duty.
`Duty`, or `on-call person`, or `duty person` - a user who is
currently an on-call person, i.e. on duty.

`Mention` - a user tagged in Slack (username prepended with the `@`
character). Used interchargably with a word `User` or a phrase `User
mention`.

`Schedule` - a scheduled event that has an `executable` (a text
command as if it were sent by a user to the Slack bot) and a `crontab`
string (scheduling in the
[crontab](https://en.wikipedia.org/wiki/Cron) file format).

## Commands

The commands work on the Slack channel basis, meaning that a rotation
Expand Down Expand Up @@ -128,6 +134,54 @@ current channel:

Watch out! The list is limited to 500 rotations.

### Schedule

A meta-command to create, delete or list schedules.

```
@dienstplan schedule <subcommand> "<executable>" <crontab>
```

where:

- `<subcommand>` is one of: `[create, delete, list]`
- `"<executalbe>"` is a command for a bot to run on schedule
- `<crontab>` is a crontab file line in
[vixie-cron](https://man7.org/linux/man-pages/man5/crontab.5.html)
format, e.g. `0 9 * * Mon-Fri`

caveats:

`"<executable>"` must be enclosed in the double quotation marks!

#### Create

Create a new schedule in the channel:

```
@dienstplan schedule create "rotate my-rota" 0 7 * * Mon-Fri
```

Schedules are unique within a channel, i.e. there could be only a
single `rotate my-rota` in `my-channel`, no matter what `crontab` is
used for the schedule.

#### Delete

Delete a schedule in the channel:

```
@dienstplan schedule delete "rotate my-rota"
```

#### List

List all the schedules in the channel:

```
@dienstplan schedule list
```

### Help

Show a help message for the bot:
Expand Down
10 changes: 6 additions & 4 deletions resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<configuration debug="true" scan="true" scanPeriod="30 seconds">
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
<import class="ch.qos.logback.core.ConsoleAppender"/>
<appender name="STDOUT" class="ConsoleAppender">
<encoder class="PatternLayoutEncoder">
<charset>UTF-8</charset>
<pattern>%date{ISO8601} [%thread] %-5level %logger{36} - %msg%n</pattern>
<pattern>%date{ISO8601} [%thread] %highlight(%-5level) %logger{36} - %msg%n</pattern>
</encoder>
</appender>

Expand Down
16 changes: 16 additions & 0 deletions resources/migrations/0003-schedule.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{:up ["CREATE TABLE IF NOT EXISTS schedule (
id serial PRIMARY KEY,
channel VARCHAR (255) NOT NULL,
executable TEXT NOT NULL,
crontab TEXT NOT NULL,
run_at TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT (NOW() AT TIME ZONE 'UTC') NOT NULL,
updated_at TIMESTAMP DEFAULT (NOW() AT TIME ZONE 'UTC') NOT NULL
);"
"CREATE UNIQUE INDEX idx_schedule_channel_executable ON schedule (channel, executable);"
"CREATE INDEX idx_schedule_channel ON schedule (channel);"
"CREATE INDEX idx_schedule_run_at ON schedule (run_at);"]
:down ["DROP INDEX idx_schedule_run_at;"
"DROP INDEX idx_schedule_channel;"
"DROP INDEX idx_schedule_channel_executable;"
"DROP TABLE schedule;"]}
Loading

0 comments on commit 90c9180

Please sign in to comment.