belated 0.5.6 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/Gemfile.lock +1 -1
- data/README.md +14 -9
- data/lib/belated.rb +8 -6
- data/lib/belated/client.rb +36 -22
- data/lib/belated/job_wrapper.rb +2 -1
- data/lib/belated/version.rb +1 -1
- data/lib/belated/worker.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: 422e1b6b1f3973315fb01e1e47b7a976d0ac1a62897c0d32880fd21a7dec8c92
|
4
|
+
data.tar.gz: 3daed6a19c67909b50bb93c8b39ba729b43d33dbbd3a6966ed425249c6eec89f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 489ee717aaf5a1cbdf26f134e4341894dd088d4ba067f9b0f16c9c64a8f4692252e98b4dfa21d0e45fe45ba8c8cbfda77118483758bc0649b031bf8d9049c785
|
7
|
+
data.tar.gz: 811c5bc89bdbafaee6869a8f267e0c748ef5ca98ccc10edd65780e1a548205471ad881c879e12dc8e6162331e7065d8afb4bd765c96c694de8748600854f6feb
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,21 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.6.2] - 2021-08-20
|
3
4
|
|
5
|
+
- Use a mutex for the proc_table used by the client. Fixes
|
6
|
+
`RuntimeError: can't add a new key into hash during iteration (Most recent call first)`, so starting improving thread safety with this fix.
|
7
|
+
|
8
|
+
## [0.6.1] - 2021-08-20
|
9
|
+
|
10
|
+
When the client closes and worker has a reference to a proc, a `DRb::ConnError` is raised. Rescueing it and ignoring it.
|
11
|
+
## [0.6.0] - 2021-08-19
|
12
|
+
|
13
|
+
- 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.
|
14
|
+
- 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.
|
15
|
+
|
16
|
+
## [0.5.7] - 2021-08-18
|
17
|
+
|
18
|
+
- Got errors under heavy load and restarting. Hopefully fixed by rescuing the DRb connection error.
|
4
19
|
## [0.5.6] - 2021-08-17
|
5
20
|
|
6
21
|
- Now the client has a hash table that holds references to the objects you push through it. This is to get by GC, otherwise the objects are collected on clientside, but are still referenced on server side. This means that you do not want to use two instances of Client at the same time. Also, might need to write a way to close the client...
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -14,15 +14,15 @@ Note that currently the timezone is hardcoded to UTC.
|
|
14
14
|
|
15
15
|
Can be used with or without Rails.
|
16
16
|
|
17
|
+
Can be used if you're on a normal instance such as EC2 or Digital Ocean drop. Not if you're on a Heroku or Docker, or anything with ephemeral storage.
|
18
|
+
|
17
19
|
TODO LIST:
|
18
20
|
|
19
|
-
-
|
20
|
-
-
|
21
|
-
- Don't use class instance variables.
|
21
|
+
- Improve thread safety.
|
22
|
+
- Use GDBM for queue storage? That way could maybe get rid of YAML dumping and make things a bit safer. Not ordered though, so maybe keep a list of the jobs as YAML and update it sometimes? Just as backup. Or RocksDB?
|
22
23
|
- Make DRb port configurable.
|
23
|
-
- Don't hardcode timezone.
|
24
|
+
- Don't hardcode timezone to UTC.
|
24
25
|
- Add some checks to the client for proper jobs.
|
25
|
-
- Have multiple queues?
|
26
26
|
- Maybe support ActiveJob?
|
27
27
|
- Have a web UI.
|
28
28
|
- Have a job history
|
@@ -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
@@ -65,9 +65,9 @@ class Belated
|
|
65
65
|
@@queue.push(:shutdown)
|
66
66
|
end
|
67
67
|
Thread.new { stop_workers }
|
68
|
-
# Max
|
68
|
+
# Max 40 seconds to shutdown
|
69
69
|
timeout = 0
|
70
|
-
until (timeout += 0.1) >=
|
70
|
+
until (timeout += 0.1) >= 40 || @@queue.empty? || $TESTING
|
71
71
|
sleep 0.1
|
72
72
|
end
|
73
73
|
exit
|
@@ -89,7 +89,6 @@ class Belated
|
|
89
89
|
end
|
90
90
|
|
91
91
|
def enqueue_future_jobs
|
92
|
-
log 'starting future jobs thread'
|
93
92
|
loop do
|
94
93
|
sleep 0.1
|
95
94
|
job = @@queue.future_jobs.min
|
@@ -97,11 +96,13 @@ class Belated
|
|
97
96
|
sleep 5
|
98
97
|
next
|
99
98
|
end
|
100
|
-
|
101
99
|
if job.at <= Time.now.utc
|
102
100
|
log "Deleting #{@@queue.future_jobs.delete(job)} from future jobs"
|
103
101
|
@@queue.push(job)
|
104
102
|
end
|
103
|
+
rescue DRb::DRbConnError
|
104
|
+
log 'DRb connection error!!!!!!'
|
105
|
+
log stats
|
105
106
|
end
|
106
107
|
end
|
107
108
|
|
@@ -112,7 +113,8 @@ class Belated
|
|
112
113
|
|
113
114
|
def stop_workers
|
114
115
|
@worker_list&.each do |worker|
|
115
|
-
|
116
|
+
i = 0
|
117
|
+
sleep 0.1 while worker.alive? || (i + 0.1) < 10
|
116
118
|
Thread.kill(worker)
|
117
119
|
end
|
118
120
|
@@queue.save_jobs
|
@@ -138,7 +140,7 @@ class Belated
|
|
138
140
|
|
139
141
|
def banner_and_info
|
140
142
|
log banner
|
141
|
-
log "Currently running Belated version #{Belated::VERSION}"
|
143
|
+
log "Currently running Belated version #{Belated::VERSION} in #{Belated.config.environment}"
|
142
144
|
log %(Belated running #{@worker_list&.length.to_i} workers on #{URI}...)
|
143
145
|
end
|
144
146
|
|
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,26 @@ 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
|
+
@mutex = Mutex.new
|
26
|
+
end
|
27
|
+
alias initialize start
|
28
|
+
|
29
|
+
def started?
|
30
|
+
@started
|
22
31
|
end
|
23
32
|
|
24
33
|
# Thread in charge of handling the bank queue.
|
@@ -28,24 +37,27 @@ class Belated
|
|
28
37
|
def start_banker_thread
|
29
38
|
self.banker_thread = Thread.new do
|
30
39
|
loop do
|
31
|
-
sleep 0.01
|
32
|
-
|
33
40
|
delete_from_table
|
34
|
-
|
35
41
|
if bank.empty?
|
36
42
|
sleep 10
|
37
|
-
|
38
|
-
|
43
|
+
next
|
44
|
+
end
|
45
|
+
begin
|
46
|
+
queue.push(wrapper = bank.pop)
|
47
|
+
rescue DRb::DRbConnError
|
48
|
+
bank.push(wrapper)
|
39
49
|
end
|
40
50
|
end
|
41
51
|
end
|
42
52
|
end
|
43
53
|
|
44
54
|
def delete_from_table
|
45
|
-
return if
|
55
|
+
return if proc_table.empty?
|
46
56
|
|
47
|
-
|
48
|
-
|
57
|
+
proc_table.select { |_k, v| v.completed }.each do |key, _value|
|
58
|
+
@mutex.synchronize do
|
59
|
+
proc_table.delete(key)
|
60
|
+
end
|
49
61
|
end
|
50
62
|
end
|
51
63
|
|
@@ -56,23 +68,25 @@ class Belated
|
|
56
68
|
# @param max_retries [Integer] - Times the job should be retried if it fails.
|
57
69
|
# @return [JobWrapper] - The job wrapper for the queue.
|
58
70
|
def perform(job, at: nil, max_retries: 5)
|
59
|
-
|
60
|
-
|
61
|
-
job_wrapper = if job.is_a?(JobWrapper)
|
62
|
-
job
|
63
|
-
else
|
64
|
-
JobWrapper.new(job: job, at: at, max_retries: max_retries)
|
65
|
-
end
|
66
|
-
queue.push(job_wrapper)
|
67
|
-
table[job_wrapper.object_id] = job_wrapper
|
68
|
-
rescue DRb::DRbConnError
|
71
|
+
job_wrapper = wrap_job(job, at: at, max_retries: max_retries)
|
69
72
|
bank.push(job_wrapper)
|
73
|
+
@mutex.synchronize do
|
74
|
+
proc_table[job_wrapper.object_id] = job_wrapper if job_wrapper.proc_klass
|
75
|
+
end
|
76
|
+
start_banker_thread if banker_thread.nil?
|
77
|
+
job_wrapper
|
70
78
|
end
|
71
79
|
alias perform_belated perform
|
72
80
|
alias perform_later perform
|
73
81
|
|
74
82
|
private
|
75
83
|
|
84
|
+
def wrap_job(job, at:, max_retries:)
|
85
|
+
return job if job.is_a?(JobWrapper)
|
86
|
+
|
87
|
+
JobWrapper.new(job: job, at: at, max_retries: max_retries)
|
88
|
+
end
|
89
|
+
|
76
90
|
def drb_connected?
|
77
91
|
queue.connected?
|
78
92
|
rescue StandardError
|
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
data/lib/belated/worker.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.2
|
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-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: drb
|