guru
Guru
Guru is a jobs processor based on Tasker. It allows to define job templates that can be run multiple times.
Installation
-
Add the dependency to your
shard.yml
:# ->>> shard.yml # ... dependencies: guru: github: GrottoPress/guru branch: master # ...
-
Run
shards install
-
In your app's bootstrap, require Guru:
# ->>> src/my_app.cr # ... require "guru" # ...
Usage
All jobs are required to define a #perform
method, indicating the actual work to be done.
At Jobs
An At job runs once, at a defined time:
# ->>> src/jobs/sample_at_job.cr
struct SampleAtJob
include Guru::AtJob
# Accepts either a `Time`, or a block that returns same.
run_at 1.minute.from_now
def initialize(@name : String)
end
private def perform
@name
end
end
Schedule this job thus:
# ->>> src/app.cr
SampleAtJob.run(name)
# OR
SampleAtJob.run(name) do |job, task|
# `job` is a `SampleAtJob` instance
# `task` is a `Tasker::OneShot` instance
# ...
end
Cron Jobs
A Cron job runs at times defined in a Unix cron format:
# ->>> src/jobs/sample_cron_job.cr
struct SampleCronJob
include Guru::CronJob
# Accepts either a `String`, or a block that returns same.
run_cron { "10 8 * * *" }
# Optionally indicate the time zone
#
# Accepts either a `Time::Location`, or a block that returns same.
timezone Time::Location.load("Africa/Accra")
def initialize(@name : String)
end
private def perform
@name
end
end
Schedule this job thus:
# ->>> src/app.cr
SampleCronJob.run(name)
# OR
SampleCronJob.run(name) do |job, task|
# `job` is a `SampleCronJob` instance
# `task` is a `Tasker::CRON` instance
# ...
end
Every Jobs
An Every job runs periodically, at a defined time interval:
# ->>> src/jobs/sample_every_job.cr
struct SampleEveryJob
include Guru::EveryJob
# Accepts either a `Time::Span`, or a block that returns same.
run_every { 1.minute }
def initialize(@name : String)
end
private def perform
@name
end
end
Schedule this job thus:
# ->>> src/app.cr
SampleEveryJob.run(name)
# OR
SampleEveryJob.run(name) do |job, task|
# `job` is a `SampleEveryJob` instance
# `task` is a `Tasker::Repeat` instance
# ...
end
In Jobs
An In job runs once, after a defined period has elapsed:
# ->>> src/jobs/sample_in_job.cr
struct SampleInJob
include Guru::InJob
# Accepts either a `Time::Span`, or a block that returns same.
run_in 1.minute
def initialize(@name : String)
end
private def perform
@name
end
end
Schedule this job thus:
# ->>> src/app.cr
SampleInJob.run(name)
# OR
SampleInJob.run(name) do |job, task|
# `job` is a `SampleInJob` instance
# `task` is a `Tasker::OneShot` instance
# ...
end
Now Jobs
A Now job runs once, immediately after it is scheduled:
# ->>> src/jobs/sample_now_job.cr
struct SampleNowJob
include Guru::NowJob
def initialize(@name : String)
end
private def perform
@name
end
end
Schedule this job thus:
# ->>> src/app.cr
SampleNowJob.run(name)
# OR
SampleNowJob.run(name) do |job, task|
# `job` is a `SampleNowJob` instance
# `task` is a `Tasker::OneShot` instance
# ...
end
Contributing
- Fork it
- Switch to the
master
branch:git checkout master
- Create your feature branch:
git checkout -b my-new-feature
- Make your changes, updating changelog and documentation as appropriate.
- Commit your changes:
git commit
- Push to the branch:
git push origin my-new-feature
- Submit a new Pull Request against the
GrottoPress:master
branch.
Security
Kindly report suspected security vulnerabilities in private, via contact details outlined in this repository's .security.txt
file.
guru
- 2
- 0
- 0
- 0
- 1
- almost 4 years ago
- February 13, 2021
Sun, 22 Dec 2024 02:34:34 GMT