Module: Croner

Defined in:
lib/croner.rb,
lib/croner/config.rb,
lib/croner/engine.rb,
lib/croner/railtie.rb,
lib/croner/version.rb,
lib/generators/croner/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: Config, Railtie

Constant Summary collapse

VERSION =
'0.1.5'

Class Method Summary collapse

Class Method Details

.configObject



4
5
6
# File 'lib/croner/engine.rb', line 4

def self.config
  @config ||= Croner::Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



8
9
10
# File 'lib/croner/engine.rb', line 8

def self.configure(&block)
  yield(config) if block_given?
end

.runObject



7
8
9
10
11
12
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/croner.rb', line 7

def self.run
  return {status: false, message: "couldn't found cron setting file"} unless File.exist?(Rails.root.join('config', 'croner', 'hosts', `hostname`.delete("\n")))

  # get cron contents
  insert_rows = File.read(Rails.root.join('config', 'croner', 'hosts', `hostname`.delete("\n"))).split("\n")

  # backup current cron contents
  if Croner.config.enable_backup
    if Croner.config.backup_path.blank?
      backup_path = '.'
    else
      backup_path = "./#{Croner.config.backup_path}"
    end
    `crontab -l > #{backup_path}/cron_#{Time.current.strftime('%Y%m%d%H%M%S')}.bak`

    # delete over backup files
    backup_files = Dir.glob("#{backup_path}/cron_*.bak")
    if backup_files.count > Croner.config.keep_backups
      backup_dates = backup_files.map do |backup_file|
        backup_file.match(/cron_([0-9]{14}).bak$/)[1] rescue nil
      end
      delete_backup_files = backup_dates.compact.sort.first(backup_files.count - Croner.config.keep_backups).map{|backup_date| "#{backup_path}/cron_#{backup_date}.bak"}
      delete_backup_files.each do |delete_backup_file|
        File.delete(delete_backup_file)
      end
    end
  end

  # get current cron contents
  cron_rows = `crontab -l`.split("\n")

  application_name = Rails.application.class.parent_name
  cron_start_row   = "# ==================== START #{application_name} CronJobs by Croner ===================="
  cron_end_row     = "# ====================  END  #{application_name} CronJobs by Croner ===================="

  start_index = cron_rows.index(cron_start_row)
  end_index   = cron_rows.index(cron_end_row)

  return ArgumentError.new('current cron contents has unvalid settings by Croner!') unless ((start_index.present? && end_index.present?) || (start_index.blank? && end_index.blank?))

  if start_index.present? && end_index.present?
    cron_rows.slice!((start_index + 1)..(end_index - 1))
  else
    start_index = cron_rows.count
    cron_rows << cron_start_row
    cron_rows << cron_end_row
  end

  cron_rows.insert(start_index + 1, *insert_rows)

  File.open("cron_tmp", "w") do |f| 
    cron_rows.each{|row| f.puts(row)}
  end

  stdout, stderror, status = Open3.capture3("crontab cron_tmp")

  File.delete("cron_tmp")

  if status.success?
    return {status: true}
  else
    return {status: false, message: stderror}
  end
end