crono_trigger 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -0
- data/lib/crono_trigger/schedulable.rb +35 -5
- data/lib/crono_trigger/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efcd9ba0f357498e090d327a1b9ef3af54437501
|
4
|
+
data.tar.gz: ca3c3b5c144479f64c8cb6e39ea2904da63b8e2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ec6528498231a77d1a2b255e1cf04fe43ed0a035dac4009a3b8cdc099acb28d8a16663b0a793fe3aa27de9d5091c86dd3bdbaed9e9f77e01c1cd674da02a2a2
|
7
|
+
data.tar.gz: c0a0edce5e9601db48f9585bb88cd964a06bb81458bbce1d83f601a7d916247c5f4c60b3528de0ba434e8c12a53de0b889834016c6c36d4d785a4d86cb78f4d9
|
data/README.md
CHANGED
@@ -93,6 +93,23 @@ class MailNotification < ActiveRecord::Base
|
|
93
93
|
throw :ok_without_reset # break execution and handle task as success but without schedule reseting and unlocking
|
94
94
|
end
|
95
95
|
end
|
96
|
+
|
97
|
+
# one time schedule
|
98
|
+
MailNotification.create(next_execute_at: Time.current.since(5.minutes))
|
99
|
+
|
100
|
+
# cron schedule
|
101
|
+
MailNotification.create(cron: "0 12 * * *").activate_schedule!
|
102
|
+
# or
|
103
|
+
MailNotification.new(cron: "0 12 * * *").activate_schedule!.save
|
104
|
+
|
105
|
+
# if update cron column or timezone column
|
106
|
+
# update next_execute_at automatically by before_update callback
|
107
|
+
mail = MailNotification.create(cron: "0 12 * * *").activate_schedule!
|
108
|
+
mail.next_execute_at # => next 12:00 with Time.zone
|
109
|
+
mail.update(cron: "0 13 * * *")
|
110
|
+
mail.next_execute_at # => next 13:00 with Time.zone
|
111
|
+
mail.update(timezone: "Asia/Tokyo")
|
112
|
+
mail.next_execute_at # => next 13:00 with Asia/Japan
|
96
113
|
```
|
97
114
|
|
98
115
|
#### Run Worker
|
@@ -137,6 +154,10 @@ Usage: crono_trigger [options] MODEL [MODEL..]
|
|
137
154
|
You can rename some columns.
|
138
155
|
ex. `crono_trigger_options[:next_execute_at_column_name] = "next_time"`
|
139
156
|
|
157
|
+
## Rollbar integration
|
158
|
+
This gem has rollbar plugin.
|
159
|
+
If `crono_trigger/rollbar` is required, Add Rollbar logging process to `CronoTrigger.config.error_handlers`
|
160
|
+
|
140
161
|
## Development
|
141
162
|
|
142
163
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -46,7 +46,9 @@ module CronoTrigger
|
|
46
46
|
rel
|
47
47
|
end
|
48
48
|
|
49
|
-
|
49
|
+
before_update :update_next_execute_at_if_update_cron
|
50
|
+
|
51
|
+
validate :validate_cron_format
|
50
52
|
end
|
51
53
|
|
52
54
|
module ClassMethods
|
@@ -109,6 +111,18 @@ module CronoTrigger
|
|
109
111
|
raise
|
110
112
|
end
|
111
113
|
|
114
|
+
def activate_schedule!(at: Time.current)
|
115
|
+
time = calculate_next_execute_at || at
|
116
|
+
|
117
|
+
if new_record?
|
118
|
+
self[crono_trigger_column_name(:next_execute_at)] ||= time
|
119
|
+
else
|
120
|
+
unless self[crono_trigger_column_name(:next_execute_at)]
|
121
|
+
update_column(crono_trigger_column_name(:next_execute_at), time)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
112
126
|
def retry!
|
113
127
|
logger.info "Retry #{self.class}-#{id}" if logger
|
114
128
|
|
@@ -171,13 +185,29 @@ module CronoTrigger
|
|
171
185
|
def calculate_next_execute_at(now = Time.current)
|
172
186
|
if self[crono_trigger_column_name(:cron)]
|
173
187
|
tz = self[crono_trigger_column_name(:timezone)].try { |zn| TZInfo::Timezone.get(zn) }
|
174
|
-
|
175
|
-
|
188
|
+
base = [now, self[crono_trigger_column_name(:started_at)]].max
|
189
|
+
cron_now = tz ? base.in_time_zone(tz) : base
|
190
|
+
Chrono::NextTime.new(now: cron_now, source: self[crono_trigger_column_name(:cron)]).to_time
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def update_next_execute_at_if_update_cron
|
195
|
+
if changes[crono_trigger_column_name(:cron)] || changes[crono_trigger_column_name(:timezone)]
|
196
|
+
if self[crono_trigger_column_name(:cron)]
|
197
|
+
self[crono_trigger_column_name(:next_execute_at)] = calculate_next_execute_at
|
198
|
+
end
|
176
199
|
end
|
177
200
|
end
|
178
201
|
|
179
|
-
def
|
180
|
-
self[crono_trigger_column_name(:
|
202
|
+
def validate_cron_format
|
203
|
+
return unless self[crono_trigger_column_name(:cron)]
|
204
|
+
|
205
|
+
Chrono::NextTime.new(now: Time.current, source: self[crono_trigger_column_name(:cron)]).to_time
|
206
|
+
rescue Chrono::Fields::Base::InvalidField
|
207
|
+
self.errors.add(
|
208
|
+
crono_trigger_column_name(:cron).to_sym,
|
209
|
+
crono_trigger_options["invalid_field_error_message"] || "has invalid field"
|
210
|
+
)
|
181
211
|
end
|
182
212
|
|
183
213
|
def retry_limit
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crono_trigger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- joker1007
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chrono
|