Class: Atig::Scheduler

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/atig/scheduler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExceptionUtil

daemon, safe

Constructor Details

#initialize(context, api, search, stream) ⇒ Scheduler

Returns a new instance of Scheduler.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/atig/scheduler.rb', line 11

def initialize(context, api, search, stream)
  @log    = context.log
  @api    = api
  @search = search
  @stream = stream
  @agents = []

  @queue = SizedQueue.new 10
  daemon do
    f = @queue.pop
    f.call
  end
end

Instance Attribute Details

#searchObject (readonly)

Returns the value of attribute search.



9
10
11
# File 'lib/atig/scheduler.rb', line 9

def search
  @search
end

Instance Method Details

#delay(interval, opts = {}, &f) ⇒ Object



38
39
40
41
# File 'lib/atig/scheduler.rb', line 38

def delay(interval, opts={}, &f)
  sleep interval
  @queue.push(lambda{ safe { f.call @api } })
end

#limitObject



67
68
69
# File 'lib/atig/scheduler.rb', line 67

def limit
  @api.limit
end

#re_try(count, &f) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/atig/scheduler.rb', line 51

def re_try(count, &f)
  begin
    f.call
  rescue => e
    log :error, [count, e.inspect].inspect
    if count > 0
      count -= 1
      sleep 1
      log :debug, "retry"
      retry
    end
    log :error, "Some Error Happened: #{e}"
    raise e
  end
end

#remainObject



71
72
73
# File 'lib/atig/scheduler.rb', line 71

def remain
  @api.remain
end

#repeat(interval, opts = {}, &f) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/atig/scheduler.rb', line 25

def repeat(interval,opts={}, &f)
  @queue.push(lambda{ safe { f.call @api } })
  t = daemon do
    sleep interval
    log :debug, "agent #{t.inspect} is invoked"
    @queue.push(lambda{ safe { f.call @api } })
  end

  log :info, "repeat agent #{t.inspect} is registered"
  @agents << t
  t
end

#resetObject



75
76
77
# File 'lib/atig/scheduler.rb', line 75

def reset
  @api.reset
end

#stream(&f) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/atig/scheduler.rb', line 43

def stream(&f)
  return nil unless @stream
  @stream_thread.kill if @stream_thread
  @stream_thread = daemon {
    f.call @stream
  }
end