crono_trigger 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f57e39321ff772c9fc7a02d4d63d19e991be0eb
4
- data.tar.gz: 72949a4a2b1984e90ff27c0acc0f5fc74818a947
3
+ metadata.gz: efcd9ba0f357498e090d327a1b9ef3af54437501
4
+ data.tar.gz: ca3c3b5c144479f64c8cb6e39ea2904da63b8e2d
5
5
  SHA512:
6
- metadata.gz: f537b832ef60d83e107946134f6d112d104e984cb6432ed3fcd9da2f95c38ca800c100bef471d8473f398fe18fab3cf2f21db000f921c0d7f9928a72dfa87a09
7
- data.tar.gz: f3268eabf16acfeb54aa39246d0315cc1c01f0668f7674d2f3b6e03607f41984a37d0ba0bd0dc51c33cd5d41b4dc3756df891e6179bd5604ff9243b3dcb944b5
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
- before_create :ensure_next_execute_at
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
- now = tz ? now.in_time_zone(tz) : now
175
- Chrono::NextTime.new(now: now, source: self[crono_trigger_column_name(:cron)]).to_time
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 ensure_next_execute_at
180
- self[crono_trigger_column_name(:next_execute_at)] ||= calculate_next_execute_at || Time.current
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
@@ -1,3 +1,3 @@
1
1
  module CronoTrigger
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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-06-30 00:00:00.000000000 Z
11
+ date: 2017-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chrono