belated 0.3.3 → 0.4.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: 639e202c5feb0ecb2af5bcd7f22815128f670f36c2aa793ce5bc0c107477a92f
4
- data.tar.gz: 65864414d040a9b1cd3e92897a97b5f596683c42cb6941c5cdbaee731502a622
3
+ metadata.gz: 3264ca317aa687386cf4f87b45a358b25197e0a8442d79c28116abbdf373cd5c
4
+ data.tar.gz: fc520d4de51b80b7ec87622f7ef66fd17a60423800a72a402fde7f7361e52a2e
5
5
  SHA512:
6
- metadata.gz: bda697be3405328560b19afbf9a90cc8cac0a8487e627ba0d6ee02ef2bbd6bf933cf3235fa312a1f817224ade6837da390d6794742a5110b255ca6052cafb838
7
- data.tar.gz: da4c2c0b49901ca28aed07eff2d279293c28a3121031500cc4846627d01619fbd24c52b0c382c8c4091d46238df53d827449c7092a85286072a1b74600402313
6
+ metadata.gz: 5512108388bf979973d471f9107b8f11f1af79320029324053ca748eeac3940635efdbff744410305e185e781de6732fa003ca7251d5d26be1c706e59818f960
7
+ data.tar.gz: 25045c09fbd423b0169cc4eda7a01a4cbf1e93c6a3ee13d0b574f24fabfd3fae9a58768eb6bef962b8613536c3585fb542e02151950eda82d6867c34a0d6e85f
data/.gitignore CHANGED
@@ -14,3 +14,4 @@
14
14
  /dummy/log/
15
15
  /dummy/log/test.log
16
16
  belated_dump
17
+ stackprof*
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0] - 2021-08-03
4
+
5
+ - Now you can enqueue jobs to be done at a later time. Just pass an `at:` keyword param to the client.
6
+ - Does not save the jobs when you quit.
7
+
3
8
  ## [0.3.3] - 2021-08-01
4
9
 
5
10
  - Shutdown trapped signal thread, make sure :shutdown is not recorded as a job.
data/Gemfile CHANGED
@@ -11,5 +11,8 @@ gem 'rspec', '~> 3.0'
11
11
 
12
12
  gem 'rubocop', '~> 1.7'
13
13
 
14
+ gem 'database_cleaner-active_record'
14
15
  gem 'rails', '>= 6.1.3'
16
+ gem 'rspec-rails'
15
17
  gem 'sqlite3'
18
+ gem 'stackprof'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- belated (0.3.3)
4
+ belated (0.4.0)
5
5
  drb
6
6
  dry-configurable
7
7
 
@@ -72,6 +72,10 @@ GEM
72
72
  byebug (11.1.3)
73
73
  concurrent-ruby (1.1.9)
74
74
  crass (1.0.6)
75
+ database_cleaner-active_record (2.0.1)
76
+ activerecord (>= 5.a)
77
+ database_cleaner-core (~> 2.0.0)
78
+ database_cleaner-core (2.0.1)
75
79
  diff-lcs (1.4.4)
76
80
  drb (2.0.4)
77
81
  dry-configurable (0.12.1)
@@ -145,6 +149,14 @@ GEM
145
149
  rspec-mocks (3.10.2)
146
150
  diff-lcs (>= 1.2.0, < 2.0)
147
151
  rspec-support (~> 3.10.0)
152
+ rspec-rails (5.0.1)
153
+ actionpack (>= 5.2)
154
+ activesupport (>= 5.2)
155
+ railties (>= 5.2)
156
+ rspec-core (~> 3.10)
157
+ rspec-expectations (~> 3.10)
158
+ rspec-mocks (~> 3.10)
159
+ rspec-support (~> 3.10)
148
160
  rspec-support (3.10.2)
149
161
  rubocop (1.18.3)
150
162
  parallel (~> 1.10)
@@ -166,6 +178,7 @@ GEM
166
178
  activesupport (>= 4.0)
167
179
  sprockets (>= 3.0.0)
168
180
  sqlite3 (1.4.2)
181
+ stackprof (0.2.16)
169
182
  thor (1.1.0)
170
183
  tzinfo (2.0.4)
171
184
  concurrent-ruby (~> 1.0)
@@ -181,11 +194,14 @@ PLATFORMS
181
194
  DEPENDENCIES
182
195
  belated!
183
196
  byebug
197
+ database_cleaner-active_record
184
198
  rails (>= 6.1.3)
185
199
  rake (~> 13.0)
186
200
  rspec (~> 3.0)
201
+ rspec-rails
187
202
  rubocop (~> 1.7)
188
203
  sqlite3
204
+ stackprof
189
205
 
190
206
  BUNDLED WITH
191
207
  2.2.17
data/README.md CHANGED
@@ -13,7 +13,8 @@ TODO LIST:
13
13
  - Catch SIGTERM and friends
14
14
  - Now supports it, partly.
15
15
  - Don't crash on errors (Partially done)
16
- - Make it possible to schedule jobs
16
+ - Save jobs enqueued at a future date too when quitting (now it only saves the jobs in the queue)
17
+ - Have multiple queues?
17
18
  - Maybe support ActiveJob?
18
19
  - Have a web UI
19
20
  - Do some performance testing
@@ -21,6 +22,7 @@ TODO LIST:
21
22
 
22
23
  DONE
23
24
 
25
+ - ~~Make it possible to schedule jobs~~
24
26
  - ~~Marshal the job queue into a file so you don't lose all progress~~
25
27
  (Ended up using YAML)
26
28
  - ~~Add a logger~~
@@ -98,6 +100,12 @@ client.perform_belated(job)
98
100
 
99
101
  If you want to pass a job to Belated.
100
102
 
103
+ If you don't want the job to run right away, you can also pass it a keyword param `at:` like so:
104
+
105
+ ```ruby
106
+ client.perform_belated(job, Time.now + 1.month)
107
+ ```
108
+
101
109
  # Settings
102
110
 
103
111
  Configuring Belated:
@@ -113,15 +121,15 @@ end
113
121
 
114
122
  From command line:
115
123
 
116
- $ bundle exec belated --rails=true
124
+ $ bundle exec belated --rails=true
117
125
 
118
126
  Use Rails or not.
119
127
 
120
- $ bundle exec belated --rails_path=/my_rails_project
128
+ $ bundle exec belated --rails_path=/my_rails_project
121
129
 
122
130
  Path to Rails project.
123
131
 
124
- $ bundle exec belated --workers=10
132
+ $ bundle exec belated --workers=10
125
133
 
126
134
  Number of workers.
127
135
 
data/lib/belated.rb CHANGED
@@ -9,6 +9,7 @@ require 'singleton'
9
9
  require 'dry-configurable'
10
10
  require 'belated/client'
11
11
  require 'logger'
12
+ require 'belated/queue'
12
13
 
13
14
  # Belated is a pure Ruby job backend.
14
15
  # It has limited functionality, as it only accepts
@@ -22,7 +23,7 @@ class Belated
22
23
  include Singleton unless $TESTING
23
24
  URI = 'druby://localhost:8788'
24
25
  FILE_NAME = 'belated_dump'
25
- @@queue = Queue.new
26
+ @@queue = Belated::Queue.new
26
27
 
27
28
  setting :rails, true
28
29
  setting :rails_path, '.'
@@ -38,15 +39,15 @@ class Belated
38
39
  def start
39
40
  boot_app && load_jobs
40
41
  @worker_list = []
41
- Belated.config.workers.times do |_i|
42
- @worker_list << Thread.new { Worker.new }
42
+ Belated.config.workers.times do |i|
43
+ @worker_list << Thread.new { Worker.new(number: i.next) }
43
44
  end
44
45
  return unless Belated.config.connect
45
46
 
46
47
  connect!
47
48
  banner_and_info
48
49
  trap_signals
49
- DRb.thread.join
50
+ enqueue_future_jobs
50
51
  end
51
52
  alias initialize start
52
53
 
@@ -55,7 +56,6 @@ class Belated
55
56
  DRb.start_service(URI, @@queue, verbose: true)
56
57
  rescue DRb::DRbConnError, Errno::EADDRINUSE
57
58
  Belated.logger.error 'Could not connect to DRb server.'
58
- retry
59
59
  end
60
60
 
61
61
  def trap_signals
@@ -65,7 +65,7 @@ class Belated
65
65
  @@queue.push(:shutdown)
66
66
  end
67
67
  Thread.new { stop_workers }
68
- sleep 0.1 until @@queue.empty?
68
+ sleep 0.1 until @@queue.empty? || $TESTING
69
69
  exit
70
70
  end
71
71
  end
@@ -84,6 +84,19 @@ class Belated
84
84
  Belated.config.rails
85
85
  end
86
86
 
87
+ def enqueue_future_jobs
88
+ log 'starting future jobs thread'
89
+ loop do
90
+ @@queue.future_jobs.each_with_index do |job, i|
91
+ if job[:at] <= Time.now.utc
92
+ log @@queue.future_jobs.delete_at(i)
93
+ @@queue.push(job[:klass])
94
+ end
95
+ end
96
+ sleep 0.01
97
+ end
98
+ end
99
+
87
100
  def load_jobs
88
101
  log "reloading... if file exists #{File.exist?(Belated::FILE_NAME)}"
89
102
  return unless File.exist?(Belated::FILE_NAME)
@@ -106,7 +119,7 @@ class Belated
106
119
  Thread.kill(worker)
107
120
  end
108
121
  class_array = []
109
- @@queue.size.times do |_i|
122
+ @@queue.length.times do |_i|
110
123
  unless (klass = @@queue.pop).instance_of?(Proc) || klass == :shutdown
111
124
  class_array << klass
112
125
  end
@@ -1,7 +1,16 @@
1
1
  class Belated
2
+ # The client class is responsible for managing the connection to the
3
+ # DRb server.
4
+ # You can enqueue jobs to be processed by the server.
5
+ # Example:
6
+ # client = Belated::Client.new
7
+ # client.enqueue(JubJub.new, at: Time.now + 5.seconds)
2
8
  class Client
3
9
  attr_accessor :queue
4
10
 
11
+ # Starts up the client.
12
+ # Connects to the queue through DRb.
13
+ # @return [void]
5
14
  def initialize
6
15
  server_uri = Belated::URI
7
16
  # @bank =
@@ -9,8 +18,12 @@ class Belated
9
18
  self.queue = DRbObject.new_with_uri(server_uri)
10
19
  end
11
20
 
12
- def perform(job)
13
- queue.push(job)
21
+ # The method that pushes the jobs to the queue.
22
+ # @param job [Object] - The the job to be pushed.
23
+ # @param at [Date] - The time at which the job should be executed.
24
+ # @return [Object] - The job that was pushed.
25
+ def perform(job, at: nil)
26
+ queue.push(job, at: at)
14
27
  end
15
28
  alias perform_belated perform
16
29
  alias perform_later perform
@@ -0,0 +1 @@
1
+ Job = Struct.new(:klass, :at)
@@ -0,0 +1,37 @@
1
+ require 'belated/job'
2
+
3
+ class Belated
4
+ class Queue
5
+ attr_accessor :future_jobs
6
+
7
+ def initialize(queue: Thread::Queue.new, future_jobs: [])
8
+ @queue = queue
9
+ self.future_jobs = future_jobs
10
+ end
11
+
12
+ def push(job, at: nil)
13
+ if at.nil?
14
+ @queue.push(job)
15
+ else
16
+ @future_jobs << Job.new(job, at)
17
+ end
18
+ end
19
+
20
+ def pop
21
+ @queue.pop
22
+ end
23
+
24
+ def clear
25
+ @queue.clear
26
+ self.future_jobs = []
27
+ end
28
+
29
+ def empty?
30
+ @queue.empty?
31
+ end
32
+
33
+ def length
34
+ @queue.length
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.3.3'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -1,22 +1,24 @@
1
1
  require_relative 'logging'
2
2
  class Belated
3
3
  # The worker class that actually gets the jobs from the queue
4
- # and calls them. Expects the jobs to be procs.
4
+ # and calls them. Expects the jobs to be procs or
5
+ # classes that have a perform method.
5
6
  class Worker
6
7
  include Logging
7
8
 
8
- def initialize
9
+ def initialize(number: 1)
10
+ @number = number
9
11
  start_working
10
12
  end
11
13
 
12
14
  def start_working
13
15
  loop do
16
+ log "Worker #{@number} fetching jobs!"
14
17
  next unless (job = Belated.fetch_job)
15
18
 
16
19
  break if job == :shutdown
17
20
 
18
21
  log call_job(job)
19
- log 'fetching jobs...'
20
22
  end
21
23
  end
22
24
 
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.3.3
4
+ version: 0.4.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-01 00:00:00.000000000 Z
11
+ date: 2021-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: drb
@@ -80,7 +80,9 @@ files:
80
80
  - bin/setup
81
81
  - lib/belated.rb
82
82
  - lib/belated/client.rb
83
+ - lib/belated/job.rb
83
84
  - lib/belated/logging.rb
85
+ - lib/belated/queue.rb
84
86
  - lib/belated/rails.rb
85
87
  - lib/belated/version.rb
86
88
  - lib/belated/worker.rb