Sha256: d7493a617f7139b653cddcf34fa8423bd80e21b732499826e6bf75b1b584a31d

Contents?: true

Size: 1.89 KB

Versions: 19

Compression:

Stored size: 1.89 KB

Contents

module Rearview
  class MonitorService
    class MonitorServiceError < StandardError; end;
    include Celluloid
    include Rearview::Logger
    attr_accessor :jobs
    attr_reader :supervisor
    def initialize(jobs=[])
      self.jobs = jobs
      @supervisor = nil
      @started = false
    end
    def jobs=(jobs)
      @jobs = jobs.inject({}) { |c,v| c[v.id] = v ; c }
    end
    def started?
      @started
    end
    def startup
      raise MonitorServiceError.new("service already started") if started?
      # TODO actor could die, need to reference by name in registry and/or create link
      @supervisor = Rearview::MonitorSupervisor.run!
      @supervisor.add_tasks(@jobs.values)
      @started = true
    end
    def shutdown
      raise MonitorServiceError.new("service not started") unless started?
      @supervisor.remove_all_tasks
      @supervisor.terminate
      @started = false
    end
    def schedule(job)
      logger.debug "#{self} schedule job: #{job.id}"
      raise MonitorServiceError.new("service not started") unless started?
      if @jobs[job.id]
        remove(job)
      end
      add(job)
    end
    def unschedule(job)
      logger.debug "#{self} unschedule job: #{job.id}"
      raise MonitorServiceError.new("service not started") unless started?
      if @jobs[job.id]
        remove(job)
      end
    end
    def add(job)
      raise MonitorServiceError.new("service not started") unless started?
      if @jobs[job.id]
        logger.warn "#{self}#add job:#{job.id} already added"
      else
        @supervisor.add_tasks([job])
        @jobs[job.id] = job
      end
    end
    def remove(job)
      raise MonitorServiceError.new("service not started") unless started?
      if !@jobs[job.id]
        logger.warn "#{self}#remove job:#{job.id} has already been removed"
      else
        @supervisor.remove_tasks([job])
        @jobs.delete(job.id)
      end
    end

  end

end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
rearview-1.1.2-jruby lib/rearview/monitor_service.rb
rearview-1.1.1-jruby lib/rearview/monitor_service.rb
rearview-1.1.0-jruby lib/rearview/monitor_service.rb
rearview-1.0.3.rc.4-jruby lib/rearview/monitor_service.rb
rearview-1.0.3.rc.3-jruby lib/rearview/monitor_service.rb
rearview-1.0.3.rc.2-jruby lib/rearview/monitor_service.rb
rearview-1.0.3.rc.1-jruby lib/rearview/monitor_service.rb
rearview-1.0.2-jruby lib/rearview/monitor_service.rb
rearview-1.0.2.rc.4-jruby lib/rearview/monitor_service.rb
rearview-1.0.2.rc.3-jruby lib/rearview/monitor_service.rb
rearview-1.0.2.rc.2-jruby lib/rearview/monitor_service.rb
rearview-1.0.2.rc.1-jruby lib/rearview/monitor_service.rb
rearview-1.0.1-jruby lib/rearview/monitor_service.rb
rearview-1.0.0-jruby lib/rearview/monitor_service.rb
rearview-1.0.0.rc5-jruby lib/rearview/monitor_service.rb
rearview-1.0.0.rc4-jruby lib/rearview/monitor_service.rb
rearview-1.0.0.rc3-jruby lib/rearview/monitor_service.rb
rearview-1.0.0.rc2-jruby lib/rearview/monitor_service.rb
rearview-1.0.0.rc1-jruby lib/rearview/monitor_service.rb