Sha256: 6cdac91f3225d8e465eaaf2e0646d87ff886d1fbbf91b74d4d53228282d87a9b
Contents?: true
Size: 800 Bytes
Versions: 31
Compression:
Stored size: 800 Bytes
Contents
# frozen_string_literal: true module ShopifyCLI class ThreadPool attr_reader :errors def initialize(pool_size: 10) @jobs = Queue.new @pool = Array.new(pool_size) { spawn_thread } end def schedule(job) @jobs << job end def shutdown @pool.size.times do schedule(-> { throw(:stop_thread) }) end @pool.map(&:join) ensure @jobs.close end private def spawn_thread Thread.new do catch(:stop_thread) do loop { perform(@jobs.pop) } end end end def perform(job) job.call reschedule(job) if job.recurring? end def reschedule(job) wait(job.interval) schedule(job) end def wait(seconds) sleep(seconds) end end end
Version data entries
31 entries across 31 versions & 1 rubygems