Sha256: 92d2b69ed53c33a00491eab3a7b72fe9f257872599a180b8ee15f798d4c6450f

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

module Berkshelf::API
  class CacheBuilder
    require_relative 'cache_builder/worker'

    class WorkerSupervisor < Celluloid::SupervisionGroup; end

    BUILD_INTERVAL = 5.0

    include Berkshelf::API::GenericServer
    include Berkshelf::API::Logging
    include Berkshelf::API::Mixin::Services

    server_name :cache_builder
    finalizer :finalize_callback

    def initialize
      log.info "Cache Builder starting..."
      @worker_registry   = Celluloid::Registry.new
      @worker_supervisor = WorkerSupervisor.new(@worker_registry)
      @building          = false

      Application.config.endpoints.each_with_index do |endpoint, index|
        endpoint_options = (endpoint.options || {}).to_hash.deep_symbolize_keys
        @worker_supervisor.supervise(CacheBuilder::Worker[endpoint.type], endpoint_options.merge(priority: index))
      end
    end

    def build
      cache_manager.process_workers(workers)
    end

    # Issue a build command to all workers at the scheduled interval
    #
    # @param [Fixnum, Float] interval
    def build_loop(interval = BUILD_INTERVAL)
      return if @building

      loop do
        @building = true
        build
        sleep interval
      end
    end

    # Return the list of running workers
    #
    # @return [Array<CacheBuilder::Worker::Base>]
    def workers
      @worker_supervisor.actors
    end

    private

      def finalize_callback
        log.info "Cache Builder shutting down..."
        @worker_supervisor.terminate if @worker_supervisor && @worker_supervisor.alive?
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
berkshelf-api-2.0.0 lib/berkshelf/api/cache_builder.rb