Sha256: 6c399f6c934298d140012126548d587bcea62790ecb76beef8425d7218232913

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

require 'concurrent'

module Yoda
  class Server
    class Scheduler
      # @return [Concurrent::ThreadPoolExecutor]
      attr_reader :thread_pool

      # @return [Concurrent::Map{String => Concurrent::Future}]
      attr_reader :future_map

      # @return [Concurrent::ThreadPoolExecutor]
      def self.default_thread_pool
        Concurrent.global_fast_executor
      end

      # @param thread_pool [Concurrent::ThreadPoolExecutor]
      def initialize(thread_pool: nil)
        @thread_pool = thread_pool || self.class.default_thread_pool
        @future_map = Concurrent::Map.new
      end

      # @param id [String]
      # @return [Concurrent::Future]
      def async(id:, &block)
        future = Concurrent::Future.new(executor: thread_pool) { block.call }
        future.add_observer { |_time, value, reason| future_map.delete(id) }
        future_map.put_if_absent(id, future)
        future.execute
        future
      end

      # @param id [String]
      def cancel(id)
        future_map[id]&.cancel
      end

      # @param timeout [Integer] the maximum number of seconds to wait for shutdown to complete.
      def wait_for_termination(timeout:)
        thread_pool.shutdown
        thread_pool.wait_for_termination(timeout)
      end

      def cancel_all
        future_map.each_value { |future| future&.cancel }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yoda-language-server-0.10.1 lib/yoda/server/scheduler.rb
yoda-language-server-0.10.0 lib/yoda/server/scheduler.rb
yoda-language-server-0.9.0 lib/yoda/server/scheduler.rb
yoda-language-server-0.8.0 lib/yoda/server/scheduler.rb