Class: CronTable::Server

Inherits:
Object
  • Object
show all
Defined in:
app/services/cron_table/server.rb

Constant Summary collapse

SLEEP =
1.second..1.day
SLEEP_IDLE =
1.hour

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



6
7
8
# File 'app/services/cron_table/server.rb', line 6

def initialize
  @middlewares = Middlewares.new
end

Instance Method Details

#exit!Object



40
41
42
43
# File 'app/services/cron_table/server.rb', line 40

def exit!
  @exit = true
  interrupt!
end

#exit?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/services/cron_table/server.rb', line 45

def exit?
  @exit.present?
end

#run!Object



30
31
32
33
34
35
36
37
38
# File 'app/services/cron_table/server.rb', line 30

def run!
  Item.connection_pool.with_connection do
    sync!
  end

  @exit = false

  run_once! until exit?
end

#sync!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/cron_table/server.rb', line 10

def sync!
  crons = CronTable.all.clone
  deleted = []
  Item.transaction do
    Item.lock.all.each do |cron|
      if definition = crons.delete(cron.key)
        context = Context.new(last_run_at: nil)
        cron.update(next_run_at: definition.next_run_at(context)) if cron.next_run_at.nil?
      elsif cron.next_run_at.present?
        deleted << cron.key
      end
    end
    Item.where(key: deleted).update(next_run_at: nil)
    crons.each do |key, definition|
      context = Context.new(last_run_at: nil)
      Item.create(key: key, next_run_at: definition.next_run_at(context))
    end
  end
end