Class: KamalBackup::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/kamal_backup/scheduler.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, &backup_block) ⇒ Scheduler

Returns a new instance of Scheduler.



7
8
9
10
11
# File 'lib/kamal_backup/scheduler.rb', line 7

def initialize(config, &backup_block)
  @config = config
  @backup_block = backup_block
  @stop = false
end

Instance Method Details

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kamal_backup/scheduler.rb', line 13

def run
  $stdout.sync = true
  $stderr.sync = true

  install_signal_handlers

  unless @config.backup_enabled?
    # Stay up rather than exit: the accessory is still how restore, list, check and
    # evidence reach the repository, and exiting would restart-loop under Kamal's
    # `--restart unless-stopped`. Set backup.enabled to true and redeploy to resume.
    log('scheduled backups are disabled (backup.enabled is false); no backups will be taken')
    idle_until_stopped
    log("scheduler stopped at #{Time.now.utc.iso8601}")
    return
  end

  sleep_interruptibly(@config.backup_start_delay_seconds)

  until @stop
    started_at = Time.now.utc
    log("backup started at #{started_at.iso8601}")
    begin
      @backup_block.call
      log("backup completed at #{Time.now.utc.iso8601}")
    rescue StandardError => e
      warn("ERROR [kamal-backup] backup failed at #{Time.now.utc.iso8601}: #{e.class}: #{e.message}")
    end

    sleep_interruptibly(@config.backup_schedule_seconds)
  end

  log("scheduler stopped at #{Time.now.utc.iso8601}")
end