scheddy 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/scheddy/cli.rb +6 -0
- data/lib/scheddy/context.rb +2 -2
- data/lib/scheddy/scheduler.rb +3 -3
- data/lib/scheddy/task.rb +17 -6
- data/lib/scheddy/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adecf31e5c9eb6a72d45ea6b58ea0fa4a3788e32e9fe823d967f1881d4d54016
|
4
|
+
data.tar.gz: 68815c4b88e800c59b72f9fadb359b73ac4eea34db23ea03b7b8f6825f158f0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d9ccdbf34b264d353bdcdf95a9edc8f3780dcfd344c138ceb82692a046cd6a9311774e2c94dc1478fdbbfff3db6ea088f9cca41f46a3a638bbe3bf8a0dee1cb
|
7
|
+
data.tar.gz: 9b58cc75e7d3306350d3dc5a5a8a9ff7beb8216ee094ce0d707494f0e6c064eae886cf517bbb9bd66a76d4b2c758684c0a5c6a894d9099f5541af7100cdf887b
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@ Scheddy is a batteries-included task scheduler for Rails. It is intended as a re
|
|
6
6
|
* Tiny intervals are great for scheduling workload specific jobs (database field `next_run_at`).
|
7
7
|
* Catch up missed tasks. Designed for environments with frequent deploys. Also useful in dev where the scheduler isn't always running.
|
8
8
|
* Job-queue agnostic. Works great with various ActiveJob adapters and non-ActiveJob queues too.
|
9
|
-
* Minimal dependencies. Uses your existing database
|
9
|
+
* Minimal dependencies. Uses your existing database (or no database at all). Redis not required either.
|
10
10
|
* Tasks and their schedules are versioned as part of your code.
|
11
11
|
|
12
12
|
|
@@ -72,7 +72,7 @@ Scheddy.config do
|
|
72
72
|
# track_runs false # when run_every is >= 15.minutes, defaults to true; else to false
|
73
73
|
perform do
|
74
74
|
User.where(welcome_email_at: nil).find_each(batch_size: 100) do |user|
|
75
|
-
WelcomeMailer.
|
75
|
+
WelcomeMailer.with(user: user).welcome_email.deliver_later
|
76
76
|
end
|
77
77
|
end
|
78
78
|
end
|
@@ -136,7 +136,7 @@ A given task will only ever be executed once at a time. Mostly relevant when usi
|
|
136
136
|
|
137
137
|
Tasks may receive an optional context to check if they need to stop for pending shutdown or to know the deadline for completing work before the next cycle would begin.
|
138
138
|
|
139
|
-
Deadlines (`finish_before`) are mostly useful if there is occasionally a large block of work combined with tiny intervals.
|
139
|
+
Deadlines (`finish_before`) are mostly useful if there is occasionally a large block of work combined with tiny intervals. It may be necessary to calculate your own buffer when comparing against `finish_before` (1 second is shown below). As already mentioned, Scheddy is smart enough to skip the next cycle if the prior cycle is still running, so handling deadlines is entirely optional.
|
140
140
|
|
141
141
|
```ruby
|
142
142
|
task 'iterating task' do
|
@@ -145,7 +145,7 @@ task 'iterating task' do
|
|
145
145
|
Model.where(...).find_each do |model|
|
146
146
|
SomeJob.perform_later model.id if model.run_job?
|
147
147
|
break if context.stop? # the scheduler has requested to shutdown
|
148
|
-
break if context.finish_before <
|
148
|
+
break if context.finish_before < 1.second.from_now # the next cycle is imminent
|
149
149
|
end
|
150
150
|
end
|
151
151
|
end
|
data/lib/scheddy/cli.rb
CHANGED
data/lib/scheddy/context.rb
CHANGED
data/lib/scheddy/scheduler.rb
CHANGED
@@ -19,7 +19,7 @@ module Scheddy
|
|
19
19
|
running = tasks.select(&:running?).count
|
20
20
|
if running > 0
|
21
21
|
puts "[Scheddy] Waiting for #{running} tasks to complete"
|
22
|
-
wait_until(45.seconds.from_now) do
|
22
|
+
wait_until(45.seconds.from_now, skip_stop: true) do
|
23
23
|
tasks.none?(&:running?)
|
24
24
|
end
|
25
25
|
tasks.select(&:running?).each do |task|
|
@@ -73,9 +73,9 @@ module Scheddy
|
|
73
73
|
end
|
74
74
|
|
75
75
|
# &block - optional block - return truthy to end prematurely
|
76
|
-
def wait_until(time)
|
76
|
+
def wait_until(time, skip_stop: false)
|
77
77
|
while (now = Time.current) < time
|
78
|
-
return if stop?
|
78
|
+
return if stop? && !skip_stop
|
79
79
|
return if block_given? && yield
|
80
80
|
sleep [time-now, 1].min
|
81
81
|
end
|
data/lib/scheddy/task.rb
CHANGED
@@ -12,12 +12,16 @@ module Scheddy
|
|
12
12
|
logger.error "Scheddy task '#{name}' already running; skipping this cycle"
|
13
13
|
return next_cycle!
|
14
14
|
end
|
15
|
-
context = Context.new(scheduler,
|
15
|
+
context = Context.new(scheduler, self)
|
16
16
|
self.thread =
|
17
17
|
Thread.new do
|
18
18
|
logger.tagged tag do
|
19
19
|
Rails.application.reloader.wrap do
|
20
|
-
|
20
|
+
if context.finish_before < Time.current
|
21
|
+
logger.info "Rails dev-mode reloader locked for entire task interval; skipping this run"
|
22
|
+
else
|
23
|
+
task.call(*[context].take(task.arity.abs))
|
24
|
+
end
|
21
25
|
rescue Exception => e
|
22
26
|
if h = Scheddy.error_handler
|
23
27
|
h.call(*[e, self].take(h.arity.abs))
|
@@ -28,6 +32,12 @@ module Scheddy
|
|
28
32
|
self.thread = nil
|
29
33
|
end
|
30
34
|
next_cycle!
|
35
|
+
rescue Exception => e
|
36
|
+
logger.error "Scheddy: error scheduling task '#{name}'; retrying in 5 seconds"
|
37
|
+
if h = Scheddy.error_handler
|
38
|
+
h.call(*[e, self].take(h.arity.abs))
|
39
|
+
end
|
40
|
+
return self.next_cycle = 5.seconds.from_now
|
31
41
|
end
|
32
42
|
|
33
43
|
def kill
|
@@ -55,7 +65,7 @@ module Scheddy
|
|
55
65
|
}
|
56
66
|
case type
|
57
67
|
when :interval
|
58
|
-
attrs[:initial_delay] = ActiveSupport::Duration.build(delay) unless track_runs
|
68
|
+
attrs[:initial_delay] = ActiveSupport::Duration.build(delay) unless track_runs && last_run
|
59
69
|
attrs[:interval] = ActiveSupport::Duration.build(interval)
|
60
70
|
when :cron
|
61
71
|
attrs[:cron] = cron.original
|
@@ -129,14 +139,15 @@ module Scheddy
|
|
129
139
|
end
|
130
140
|
end
|
131
141
|
|
132
|
-
def finish_before
|
142
|
+
def finish_before(grace: 0.1.seconds)
|
133
143
|
case type
|
134
144
|
when :interval
|
135
|
-
Time.current + interval -
|
145
|
+
Time.current + interval - grace
|
136
146
|
when :cron
|
137
|
-
cron.next_time.to_utc_time -
|
147
|
+
cron.next_time.to_utc_time - grace
|
138
148
|
end
|
139
149
|
end
|
150
|
+
public :finish_before
|
140
151
|
|
141
152
|
|
142
153
|
def last_run
|
data/lib/scheddy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scheddy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thomas morgan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-06-
|
11
|
+
date: 2023-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fugit
|
@@ -85,6 +85,7 @@ licenses:
|
|
85
85
|
metadata:
|
86
86
|
homepage_uri: https://github.com/zarqman/scheddy
|
87
87
|
source_code_uri: https://github.com/zarqman/scheddy
|
88
|
+
changelog_uri: https://github.com/zarqman/scheddy/blob/master/CHANGELOG.md
|
88
89
|
post_install_message:
|
89
90
|
rdoc_options: []
|
90
91
|
require_paths:
|