belated 0.5.7 → 0.6.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: 0c1e102693d57c373c84b2f6a4fb5624770334b0fb15bc3cffa05541cf7bbe29
4
- data.tar.gz: bb18635e1ce9a04d83618cbd41fbdb91912c9b2f5c2d5f9dc6715154ef9254dc
3
+ metadata.gz: 4ca7f7d0a3a78ca41e6ba3afed7630e1ac72d4a88c4fdea08604066518c843a6
4
+ data.tar.gz: 2fb05c90501681f9f45ab4476cbdc4c1ac88de9addafaa1c54da0a16731426df
5
5
  SHA512:
6
- metadata.gz: 3465242fd5fe5a55ccd288cbc7d034ad6538e4bd75d8eeaa60c65b73e2f2a2c545ce63885172b85a8ec1e5c5a320dd51eaaf2038542bcc0f95da7a381973a217
7
- data.tar.gz: f889516ac99765cfb5aa744589c614e7d6c59b7488223214e1f8a8eb6da879fef0a0b66556609dd625a04dcba454300f1915b1e45ff01a68e2bc212ea4ed4857
6
+ metadata.gz: 5182420208c866b37511bda56cc5a2e8fddac8d87a23274923318f576b66f3d3848b33c6633296bbfed088e6bd339a458e92ccc057a63c8a815dfaf12aa3fdf7
7
+ data.tar.gz: 78ef5824e2afdd45910dbe81e79e54986dcc2c7ccf346515228c978a55c5d2aa0d0f29c4d894e219667ee6d6b735fbef43c94b61bd36d645a3e9ca1152bf6a86
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.6.0] - 2021-08-19
4
+
5
+ - Only need to keep references on the client side for procs. Not needed for classes, as they are pass-by-value. However, you can only pass procs by reference, so need to keep track of them. They're removed from the client side when they're completed though.
6
+ - The client is now a singleton. This is because it had some overhead when pushing the jobs to dRuby, so I took the approach of also doing that in a background thread. You however do not want more than one client to be running at the same time, so making it a singleton is the best option. Call the `.instance` method to get the singleton and then `.start` to get it started.
7
+
3
8
  ## [0.5.7] - 2021-08-18
4
9
 
5
10
  - Got errors under heavy load and restarting. Hopefully fixed by rescuing the DRb connection error.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- belated (0.5.7)
4
+ belated (0.6.0)
5
5
  drb
6
6
  dry-configurable
7
7
  sorted_set
data/README.md CHANGED
@@ -85,10 +85,17 @@ First, start up Belated.
85
85
  Then,
86
86
 
87
87
  ```ruby
88
- client = Belated::Client.new
88
+ # Get the client
89
+ client = Belated::Client.instance
90
+ # Start the client, only need to do this once
91
+ client.start unless client.started?
89
92
  ```
90
93
 
91
94
  and you can use the client!
95
+ Note that the client is a singleton.
96
+ This means that you can only have one client running at a time,
97
+ but it also means you only have one connection to dRuby, and that the number of threads in charge of queuing the jobs is only one.
98
+
92
99
  Call
93
100
 
94
101
  ```ruby
@@ -104,9 +111,7 @@ If you don't want the job to run right away, you can also pass it a keyword para
104
111
  client.perform_belated(job, Time.now + 1.month)
105
112
  ```
106
113
 
107
- Note that you probably want to memoize the client, as it always creates a 'banker thread' and there is the overhead of connecting to dRuby. Maybe even use it as a global!(`$client`).
108
-
109
- The client also holds references to the jobs that have been pushed so that they are not collected by GC.
114
+ The client also holds references to the jobs that are instances of `Proc` that have been pushed so that they are not collected by GC. This is because procs are passed by reference, and the client needs to keep them alive. They are removed from the list when the job is done.
110
115
 
111
116
  # Settings
112
117
 
data/lib/belated.rb CHANGED
@@ -113,7 +113,8 @@ class Belated
113
113
 
114
114
  def stop_workers
115
115
  @worker_list&.each do |worker|
116
- sleep 0.1 if worker.alive?
116
+ i = 0
117
+ sleep 0.1 while worker.alive? || (i + 0.1) < 10
117
118
  Thread.kill(worker)
118
119
  end
119
120
  @@queue.save_jobs
@@ -1,4 +1,5 @@
1
1
  require 'belated/job_wrapper'
2
+ require 'singleton'
2
3
  class Belated
3
4
  # The client class is responsible for managing the connection to the
4
5
  # DRb server. If it has no connection, it adds the jobs to a bank queue.
@@ -7,18 +8,25 @@ class Belated
7
8
  # client = Belated::Client.new
8
9
  # client.enqueue(JubJub.new, at: Time.now + 5.seconds)
9
10
  class Client
10
- attr_accessor :queue, :bank, :banker_thread, :table
11
+ include Singleton unless $TESTING
12
+
13
+ attr_accessor :queue, :bank, :banker_thread, :proc_table
11
14
 
12
15
  # Starts up the client.
13
16
  # Connects to the queue through DRb.
14
17
  # @return [void]
15
- def initialize
18
+ def start
16
19
  server_uri = Belated::URI
17
20
  DRb.start_service
18
- self.table = {}
21
+ self.proc_table = {}
19
22
  self.bank = Thread::Queue.new
20
23
  self.queue = DRbObject.new_with_uri(server_uri)
21
- start_banker_thread
24
+ @started = true
25
+ end
26
+ alias initialize start
27
+
28
+ def started?
29
+ @started
22
30
  end
23
31
 
24
32
  # Thread in charge of handling the bank queue.
@@ -28,24 +36,25 @@ class Belated
28
36
  def start_banker_thread
29
37
  self.banker_thread = Thread.new do
30
38
  loop do
31
- sleep 0.01
32
-
33
39
  delete_from_table
34
-
35
40
  if bank.empty?
36
41
  sleep 10
37
- else
38
- perform(bank.pop)
42
+ next
43
+ end
44
+ begin
45
+ queue.push(wrapper = bank.pop)
46
+ rescue DRb::DRbConnError
47
+ bank.push(wrapper)
39
48
  end
40
49
  end
41
50
  end
42
51
  end
43
52
 
44
53
  def delete_from_table
45
- return if table.empty?
54
+ return if proc_table.empty?
46
55
 
47
- table.select { |_k, v| v.completed }.each do |key, _value|
48
- table.delete(key)
56
+ proc_table.select { |_k, v| v.completed }.each do |key, _value|
57
+ proc_table.delete(key)
49
58
  end
50
59
  end
51
60
 
@@ -63,10 +72,10 @@ class Belated
63
72
  else
64
73
  JobWrapper.new(job: job, at: at, max_retries: max_retries)
65
74
  end
66
- queue.push(job_wrapper)
67
- table[job_wrapper.object_id] = job_wrapper
68
- rescue DRb::DRbConnError
69
75
  bank.push(job_wrapper)
76
+ proc_table[job_wrapper.object_id] = job_wrapper if job_wrapper.proc_klass
77
+ start_banker_thread if banker_thread.nil?
78
+ job_wrapper
70
79
  end
71
80
  alias perform_belated perform
72
81
  alias perform_later perform
@@ -13,7 +13,7 @@ class Belated
13
13
  class JobWrapper
14
14
  include Comparable
15
15
  include Logging
16
- attr_accessor :retries, :max_retries, :id, :job, :at, :completed
16
+ attr_accessor :retries, :max_retries, :id, :job, :at, :completed, :proc_klass
17
17
 
18
18
  def initialize(job:, max_retries: 5, at: nil)
19
19
  self.retries = 0
@@ -22,6 +22,7 @@ class Belated
22
22
  self.job = job
23
23
  self.at = at
24
24
  self.completed = false
25
+ self.proc_klass = job.instance_of?(Proc)
25
26
  end
26
27
 
27
28
  def <=>(other)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Belated
4
- VERSION = '0.5.7'
4
+ VERSION = '0.6.0'
5
5
  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.5.7
4
+ version: 0.6.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-18 00:00:00.000000000 Z
11
+ date: 2021-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: drb