belated 0.6.3 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae5f827cc80ba5fba59161a50ae5797ef774be28feeb478ea641dda6bca79b3a
4
- data.tar.gz: 01a61d9a44ae2b76ad27827e58dd8ba81e2040753925dc7172458c04495f010a
3
+ metadata.gz: 535f6d7f0af3565867c6f621fb328966d35ff524e38ffc22813e4410fd66eb2f
4
+ data.tar.gz: ace8cc58b9519d07e7bce2f12c5b1e45eba570f75a0a5f34b0496a5acfac679d
5
5
  SHA512:
6
- metadata.gz: 73a7fe76eacd1334186ef70628a78f193a20a72373d4faaa6bc9b8688ff0d9786156a69b44c3dfb9405837f0f5d980d1317277ad87c33d239d5d8c13b73207a4
7
- data.tar.gz: bcc797637c63a712e81b77189ecd93c325a67fb67f91b392a86d8231b685394341447539c12012280fee392a52a71a6fb20713fdb0ae0be32201c2b2670f5a33
6
+ metadata.gz: '058ed5f47971a9f39d7ec39997f23bdfeb54221b516ee9162330e9a0c783dc9ec6d7633ad029b7d96792e08bd317d5efced10ca2ea97b0e8322507a6e5f9a045'
7
+ data.tar.gz: c5ef2e3ad64ecda5e1fa2cb0feab2dda1b50388a523e571fe3be829af76e864973429bb95d0433353dd5a58bf97f2f366d19bd19a7ae0d6d578030646156f542
data/.rubocop.yml CHANGED
@@ -37,6 +37,9 @@ Style/BlockDelimiters:
37
37
  Style/ModuleFunction:
38
38
  EnforcedStyle: extend_self
39
39
 
40
+ Metrics/AbcSize:
41
+ Max: 17
42
+
40
43
  Metrics/BlockLength:
41
44
  Exclude:
42
45
  - 'spec/**/*.rb'
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.6.4] - 2021-08-22
4
+ - Inline jobs for testing!
5
+ ```ruby
6
+ `belated/testing`
7
+ Belated::Testing.inline!
8
+ ```
9
+ - Very much inspired by how Sidekiq is doing this.
10
+ - Read more in the testing part of README.md
11
+
3
12
  ## [0.6.3] - 2021-08-21
4
13
 
5
14
  - Needed to have the hash inside the mutex when going over it; otherwise you still the get can't add key into hash during iteration error. Of course.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- belated (0.6.3)
4
+ belated (0.6.4)
5
5
  drb
6
6
  dry-configurable
7
7
  sorted_set
data/README.md CHANGED
@@ -18,8 +18,7 @@ Can be used if you're on a normal instance such as EC2 or Digital Ocean drop. No
18
18
 
19
19
  TODO LIST:
20
20
 
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?
21
+ - 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? Would need to be configurable if you don't have something installed.
23
22
  - Make DRb port configurable.
24
23
  - Don't hardcode timezone to UTC.
25
24
  - Add some checks to the client for proper jobs.
@@ -140,6 +139,22 @@ Path to Rails project.
140
139
 
141
140
  Number of workers.
142
141
 
142
+ ## Testing
143
+
144
+ When testing, you can require `belated/testing` and then call `Belated::Testing.inline!` to make your jobs perform inline.
145
+
146
+ ```ruby
147
+ `belated/testing`
148
+ c = Belated::Client.instance
149
+ c.start
150
+ c.perform(proc { 2/ 1}) # Tries to push the job to the drb backend
151
+ # <Belated::JobWrapper:0x00005654bc2db1f0 @at=nil, @completed=false, @id="95e4dc6a-1876-4adf-ae0f-5ae902f5f024", @job=#<Proc:0x00005654bc2db330 (irb):3>, @max_retries=5, @proc_klass=true, @retries=0>
152
+ Belated::Testing.inline! # Sidekiq-inspired, now jobs run inline
153
+ c.perform(proc { 2/ 1}) # Returns 2 right away
154
+ # 2
155
+ Belated::Client.test_mode_off! # Turn off inline job processing
156
+ ```
157
+
143
158
  ## Development
144
159
 
145
160
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/belated.rb CHANGED
@@ -29,7 +29,7 @@ class Belated
29
29
  setting :rails_path, '.'
30
30
  setting :workers, 1
31
31
  setting :connect, true
32
- setting :environment, 'development'
32
+ setting :environment, 'development', reader: true
33
33
  setting :logger, Logger.new($stdout), reader: true
34
34
  setting :log_level, :info, reader: true
35
35
 
@@ -16,6 +16,8 @@ class Belated
16
16
  # Connects to the queue through DRb.
17
17
  # @return [void]
18
18
  def start
19
+ return if started?
20
+
19
21
  server_uri = Belated::URI
20
22
  DRb.start_service
21
23
  self.proc_table = {}
@@ -30,18 +32,22 @@ class Belated
30
32
  @started
31
33
  end
32
34
 
35
+ # Makes it possible to reset the client
36
+ def turn_off
37
+ @started = false
38
+ banker_thread&.kill
39
+ end
40
+
33
41
  # Thread in charge of handling the bank queue.
34
42
  # You probably want to memoize the client in order to avoid
35
43
  # having many threads in the sleep state.
36
44
  # @return [void]
37
45
  def start_banker_thread
38
- self.banker_thread = Thread.new do
46
+ Thread.new do
39
47
  loop do
40
- delete_from_table if proc_table.length > 20
41
- if bank.empty?
42
- sleep 10
43
- next
44
- end
48
+ delete_from_table
49
+ sleep 10 and next if bank.empty?
50
+
45
51
  until bank.empty?
46
52
  begin
47
53
  queue.push(wrapper = bank.pop)
@@ -55,7 +61,7 @@ class Belated
55
61
  end
56
62
 
57
63
  def delete_from_table
58
- return if proc_table.empty?
64
+ return if proc_table.length < 25
59
65
 
60
66
  @mutex.synchronize do
61
67
  proc_table.select { |_k, v| v.completed }.each do |key, _value|
@@ -71,12 +77,14 @@ class Belated
71
77
  # @param max_retries [Integer] - Times the job should be retried if it fails.
72
78
  # @return [JobWrapper] - The job wrapper for the queue.
73
79
  def perform(job, at: nil, max_retries: 5)
80
+ log 'Call .start on the client instance first!' unless started?
81
+
74
82
  job_wrapper = wrap_job(job, at: at, max_retries: max_retries)
75
83
  bank.push(job_wrapper)
76
84
  @mutex.synchronize do
77
85
  proc_table[job_wrapper.object_id] = job_wrapper if job_wrapper.proc_klass
78
86
  end
79
- start_banker_thread if banker_thread.nil?
87
+ self.banker_thread = start_banker_thread if banker_thread.nil?
80
88
  job_wrapper
81
89
  end
82
90
  alias perform_belated perform
@@ -0,0 +1,37 @@
1
+ class Belated
2
+ # Testing helpers
3
+ # Enable or disable testing
4
+ class Testing
5
+ @@testing = false
6
+
7
+ def self.inline?
8
+ @@testing == true
9
+ end
10
+
11
+ def self.inline!
12
+ @@testing = true
13
+ end
14
+
15
+ def self.test_mode_off!
16
+ @@testing = false
17
+ end
18
+ end
19
+ end
20
+
21
+ class Belated
22
+ # A client that can perform jobs inline
23
+ class Client
24
+ alias old_perform perform
25
+ def perform(job, at: nil, max_retries: 5)
26
+ if Belated::Testing.inline?
27
+ if job.respond_to?(:call)
28
+ job.call
29
+ else
30
+ job.perform
31
+ end
32
+ else
33
+ old_perform(job, at: at, max_retries: max_retries)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Belated
4
- VERSION = '0.6.3'
4
+ VERSION = '0.6.4'
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.6.3
4
+ version: 0.6.4
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-21 00:00:00.000000000 Z
11
+ date: 2021-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: drb
@@ -99,6 +99,7 @@ files:
99
99
  - lib/belated/logging.rb
100
100
  - lib/belated/queue.rb
101
101
  - lib/belated/rails.rb
102
+ - lib/belated/testing.rb
102
103
  - lib/belated/version.rb
103
104
  - lib/belated/worker.rb
104
105
  homepage: https://github.com/sampokuokkanen/belated