Skip to content

Commit

Permalink
Add from: parameter to Mel::Job::Every#run_every methods
Browse files Browse the repository at this point in the history
This is used to specify the first run time.
  • Loading branch information
akadusei committed Dec 6, 2023
1 parent 00ea307 commit 65f907f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Added] -

### Changed
- Add `from:` parameter to `Mel::Job::Every#run_every` methods

## [0.17.2] - 2023-12-04

### Fixed
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ This makes *Redis* the *source of truth* for schedules, allowing to easily scale
DoSomeWork.run_every(10.minutes, for: 1.hour, arg_1: 5, arg_2: "value")
```

This will do the first run 10 minutes from now. If you would like to do the first run some other time, specify that in a `from:` argument:

```crystal
# ->>> src/app/some_file.cr
DoSomeWork.run_every(10.minutes, from: Time.local, for: 1.hour, arg_1: 5, arg_2: "value")
```

Instead of `for:`, you may use `till:` and specify a `Time`. Leave those out to run forever.

- Run on a Cron schedule:
Expand Down
24 changes: 24 additions & 0 deletions spec/mel/job/every_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ describe Mel::Job::Every do
Mel::PeriodicTask.find(id).should be_a(Mel::PeriodicTask)
end

it "starts at specified time" do
address = "user@domain.tld"
id = "1001"

SendEmailEveryJob.run_every(
2.hours,
from: 10.days.from_now,
id: id,
address: address
)

Time::Location.local = Time::Location.load("Europe/Berlin")

Timecop.travel(6.days.from_now) do
task = Mel::PeriodicTask.find(id)
task.try(&.due?).should be_false
end

Timecop.travel(10.days.from_now) do
task = Mel::PeriodicTask.find(id)
task.try(&.due?).should be_true
end
end

it "deletes task after given time" do
address = "user@domain.tld"
id = "1001"
Expand Down
6 changes: 4 additions & 2 deletions src/mel/job/every.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@ module Mel::Job::Every
def self.run_every(
interval : Time::Span,
for : Time::Span,
from : Time? = nil,
id = UUID.random.hexstring,
retries = nil,
redis = nil,
force = false,
**job_args
)
till = for.from_now
run_every(interval, till, id, retries, redis, force, **job_args)
run_every(interval, till, from, id, retries, redis, force, **job_args)
end

def self.run_every(
interval : Time::Span,
till : Time? = nil,
from : Time? = nil,
id = UUID.random.hexstring,
retries = nil,
redis = nil,
force = false,
**job_args
) : String?
time = from || interval.abs.from_now
job = new(**job_args)
time = interval.abs.from_now

task = Mel::PeriodicTask.new(
id.to_s,
Expand Down

0 comments on commit 65f907f

Please sign in to comment.