Class: DBcron::Clock

Inherits:
Object
  • Object
show all
Includes:
Actor
Defined in:
lib/dbcron.rb

Overview

nodoc

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Actor

included

Constructor Details

#initializeClock

Returns a new instance of Clock.



104
105
106
107
108
109
# File 'lib/dbcron.rb', line 104

def initialize
  @tasks = {}
  @last_refresh = nil
  @tz = UTC
  @uuid = SecureRandom.uuid
end

Instance Attribute Details

#stopObject

Returns the value of attribute stop.



99
100
101
# File 'lib/dbcron.rb', line 99

def stop
  @stop
end

#tzObject (readonly)

Returns the value of attribute tz.



100
101
102
# File 'lib/dbcron.rb', line 100

def tz
  @tz
end

Instance Method Details

#add_entry(name, cron, task) ⇒ Object



115
116
117
# File 'lib/dbcron.rb', line 115

def add_entry(name, cron, task)
  @tasks[name] = CrontabEntry.new(name, cron: cron, task: task)
end

#configure(opts = {}) ⇒ Object



111
112
113
# File 'lib/dbcron.rb', line 111

def configure(opts = {})
  @tz = opts[:tz] if opts[:tz]
end

#ready_tasks(time) ⇒ Object



119
120
121
# File 'lib/dbcron.rb', line 119

def ready_tasks(time)
  task_array.select { |e| e.ready?(time) }
end

#startObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/dbcron.rb', line 127

def start
  started_at = now

  host = Host.where(uuid: @uuid).first_or_create!(
    hostname: Socket.gethostname,
    pid: Process.pid,
    started: started_at,
    last_seen: started_at
  )

  create(task_array)
  refresh(task_array)

  info "dbcron starting with #{worker_pool_size} workers"

  loop do
    start = now

    if start >= host.last_seen + HOST_ALIVE_TIME
      host.update_column(:last_seen, start)
    end

    tick(start)

    finish = now

    sleep_until_next_tick(finish)

    break if @stop
  end
end

#task_arrayObject



123
124
125
# File 'lib/dbcron.rb', line 123

def task_array
  @task_array ||= @tasks.values
end