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 +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +9 -4
- data/lib/belated.rb +2 -1
- data/lib/belated/client.rb +24 -15
- data/lib/belated/job_wrapper.rb +2 -1
- data/lib/belated/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ca7f7d0a3a78ca41e6ba3afed7630e1ac72d4a88c4fdea08604066518c843a6
|
4
|
+
data.tar.gz: 2fb05c90501681f9f45ab4476cbdc4c1ac88de9addafaa1c54da0a16731426df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/README.md
CHANGED
@@ -85,10 +85,17 @@ First, start up Belated.
|
|
85
85
|
Then,
|
86
86
|
|
87
87
|
```ruby
|
88
|
-
|
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
|
-
|
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
data/lib/belated/client.rb
CHANGED
@@ -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
|
-
|
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
|
18
|
+
def start
|
16
19
|
server_uri = Belated::URI
|
17
20
|
DRb.start_service
|
18
|
-
self.
|
21
|
+
self.proc_table = {}
|
19
22
|
self.bank = Thread::Queue.new
|
20
23
|
self.queue = DRbObject.new_with_uri(server_uri)
|
21
|
-
|
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
|
-
|
38
|
-
|
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
|
54
|
+
return if proc_table.empty?
|
46
55
|
|
47
|
-
|
48
|
-
|
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
|
data/lib/belated/job_wrapper.rb
CHANGED
@@ -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)
|
data/lib/belated/version.rb
CHANGED
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
|
+
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-
|
11
|
+
date: 2021-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: drb
|