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}']"
  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