Class: Arask::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/arask/setup.rb

Class Method Summary collapse

Class Method Details

.create(script: nil, task: nil, job: nil, interval: nil, cron: nil, run_first_time: false) ⇒ Object



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
# File 'lib/arask/setup.rb', line 12

def self.create(script: nil, task: nil, job: nil, interval: nil, cron: nil, run_first_time: false)
  interval = parse_interval_or_cron(interval, cron)
  if interval.nil?
    puts 'Arask: You did not specify neither cron: nor interval:! When should the task run?'
    return
  end
  unless task.nil?
    script = "Rake::Task['#{task}'].invoke"
  end
  unless job.nil?
    script = "#{job}.perform_now"
  end
  if script.nil?
    puts 'Arask: You did not specify a script or task to run!'
    return
  end
  begin
    job = AraskJob.where(job: script, interval: interval).first
    if job
      Arask.jobs_touched << job.id
    else
      job = AraskJob.create(job: script, interval: interval)
      Arask.jobs_touched << job.id
      if run_first_time===true
        job.update_attribute(:execute_at, Time.now)
      end
    end
  rescue ActiveRecord::StatementInvalid => e
    puts 'Could not create arask job! Looks like the database is not migrated? Remember to run `rails generate arask:install` and `rails db:migrate`'
  end
end

.on_exception(email: nil, from: 'robot@server', &block) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/arask/setup.rb', line 3

def self.on_exception(email: nil, from: 'robot@server', &block)
  Arask.exception_email = email unless email.nil?
  Arask.exception_email_from = from unless from.nil?
  puts "Arask could not parse parameter for on_exception!" unless email.nil? or email.class == String
  if block_given?
    Arask.exception_block = block
  end
end

.parse_interval_or_cron(interval, cron) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/arask/setup.rb', line 45

def self.parse_interval_or_cron(interval, cron)
  unless interval.nil?
    case interval
    when :hourly
      interval = 1.hour
    when :daily
      interval = 1.day
    when :monthly
      interval = 1.month
    end
    interval = interval.to_s.to_i
  end
  unless cron.nil?
    interval = 'cron: ' + cron
  end
  return interval
end