Class: INatGet::App::Worker
- Inherits:
-
Object
- Object
- INatGet::App::Worker
- Defined in:
- lib/inat-get/app/core/worker.rb
Class Method Summary collapse
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(task, **params) ⇒ Worker
constructor
A new instance of Worker.
Constructor Details
#initialize(task, **params) ⇒ Worker
Returns a new instance of Worker.
8 9 10 11 12 13 |
# File 'lib/inat-get/app/core/worker.rb', line 8 def initialize task, **params @task = task @params = params @console = params.delete :console @logger = INatGet::App::ConsoleLogger::new @console, progname: task.name end |
Class Method Details
.count ⇒ Object
42 43 44 45 46 |
# File 'lib/inat-get/app/core/worker.rb', line 42 def count @detachers ||= [] @detachers.reject { |dt| !dt.alive? } @detachers.size end |
.create(task, **params) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/inat-get/app/core/worker.rb', line 29 def create task, **params @detachers ||= [] pid = fork do worker = new(task, **params) worker.execute end result = Process::detach pid @detachers << result result end |
.enqueue(config, *tasks, **params) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/inat-get/app/core/worker.rb', line 48 def enqueue config, *tasks, **params @shutdown = false Signal::trap(:TERM) { @shutdown = true } Signal::trap(:INT) { @shutdown = true } queue = tasks.dup while queue.size > 0 && !@shutdown if self.count >= config.dig(:workers, :limit) sleep 0.1 next end task = queue.shift self.create task, **params sleep 0.1 end if @shutdown @detachers.each do |dt| if dt.alive? begin Process::kill :TERM, dt.pid rescue Errno::ESRCH end end end @detachers.each do |dt| dt.join 0.5 if dt.alive? begin Process::kill :KILL, dt.pid rescue Errno::ESRCH end end end else @detachers.map(&:join) end end |
Instance Method Details
#execute ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/inat-get/app/core/worker.rb', line 15 def execute require_relative '../../sys/context' Signal::trap(:TERM) { INatGet::System::Context::shutdown = true } Signal::trap(:INT) { INatGet::System::Context::shutdown = true } @console.register status: 'started...', name: @task.name @task.prepare @task.execute @console.update _active: false, status: 'done' rescue => e @logger.error e. end |