Sha256: d60e2ae94ac1d45cd631a1a2cef31928dcbb85014938b382072aa4be1bf13c77

Contents?: true

Size: 1.52 KB

Versions: 7

Compression:

Stored size: 1.52 KB

Contents

require 'json'

module Rearview
  class MonitorTask
    class MonitorTaskError < StandardError; end;
    include Celluloid
    include Celluloid::Logger
    attr_reader :job,:timer,:initial_delay
    def initialize(job,initial_delay=0,start=true)
      debug "#{self} intialize initial_delay:#{initial_delay} start:#{start}"
      @job = job
      @initial_delay = initial_delay
      schedule if start
    end
    def schedule
      debug "#{self} schedule"
      if ActiveRecord::Base.connection_pool.active_connection?
        ActiveRecord::Base.connection_pool.release_connection
      end
      # TODO is this really necessary?
      if(@timer)
        @timer.cancel
      end
      delay = @job.delay + @initial_delay
      debug "#{self} next run in #{delay}s"
      @timer = after(delay) { self.run }
    end
    def run
      debug "#{self} run"
      ActiveRecord::Base.connection_pool.with_connection do
        @initial_delay = 0
        result = Rearview::MonitorRunner.run(@job.metrics, @job.monitor_expr, @job.minutes)
        @job.last_run = Time.now.utc
        Rearview::ResultsHandler.new(@job,result).run
      end
    rescue
      error "#{self} run failed: #{$!}\n#{$@.join("\n")}"
      ActiveRecord::Base.connection_pool.with_connection do
        @job.last_run = Time.now.utc
        @job.error!
      end
    ensure
      schedule
    end
    def to_s
      "#{super.to_s} [jobId:#{self.job.try(:id)||'nil'} threadId:#{java.lang.Thread.currentThread.getId} threadName:#{java.lang.Thread.currentThread.getName}]"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rearview-1.0.1-jruby lib/rearview/monitor_task.rb
rearview-1.0.0-jruby lib/rearview/monitor_task.rb
rearview-1.0.0.rc5-jruby lib/rearview/monitor_task.rb
rearview-1.0.0.rc4-jruby lib/rearview/monitor_task.rb
rearview-1.0.0.rc3-jruby lib/rearview/monitor_task.rb
rearview-1.0.0.rc2-jruby lib/rearview/monitor_task.rb
rearview-1.0.0.rc1-jruby lib/rearview/monitor_task.rb