belated 0.4.4 → 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: b946c5167a28af0cd73bad436d52c47ad19af699d88bd8a20c592b8fb2327ea1
4
- data.tar.gz: da03cfda6b0c4ec8b9a9a7ef4cb52747294f3b582633ad023cc98aa5e88c697f
3
+ metadata.gz: f63b50cb41ebaa52127b51ef4a38f70467bd6527d16efb6869965d5b3c9d3bab
4
+ data.tar.gz: 03ffefae27d2a9f76854b18abad3e2bd46fe3012e987008b5514fd86f699a5ba
5
5
  SHA512:
6
- metadata.gz: 5add925bbaca101aef9a35c8d4ae6cc544b736ebbbcbae4e6603faf0e42a0a248bd04844b0af8fa6dceb32f021948c105bf18031fd0c97d12cfc85ac9d9cde3f
7
- data.tar.gz: 13ac76b9f2b956e9b0b363d16ec3e615365ee39132fe788022c11c2ff90ed7fa9234c403903ac268efae0c63a51afbe6bc9893eb95cd8bbdf5d20b75e6a85aaf
6
+ metadata.gz: 619b6fc1269319b827dcfe48cc84caa951b00bea7fcd4086ebb73ab4ab72c431a3d2b02c2032e02e305913d2613c6b1f67be9c5f1edbd3a2432a0fdec3667c57
7
+ data.tar.gz: ccbed4ecf9df5627e28929c66287700160f3b9a31dd40b107f186eb1a8fe917cfc80edaa7b756743440d4c643ffa60b93cb06f463b06541929a62639b0fae1fb
data/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
3
 
4
+ ## [0.5.0] - 2021-08-07
5
+
6
+ - Job retries! The jobs now have ids, so you can follow the job and it's retries from the log.
7
+ - Quite a lot has changed internally, so if you were not using the Belated::Queue class to enqueue your jobs, you will need to update your code.
8
+
4
9
  ## [0.4.4] - 2021-08-07
5
10
 
6
11
  - Now if you pass something with a syntax error in it as a job, it should not bring down the whole app!
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- belated (0.4.4)
4
+ belated (0.5.0)
5
5
  drb
6
6
  dry-configurable
7
7
 
data/README.md CHANGED
@@ -10,16 +10,21 @@ Note that Belated used to be called HardWorker. That name was already in use in
10
10
 
11
11
  It uses dRuby to do the communication! Which is absolute great. No need for Redis or PostgreSQL, just Ruby standard libraries.
12
12
 
13
+ Note that currently the timezone is hardcoded to UTC.
14
+
13
15
  Can be used with or without Rails.
14
16
 
15
17
  TODO LIST:
16
18
 
17
- - Add retries for jobs
19
+ - Don't use class instance variables.
20
+ - Make port configurable.
21
+ - Don't hardcode timezone.
18
22
  - Add some checks to the client for proper jobs.
19
23
  - Have multiple queues?
20
24
  - Maybe support ActiveJob?
21
- - Have a web UI
22
- - Do some performance testing
25
+ - Have a web UI.
26
+ - Have a job history
27
+ - Do some performance testing.
23
28
  - Deploy a Rails app to production that is using Belated
24
29
  and mention it in the readme. (Capistrano support?)
25
30
  - Add a section telling people to use Sidekiq if they can
@@ -50,15 +55,6 @@ Then, in another program, connect to Belated and give it a job to do.
50
55
  Sample below:
51
56
 
52
57
  ```ruby
53
- class DummyWorker
54
- attr_accessor :queue
55
-
56
- def initialize
57
- server_uri = Belated::URI
58
- self.queue = DRbObject.new_with_uri(server_uri)
59
- end
60
- end
61
-
62
58
  class DumDum
63
59
  # classes need to have a perform method
64
60
  def perform
@@ -66,11 +62,14 @@ class DumDum
66
62
  end
67
63
  end
68
64
 
69
- # Need to start dRuby on the client side
70
- DRb.start_service
71
- dummy = DummyWorker.new
72
- dummy.queue.push(proc { 2 / 1 })
73
- dummy.queue.push(DumDum.new)
65
+ client = Belated::Client.new
66
+ client.perform_belated(proc { 2 / 1 })
67
+ client.perform_belated(DumDum.new)
68
+ # client.perform, client.perform_later are also good
69
+ # if you want to do something later:
70
+ client.perform_belated(DumDum.new, at: Time.now + 5 * 60)
71
+ # max retries:
72
+ client.perform_belated(DumDum.new, max_retries: 3) # default 5
74
73
  ```
75
74
 
76
75
  Belated runs on localhost, port 8788.
data/lib/belated.rb CHANGED
@@ -4,6 +4,7 @@ require_relative 'belated/logging'
4
4
  require_relative 'belated/version'
5
5
  require_relative 'belated/worker'
6
6
  require 'belated/client'
7
+ require 'belated/job_wrapper'
7
8
  require 'belated/queue'
8
9
  require 'drb'
9
10
  require 'dry-configurable'
@@ -91,9 +92,9 @@ class Belated
91
92
  log 'starting future jobs thread'
92
93
  loop do
93
94
  @@queue.future_jobs.each_with_index do |job, i|
94
- if job[:at] <= Time.now.utc
95
- log @@queue.future_jobs.delete_at(i)
96
- @@queue.push(job[:klass])
95
+ if job.at <= Time.now.utc
96
+ log "Deleting #{@@queue.future_jobs.delete_at(i)} from future jobs"
97
+ @@queue.push(job)
97
98
  end
98
99
  end
99
100
  sleep 0.01
@@ -155,12 +156,16 @@ class Belated
155
156
  @@queue.clear
156
157
  end
157
158
 
159
+ def self.fetch_job
160
+ @@queue.pop
161
+ end
162
+
158
163
  def job_list
159
164
  @@queue
160
165
  end
161
166
 
162
- def self.fetch_job
163
- @@queue.pop
167
+ def self.job_list
168
+ @@queue
164
169
  end
165
170
 
166
171
  class Error < StandardError; end
@@ -1,3 +1,4 @@
1
+ require 'belated/job_wrapper'
1
2
  class Belated
2
3
  # The client class is responsible for managing the connection to the
3
4
  # DRb server. If it has no connection, it adds the jobs to a bank queue.
@@ -25,9 +26,9 @@ class Belated
25
26
  def start_banker_thread
26
27
  self.banker_thread = Thread.new do
27
28
  loop do
28
- job, at = bank.pop
29
+ job = bank.pop
29
30
 
30
- perform(job, at: at)
31
+ perform(job)
31
32
  end
32
33
  end
33
34
  end
@@ -36,13 +37,20 @@ class Belated
36
37
  # If there is no connection, it pushes the job to the bank.
37
38
  # @param job [Object] - The the job to be pushed.
38
39
  # @param at [Date] - The time at which the job should be executed.
39
- # @return [Object] - The job that was pushed.
40
- def perform(job, at: nil)
41
- queue.push(job, at: at)
40
+ # @param max_retries [Integer] - Times the job should be retried if it fails.
41
+ # @return [JobWrapper] - The job wrapper for the queue.
42
+ def perform(job, at: nil, max_retries: 5)
43
+ job_wrapper = if job.is_a?(JobWrapper)
44
+ job
45
+ else
46
+ JobWrapper.new(job: job, at: at, max_retries: max_retries)
47
+ end
48
+ pp queue.push(job_wrapper)
49
+ job_wrapper
42
50
  rescue DRb::DRbConnError
43
- bank.push([job, at])
51
+ bank.push(job_wrapper)
44
52
  start_banker_thread if banker_thread.nil?
45
- # banker_thread.wakeup if banker_thread.status == 'sleep'
53
+ banker_thread.wakeup if banker_thread.status == 'sleep'
46
54
  end
47
55
  alias perform_belated perform
48
56
  alias perform_later perform
@@ -0,0 +1,44 @@
1
+ require 'securerandom'
2
+ require_relative 'logging'
3
+
4
+ class Belated
5
+ class JobWrapper
6
+ include Logging
7
+ attr_accessor :retries, :max_retries, :id, :job, :at
8
+
9
+ def initialize(job:, max_retries: 5, at: nil)
10
+ self.retries = 0
11
+ self.max_retries = max_retries
12
+ self.id = SecureRandom.uuid
13
+ self.job = job
14
+ self.at = at
15
+ end
16
+
17
+ # rubocop:disable Lint/RescueException
18
+ def perform
19
+ if job.respond_to?(:call)
20
+ job.call
21
+ else
22
+ job.perform
23
+ end
24
+ rescue Exception => e
25
+ case e.class
26
+ when Interrupt, SignalException
27
+ raise e
28
+ else
29
+ retry_job
30
+ "Error while executing job, #{e.inspect}. Retry #{retries} of #{max_retries}"
31
+ end
32
+ end
33
+ # rubocop:enable Lint/RescueException
34
+
35
+ def retry_job
36
+ self.retries += 1
37
+ return if retries > max_retries
38
+
39
+ self.at = Time.now.utc + (retries.next**4)
40
+ log "Job #{id} failed, retrying at #{at}"
41
+ Belated.job_list.push(self)
42
+ end
43
+ end
44
+ end
data/lib/belated/queue.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'belated/job'
4
4
  require 'belated/logging'
5
+ require 'belated/job_wrapper'
5
6
  class Belated
6
7
  class Queue
7
8
  include Logging
@@ -14,11 +15,11 @@ class Belated
14
15
  self.future_jobs = future_jobs
15
16
  end
16
17
 
17
- def push(job, at: nil)
18
- if at.nil?
18
+ def push(job)
19
+ if job.at.nil? || job.at <= Time.now.utc
19
20
  @queue.push(job)
20
21
  else
21
- @future_jobs << Job.new(job, at)
22
+ @future_jobs << job
22
23
  end
23
24
  end
24
25
 
@@ -45,7 +46,7 @@ class Belated
45
46
 
46
47
  jobs = YAML.load(File.binread(FILE_NAME))
47
48
  jobs.each do |job|
48
- if job.is_a?(Job)
49
+ if job.at && job.at > Time.now.utc
49
50
  future_jobs.push(job)
50
51
  else
51
52
  @queue.push(job)
@@ -73,7 +74,7 @@ class Belated
73
74
  private
74
75
 
75
76
  def proc_or_shutdown?(job)
76
- job.instance_of?(Proc) || job == :shutdown
77
+ job.job.instance_of?(Proc) || job == :shutdown
77
78
  end
78
79
  end
79
80
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Belated
4
- VERSION = '0.4.4'
4
+ VERSION = '0.5.0'
5
5
  end
@@ -18,25 +18,9 @@ class Belated
18
18
 
19
19
  break if job == :shutdown
20
20
 
21
- log call_job(job)
22
- end
23
- end
24
-
25
- # rubocop:disable Lint/RescueException
26
- def call_job(job)
27
- if job.respond_to?(:call)
28
- job.call
29
- else
21
+ log "Worker #{@number} got job: #{job.inspect}"
30
22
  job.perform
31
23
  end
32
- rescue Exception => e
33
- case e.class
34
- when Interrupt, SignalException
35
- raise e
36
- else
37
- e.inspect
38
- end
39
24
  end
40
- # rubocop:enable Lint/RescueException
41
25
  end
42
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: belated
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sampo Kuokkanen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-07 00:00:00.000000000 Z
11
+ date: 2021-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: drb
@@ -81,6 +81,7 @@ files:
81
81
  - lib/belated.rb
82
82
  - lib/belated/client.rb
83
83
  - lib/belated/job.rb
84
+ - lib/belated/job_wrapper.rb
84
85
  - lib/belated/logging.rb
85
86
  - lib/belated/queue.rb
86
87
  - lib/belated/rails.rb