postburner 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6faa3078340ee2bc8d90e931ee1db203114e32ff69b288ec69340e8b86a987cd
4
- data.tar.gz: 0a2ddf78a320e9b01c26a0e3fbd3158b78d37a89bca04a5fdd8c64b70eada1a7
3
+ metadata.gz: ffbffc2cebbe2e83bfff21178b71d0b205756cd6ef05c6dc30c8029d073c70d7
4
+ data.tar.gz: 68136a8de901340817dcd60e0fbd3b6794eb6dea2f8da386af138576dbba6334
5
5
  SHA512:
6
- metadata.gz: 5253309eb7aff502623040d49e923faff7c91df6aac407ec7a4e306a8317b1363db43a4b0408eb20fbdad6ee017ad68489480ac50d7e2ac68a612a1337488457
7
- data.tar.gz: 49d0253fe3ecde9cfd4de8b48a2ce95997245bea4310f7c751ccca3685fea8a42b29310a8058f222031ee8aa91f6ec61f990ec5c330157e39853c95830236cfa
6
+ metadata.gz: 319ebb136625b266efe9189c8f6114ee7153ad18503b6d38343609a8bcb62dc3ea814b4b22e60f685f90f68957a2c9647ff3c958e26983796fc2c40bb8cb981c
7
+ data.tar.gz: 0fb16c1e08994dc7676b1546dc9d492c51243a9bcf8ffa9a2367b0a4274ecd806c322f00b6304a637fbfa20ddb2bc0872bd5ccdb357bba1d518f60f589edbf46
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.5.0 - 2021-10-27
4
+
5
+ ### Added
6
+ - add mailer, action, params accessors to Mailer for easy access in subclassed jobs.
7
+ - add logging to Mailer perform.
8
+ - add save to #queue! job if unsaved
9
+ - add block with logging if queued_at isn't set or isn't prior to run time.
10
+
3
11
  ## v0.4.0 - 2021-10-12
4
12
 
5
13
  ### Added
@@ -1,4 +1,17 @@
1
1
  module Postburner
2
+ # Must implement a perform method, if an exception is raised the job
3
+ # doesn't complete.
4
+ #
5
+ # Job won't run unless queued_at is set, and is set to a time prior to
6
+ # the time the job runs.
7
+ #
8
+ # TODO Mailer uses ActiveJob::Arguments... probably should use that here
9
+ # as well. Decided how to migrate existing jobs or allow both - Opt to
10
+ # allow both: rescue from ActiveJob::DeserializationError and use the
11
+ # plain hash, probably log it too.
12
+ #
13
+ # Add `cancelled_at` that blocks jobs performing if present.
14
+ #
2
15
  class Job < ApplicationRecord
3
16
  include Backburner::Queue
4
17
 
@@ -17,6 +30,8 @@ module Postburner
17
30
  def queue!(options={})
18
31
  return if self.queued_at.present? && self.bkid.present?
19
32
 
33
+ self.save!
34
+
20
35
  case
21
36
  when options[:at].present?
22
37
  # this is rudimentary, add error handling
@@ -33,14 +48,15 @@ module Postburner
33
48
  # tube: backburner.worker.queue.backburner-jobs
34
49
  #
35
50
  def self.perform(id, _={})
51
+ job = nil
36
52
  begin
37
53
  job = self.find(id)
38
- job.perform!(job.args)
39
54
  rescue ActiveRecord::RecordNotFound => e
40
55
  Rails.logger.warn <<-MSG
41
56
  [Postburner::Job] [#{id}] Not Found.
42
57
  MSG
43
58
  end
59
+ job&.perform!(job.args)
44
60
  end
45
61
 
46
62
  def perform!(args={})
@@ -55,6 +71,16 @@ module Postburner
55
71
  )
56
72
 
57
73
  begin
74
+ if self.queued_at.nil?
75
+ self.log! "Not Queued", level: :error
76
+ return
77
+ end
78
+
79
+ if self.queued_at > Time.zone.now
80
+ self.log! "Future Queued", level: :error
81
+ return
82
+ end
83
+
58
84
  if self.processed_at.present?
59
85
  self.log! "Already Processed", level: :error
60
86
  self.delete!
@@ -179,7 +205,11 @@ module Postburner
179
205
  private
180
206
 
181
207
  def insert!(options={})
182
- response = Backburner::Worker.enqueue(Postburner::Job, self.id, options)
208
+ response = Backburner::Worker.enqueue(
209
+ Postburner::Job,
210
+ self.id,
211
+ options
212
+ )
183
213
 
184
214
  self.log("QUEUED: #{response}")
185
215
 
@@ -7,8 +7,8 @@ module Postburner
7
7
  class Mailer < Job
8
8
  #queue 'mailers'
9
9
 
10
- def self.deliver(mailer, action)
11
- job = self.create!(
10
+ def self.delivery(mailer, action)
11
+ job = self.new(
12
12
  args: {
13
13
  mailer: mailer.to_s,
14
14
  action: action.to_s,
@@ -17,32 +17,62 @@ module Postburner
17
17
  job
18
18
  end
19
19
 
20
+ def self.delivery!(mailer, action)
21
+ job = self.delivery(mailer, action)
22
+ job.save!
23
+ job
24
+ end
25
+
20
26
  # Similar to ActionMailer #with - set the parameters
21
27
  #
22
28
  def with(params={})
23
29
  self.args.merge!(
24
- params: ActiveJob::Arguments.serialize(params)
30
+ 'params' => ActiveJob::Arguments.serialize(params)
25
31
  )
32
+ self
33
+ end
34
+
35
+ def with!(params={})
36
+ self.with(params)
26
37
  self.save!
27
38
  self
28
39
  end
29
40
 
30
41
  # Build the mail but don't send.
31
42
  #
32
- def assemble(args=nil)
33
- _args = args || self.args
43
+ # Optional `args` argument for testing convenience.
44
+ #
45
+ def assemble
46
+ mail = self.mailer.with(self.params).send(self.action)
47
+ mail
48
+ end
34
49
 
35
- cls = _args['mailer'].constantize
50
+ # Get the mailer class.
51
+ #
52
+ def mailer
53
+ self.args['mailer'].constantize
54
+ end
36
55
 
37
- mail = cls.
38
- with(ActiveJob::Arguments.deserialize(_args['params']).to_h).
39
- send(_args['action'])
56
+ # Get the mailer action as a symbol.
57
+ #
58
+ def action
59
+ self.args['action']&.to_sym
60
+ end
40
61
 
41
- mail
62
+ # Get the deserialized params.
63
+ #
64
+ def params
65
+ ActiveJob::Arguments.deserialize(self.args['params']).to_h
42
66
  end
43
67
 
44
68
  def perform(args)
45
- self.assemble(args).deliver_now
69
+ self.log! "Building"
70
+ mail = self.assemble
71
+
72
+ self.log! "Delivering"
73
+ mail.deliver_now
74
+
75
+ self.log! "Delivered"
46
76
  end
47
77
  end
48
78
  end
@@ -1,3 +1,3 @@
1
1
  module Postburner
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
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.4.0
4
+ version: 0.5.0
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-12 00:00:00.000000000 Z
11
+ date: 2021-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails