Class: HardWorker
- Inherits:
-
Object
show all
- Extended by:
- Dry::Configurable
- Includes:
- Singleton
- Defined in:
- lib/hard_worker.rb,
lib/hard_worker/rails.rb,
lib/hard_worker/client.rb,
lib/hard_worker/worker.rb,
lib/hard_worker/version.rb
Overview
HardWorker is a pure Ruby job backend. It has limited functionality, as it only accepts jobs as procs, but that might make it useful if you don’t need anything as big as Redis. Saves jobs into a file as YAML as long as they’re not procs and reloads them when started again.
Defined Under Namespace
Classes: Client, Error, Rails, Worker
Constant Summary
collapse
- URI =
"druby://localhost:#{$TESTING ? Array.new(4) { rand(10) }.join : "8788"}"
- FILE_NAME =
'hard_worker_dump'
- VERSION =
'0.0.4'
- @@queue =
Queue.new
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.clear_queue! ⇒ Object
131
132
133
|
# File 'lib/hard_worker.rb', line 131
def self.clear_queue!
@@queue.clear
end
|
.fetch_job ⇒ Object
139
140
141
|
# File 'lib/hard_worker.rb', line 139
def self.fetch_job
@@queue.pop
end
|
.stop_workers ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/hard_worker.rb', line 86
def self.stop_workers
@worker_list&.each do |worker|
Thread.kill(worker)
end
class_array = []
@@queue.size.times do |_i|
next if (klass_or_proc = @@queue.pop).instance_of?(Proc)
class_array << klass_or_proc
end
File.open(FILE_NAME, 'wb') { |f| f.write(YAML.dump(class_array)) }
end
|
Instance Method Details
#banner ⇒ Object
rubocop:disable Layout/LineLength rubocop:disable Metrics/MethodLength
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/hard_worker.rb', line 101
def banner
<<-'BANNER'
.----------------. .----------------. .----------------. .----------------. .----------------. .----------------. .----------------.
| .--------------. | .--------------. | .--------------. | .--------------. | .--------------. | .--------------. | .--------------. |
| | ______ | | | _________ | | | _____ | | | __ | | | _________ | | | _________ | | | ________ | |
| | |_ _ \ | | | |_ ___ | | | | |_ _| | | | / \ | | | | _ _ | | | | |_ ___ | | | | |_ ___ `. | |
| | | |_) | | | | | |_ \_| | | | | | | | | / /\ \ | | | |_/ | | \_| | | | | |_ \_| | | | | | `. \ | |
| | | __'. | | | | _| _ | | | | | _ | | | / ____ \ | | | | | | | | | _| _ | | | | | | | | |
| | _| |__) | | | | _| |___/ | | | | _| |__/ | | | | _/ / \ \_ | | | _| |_ | | | _| |___/ | | | | _| |___.' / | |
| | |_______/ | | | |_________| | | | |________| | | ||____| |____|| | | |_____| | | | |_________| | | | |________.' | |
| | | | | | | | | | | | | | | | | | | | | |
| '--------------' | '--------------' | '--------------' | '--------------' | '--------------' | '--------------' | '--------------' |
'----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
BANNER
end
|
#banner_and_info ⇒ Object
117
118
119
120
121
122
|
# File 'lib/hard_worker.rb', line 117
def banner_and_info
puts banner
puts 'HardWorker is going to change to Belated!'
puts "Currently running HardWorker version #{HardWorker::VERSION}"
puts %(HardWorker running #{@worker_list&.length.to_i} workers on #{URI}...)
end
|
#boot_app ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/hard_worker.rb', line 46
def boot_app
return unless rails?
ENV['RAILS_ENV'] ||= HardWorker.config.environment
require File.expand_path("#{HardWorker.config.rails_path}/config/environment.rb")
require 'rails/all'
require 'hard_worker/rails'
end
|
#job_list ⇒ Object
135
136
137
|
# File 'lib/hard_worker.rb', line 135
def job_list
@@queue
end
|
#load_jobs ⇒ Object
59
60
61
62
63
64
65
66
67
|
# File 'lib/hard_worker.rb', line 59
def load_jobs
jobs = YAML.load(File.binread(FILE_NAME))
jobs.each do |job|
@@queue.push(job)
end
File.delete(HardWorker::FILE_NAME) if File.exist?(HardWorker::FILE_NAME)
rescue StandardError
end
|
#rails? ⇒ Boolean
55
56
57
|
# File 'lib/hard_worker.rb', line 55
def rails?
HardWorker.config.rails
end
|
#reload ⇒ Object
69
70
71
|
# File 'lib/hard_worker.rb', line 69
def reload
load_jobs
end
|
#start ⇒ Object
Also known as:
initialize
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/hard_worker.rb', line 31
def start
boot_app
load_jobs
@worker_list = []
HardWorker.config.workers.times do |_i|
@worker_list << Thread.new { Worker.new }
end
return unless HardWorker.config.connect
DRb.start_service(URI, @@queue, verbose: true)
puts banner_and_info
DRb.thread.join
end
|
#stats ⇒ Object
124
125
126
127
128
129
|
# File 'lib/hard_worker.rb', line 124
def stats
{
jobs: @@queue.size,
workers: @worker_list&.length
}
end
|
#stop_workers ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/hard_worker.rb', line 73
def stop_workers
@worker_list.each do |worker|
Thread.kill(worker)
end
class_array = []
@@queue.size.times do |_i|
next if (klass_or_proc = @@queue.pop).instance_of?(Proc)
class_array << klass_or_proc
end
File.open(FILE_NAME, 'wb') { |f| f.write(YAML.dump(class_array)) }
end
|