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
|