Skip to content

Commit

Permalink
Add Scheduler Feature for Running Cron Jobs (#735)
Browse files Browse the repository at this point in the history
Support scheduler jobs
  • Loading branch information
kaplanelad authored Sep 12, 2024
1 parent 9578e93 commit 52e4f82
Show file tree
Hide file tree
Showing 28 changed files with 1,045 additions and 64 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci-generators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ jobs:
REDIS_URL: redis://localhost:${{job.services.redis.ports[6379]}}
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres_test

- name: scheduler
run: cargo run -- generate scheduler
working-directory: ./examples/demo
env:
REDIS_URL: redis://localhost:${{job.services.redis.ports[6379]}}
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres_test

- name: docker deployment
run: cargo run -- generate deployment
working-directory: ./examples/demo
Expand Down
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
tracing-appender = "0.2.3"

# cli/migrations
duct = "0.13.6"
duct = { version = "0.13.6" }
duct_sh = { version = "0.13.7" }

tower-http = { workspace = true }
byte-unit = "4.0.19"
Expand Down Expand Up @@ -123,6 +123,10 @@ object_store = { version = "0.10.2", default-features = false }
# cache
moka = { version = "0.12.7", features = ["sync"], optional = true }

# Scheduler
tokio-cron-scheduler = { version = "0.11.0", features = ["signal"] }
english-to-cron = {version = "0.1.2"}

[workspace.dependencies]
async-trait = { version = "0.1.74" }
axum = { version = "0.7.5", features = ["macros"] }
Expand Down
85 changes: 69 additions & 16 deletions examples/demo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions examples/demo/config/development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,32 @@ auth:
expiration: 604800 # 7 days
# </snip>

# Scheduler Jobs Configuration
# <snip id="configuration-scheduler">
scheduler:
# Location of shipping the command stdout and stderr.
output: stdout
# A list of jobs to be scheduled.
jobs:
# The name of the job.
write_content:
# by default false meaning executing the the run value as a task. if true execute the run value as shell command
shell: true
# command to run
run: "echo loco >> ./scheduler.txt"
# run the command as shell
# The cron expression defining the job's schedule.
cron: run every 1 second
output: silent
tags: ['base', 'infra']

run_task:
run: "foo"
cron: "at 10:00 am"

list_if_users:
run: "user_report"
shell: true
cron: "* 2 * * * *"
tags: ['base', 'users']
# </snip>
18 changes: 18 additions & 0 deletions examples/demo/config/scheduler.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
output: stdout
jobs:
write_content:
shell: true
run: "echo loco >> ./scheduler.txt"
cron: run every 1 second
output: silent
tags: ['base', 'infra']

run_task:
run: "foo"
cron: "at 10:00 am"

list_if_users:
run: "user_report"
shell: true
cron: "* 2 * * * *"
tags: ['base', 'users']
20 changes: 20 additions & 0 deletions examples/demo/config/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,23 @@ auth:
secret: PqRwLF2rhHe8J22oBeHy
# Token expiration time in seconds
expiration: 604800 # 7 days

scheduler:
output: stdout
jobs:
write_content:
shell: true
run: "echo loco >> ./scheduler.txt"
cron: run every 1 second
output: silent
tags: ['base', 'infra']

run_task:
run: "foo"
cron: "at 10:00 am"

list_if_users:
run: "user_report"
shell: true
cron: "* 2 * * * *"
tags: ['base', 'users']
18 changes: 10 additions & 8 deletions examples/demo/tests/cmd/cli.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ The one-person framework for Rust
Usage: blo-cli [OPTIONS] <COMMAND>

Commands:
start Start an app
db Perform DB operations
routes Describe all application endpoints
task Run a custom task
generate code generation creates a set of files and code templates based on a predefined set of rules
doctor Validate and diagnose configurations
version Display the app version
help Print this message or the help of the given subcommand(s)
start Start an app
db Perform DB operations
routes Describe all application endpoints
task Run a custom task
scheduler Run the scheduler
generate code generation creates a set of files and code templates based on a predefined set of rules
doctor Validate and diagnose configurations
version Display the app version
help Print this message or the help of the given subcommand(s)

Options:
-e, --environment <ENVIRONMENT> Specify the environment [default: development]
Expand Down Expand Up @@ -59,6 +60,7 @@ Commands:
scaffold Generates a CRUD scaffold, model and controller
controller Generate a new controller with the given controller name, and test file
task Generate a Task based on the given name
scheduler Generate a scheduler jobs configuration template
worker Generate worker
mailer Generate mailer
deployment Generate a deployment infrastructure
Expand Down
19 changes: 19 additions & 0 deletions examples/demo/tests/cmd/scheduler.trycmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
```console
$ LOCO_ENV=test blo-cli scheduler --list
# job_name cron tags run
1 list_if_users * 2 * * * * base, users "user_report"
2 run_task at 10:00 am - "foo"
3 write_content run every 1 second base, infra "echo loco >> ./scheduler.txt"


```

```console
$ LOCO_ENV=test blo-cli scheduler --config ./config/scheduler.yaml --list
# job_name cron tags run
1 list_if_users * 2 * * * * base, users "user_report"
2 run_task at 10:00 am - "foo"
3 write_content run every 1 second base, infra "echo loco >> ./scheduler.txt"


```
18 changes: 18 additions & 0 deletions snipdoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,21 @@ snippets:
migrate-down-n-command:
content: cargo loco db down 2
path: ./snipdoc.yml
scheduler-generate-command:
content: cargo loco generate scheduler
path: ./snipdoc.yml
scheduler-list-command:
content: cargo loco scheduler --list
path: ./snipdoc.yml
scheduler-list-from-file-command:
content: cargo loco scheduler --path config/scheduler.yaml --list
path: ./snipdoc.yml
scheduler-list-from-env-setting-command:
content: LOCO_ENV=production cargo loco scheduler --list
path: ./snipdoc.yml
scheduler-run-job-by-name-command:
content: LOCO_ENV=production cargo loco scheduler --name 'JOB_NAME'
path: ./snipdoc.yml
scheduler-run-job-by-tag-command:
content: LOCO_ENV=production cargo loco scheduler --tag 'maintenance'
path: ./snipdoc.yml
Loading

0 comments on commit 52e4f82

Please sign in to comment.