postburner 0.5.0 → 0.6.3

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
  SHA256:
3
- metadata.gz: ffbffc2cebbe2e83bfff21178b71d0b205756cd6ef05c6dc30c8029d073c70d7
4
- data.tar.gz: 68136a8de901340817dcd60e0fbd3b6794eb6dea2f8da386af138576dbba6334
3
+ metadata.gz: a63bf3c67a22be147995ab0fbe4464052a3d1aa733a537154045cd9f877659e4
4
+ data.tar.gz: cada05ac83afe59868738069c37b2d0cd4146fccff113f8652457c63101de6e6
5
5
  SHA512:
6
- metadata.gz: 319ebb136625b266efe9189c8f6114ee7153ad18503b6d38343609a8bcb62dc3ea814b4b22e60f685f90f68957a2c9647ff3c958e26983796fc2c40bb8cb981c
7
- data.tar.gz: 0fb16c1e08994dc7676b1546dc9d492c51243a9bcf8ffa9a2367b0a4274ecd806c322f00b6304a637fbfa20ddb2bc0872bd5ccdb357bba1d518f60f589edbf46
6
+ metadata.gz: 1ef155f03796765abe928ca1bf4bad33d02baa477b89228ceb6a60b7dfd8f0fb53c971514f768f37d2da5d1c6e448979c1518d4432f41df52996cc0f7c5ec970
7
+ data.tar.gz: 195c508533acc42cef7d680b60af1ac2f7c29648ce4a3df5b2ba0cf866d2879d9410ea0f35f528d55ca173e5d02d5f536252621c935a557620fd0250d8f708e1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.6.3 - 2021-11-02
4
+
5
+ ### Fixed
6
+ - fix #remove! to allow removal even if no beanstalkd job present.
7
+
8
+ ## v0.6.2 - 2021-11-01
9
+
10
+ ### Fixed
11
+ - add #requeue method.
12
+ - check if AlreadyProcessed.
13
+
14
+ ## v0.6.1 - 2021-11-01
15
+
16
+ ### Updated
17
+ - update run_at logic to support only inserting after save commit.
18
+
19
+ ## v0.6.0 - 2021-11-01
20
+
21
+ ### Added
22
+ - update exception handling to better show where the error was raised.
23
+ - update logging to match errata format.
24
+ - update documentation.
25
+
3
26
  ## v0.5.0 - 2021-10-27
4
27
 
5
28
  ### Added
data/README.md CHANGED
@@ -47,6 +47,17 @@ RunDonation.create!(args: {donation_id: 123}).queue! delay: 1.hour
47
47
  => {:status=>"INSERTED", :id=>"1141"}
48
48
  ```
49
49
 
50
+ ### Mailers
51
+
52
+ ```ruby
53
+ j = Postburner::Mailer.
54
+ delivery(UserMailer, :welcome)
55
+ .with(name: 'Freddy')
56
+
57
+ j.queue!
58
+ => {:status=>"INSERTED", :id=>"1139"}
59
+ ```
60
+
50
61
  ### [Beaneater](https://github.com/beanstalkd/beaneater) and [beanstalkd](https://raw.githubusercontent.com/beanstalkd/beanstalkd/master/doc/protocol.txt) attributes and methods
51
62
  ```ruby
52
63
  # get the beanstalkd job id
@@ -24,25 +24,42 @@ module Postburner
24
24
 
25
25
  before_validation :ensure_sid!
26
26
  before_destroy :delete!
27
+ after_save_commit :insert_if_queued!
27
28
 
28
29
  validates :sid, presence: {strict: true}
29
30
 
30
31
  def queue!(options={})
31
32
  return if self.queued_at.present? && self.bkid.present?
33
+ raise ActiveRecord::RecordInvalid, "Can't queue unless valid." unless self.valid?
34
+ raise AlreadyProcessed, "Processed at #{self.processed_at}" if self.processed_at
35
+
36
+ at = options.delete(:at)
37
+ now = Time.zone.now
38
+
39
+ self.queued_at = now
40
+ self.run_at = case
41
+ when at.present?
42
+ # this is rudimentary, add error handling
43
+ options[:delay] ||= at.to_i - now.to_i
44
+ at
45
+ when options[:delay].present?
46
+ now + options[:delay].seconds
47
+ end
48
+
49
+ @_insert_options = options
32
50
 
33
51
  self.save!
52
+ end
34
53
 
35
- case
36
- when options[:at].present?
37
- # this is rudimentary, add error handling
38
- options[:delay] ||= options[:at].to_i - Time.zone.now.to_i
39
- update_column :run_at, options[:at]
40
- options.delete(:at)
41
- when options[:delay].present?
42
- update_column :run_at, Time.zone.now + options[:delay].seconds
43
- end
54
+ def requeue!(options={})
55
+ self.delete!
56
+ self.bkid, self.queued_at = nil, nil
57
+
58
+ self.queue! options
59
+ end
44
60
 
45
- insert!(options)
61
+ def will_insert?
62
+ @_insert_options.is_a? Hash
46
63
  end
47
64
 
48
65
  # tube: backburner.worker.queue.backburner-jobs
@@ -98,22 +115,24 @@ module Postburner
98
115
  return
99
116
  end
100
117
 
101
- self.log!('START')
118
+ self.log!("START (bkid #{self.bkid})")
102
119
 
103
- self.perform(args)
120
+ begin
121
+ self.perform(args)
122
+ rescue Exception => exception
123
+ self.persist_metadata!
124
+ self.log! '[Postburner] Exception raised during perform prevented completion.'
125
+ raise exception
126
+ end
104
127
 
105
- self.log!('DONE')
128
+ self.log!("DONE (bkid #{self.bkid})")
106
129
 
107
130
  begin
108
131
  now = Time.zone.now
109
132
  _duration = (now - self.processing_at) * 1000 rescue nil
110
- self.update_columns(
133
+ persist_metadata!(
111
134
  processed_at: now,
112
135
  duration: _duration,
113
- errata: self.errata,
114
- error_count: self.errata.length,
115
- logs: self.logs,
116
- log_count: self.logs.length,
117
136
  )
118
137
  rescue Exception => e
119
138
  self.log_exception!(e)
@@ -123,7 +142,6 @@ module Postburner
123
142
 
124
143
  rescue Exception => exception
125
144
  self.log_exception!(exception)
126
- self.log! '[Postburner] Exception raised during perform prevented completion.'
127
145
  raise exception
128
146
  end
129
147
 
@@ -148,12 +166,9 @@ module Postburner
148
166
  end
149
167
 
150
168
  def remove!
151
- if self.beanstalk_job
152
- self.delete!
153
- self.update_column(:removed_at, Time.zone.now)
154
- else
155
- false
156
- end
169
+ return if self.removed_at
170
+ self.delete!
171
+ self.update_column(:removed_at, Time.zone.now)
157
172
  end
158
173
 
159
174
  def beanstalk_job
@@ -174,6 +189,7 @@ module Postburner
174
189
  self.errata << [
175
190
  Time.zone.now,
176
191
  {
192
+ bkid: self.bkid,
177
193
  class: exception.class,
178
194
  message: exception.message,
179
195
  backtrace: exception.backtrace,
@@ -191,34 +207,67 @@ module Postburner
191
207
  options[:level] = :error unless LOG_LEVELS.member?(options[:level])
192
208
 
193
209
  self.logs << [
194
- Time.zone.now,
195
- options[:level],
196
- message,
210
+ Time.zone.now, # time
211
+ {
212
+ bkid: self.bkid,
213
+ level: options[:level], # level
214
+ message: message, # message
215
+ elapsed: self.elapsed_ms, # ms from start
216
+ }
197
217
  ]
198
218
  end
199
219
 
220
+ # ms from attempting_at
221
+ #
222
+ def elapsed_ms
223
+ return unless self.attempting_at
224
+ (Time.zone.now - self.attempting_at) * 1000
225
+ end
226
+
200
227
  def log!(message, options={})
201
228
  self.log(message, options)
202
229
  self.update_column :logs, self.logs
203
230
  end
204
231
 
232
+ def intended_at
233
+ self.run_at ? self.run_at : self.queued_at
234
+ end
235
+
236
+ class AlreadyProcessed < StandardError; end
237
+
205
238
  private
206
239
 
240
+ def persist_metadata!(data={})
241
+ self.update_columns({
242
+ errata: self.errata,
243
+ error_count: self.errata.length,
244
+ logs: self.logs,
245
+ log_count: self.logs.length,
246
+ }.merge(data))
247
+ end
248
+
249
+ def insert_if_queued!
250
+ return unless self.will_insert?
251
+ insert!(@_insert_options)
252
+ end
253
+
207
254
  def insert!(options={})
208
- response = Backburner::Worker.enqueue(
209
- Postburner::Job,
210
- self.id,
211
- options
212
- )
255
+ response = nil
256
+
257
+ Job.transaction do
258
+ response = Backburner::Worker.enqueue(
259
+ Postburner::Job,
260
+ self.id,
261
+ options
262
+ )
263
+
264
+ persist_metadata!(
265
+ bkid: response[:id],
266
+ )
267
+ end
213
268
 
214
269
  self.log("QUEUED: #{response}")
215
270
 
216
- update_columns(
217
- queued_at: Time.zone.now,
218
- bkid: response[:id],
219
- logs: self.logs,
220
- )
221
-
222
271
  response
223
272
  end
224
273
 
@@ -226,14 +275,10 @@ module Postburner
226
275
  now = Time.zone.now
227
276
  self.attempts << now
228
277
  self.attempting_at ||= now
229
- self.lag ||= (self.attempting_at - self.intended_at) * 1000
278
+ self.lag ||= (self.attempting_at - self.intended_at) * 1000 rescue nil
230
279
  now
231
280
  end
232
281
 
233
- def intended_at
234
- self.run_at ? self.run_at : self.queued_at
235
- end
236
-
237
282
  def ensure_sid!
238
283
  self.sid ||= SecureRandom.uuid
239
284
  end
@@ -1,3 +1,3 @@
1
1
  module Postburner
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postburner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-27 00:00:00.000000000 Z
11
+ date: 2021-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails