Skip to content

Commit

Permalink
Be able to parse cron:
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebbe committed Jun 14, 2018
1 parent a694d68 commit 66d2b60
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PATH
remote: .
specs:
arask (0.1.2)
fugit (~> 1.1)
rails (~> 5.1)

GEM
Expand Down Expand Up @@ -54,6 +55,11 @@ GEM
crass (1.0.4)
diff-lcs (1.3)
erubi (1.7.1)
et-orbi (1.1.2)
tzinfo
fugit (1.1.1)
et-orbi (~> 1.1, >= 1.1.1)
raabro (~> 1.1)
globalid (0.4.1)
activesupport (>= 4.2.0)
i18n (1.0.1)
Expand All @@ -73,6 +79,7 @@ GEM
nio4r (2.3.1)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
raabro (1.1.5)
rack (2.0.5)
rack-test (1.0.0)
rack (>= 1.0, < 3)
Expand Down
1 change: 1 addition & 0 deletions arask.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Gem::Specification.new do |s|
s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]

s.add_dependency "rails", "~> 5.1"
s.add_dependency "fugit", "~> 1.1"

s.add_development_dependency "sqlite3"
s.add_development_dependency "rspec"
Expand Down
1 change: 1 addition & 0 deletions lib/arask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "arask/arask_job"
require "arask/run_jobs"
require "arask/setup"
require 'fugit' # To parse cron

module Arask
class << self; attr_accessor :jobs_touched, :exception_email, :exception_email_from; end
Expand Down
14 changes: 13 additions & 1 deletion lib/arask/arask_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ module Arask
require 'rake'

class AraskJob < ActiveRecord::Base
after_create :calculate_new_execute_at

def run
self.update_attribute(:execute_at, self.interval.seconds.from_now)
calculate_new_execute_at
begin
if self.job.start_with?('Rake::Task')
Rake.load_rakefile Rails.root.join( 'Rakefile' )
Expand All @@ -24,5 +26,15 @@ def run
end
end
end

private
def calculate_new_execute_at
if self.interval.start_with?('cron: ')
cron = Fugit::Cron.parse(self.interval.split(' ', 2)[1])
self.update_attribute(:execute_at, cron.next_time.to_t)
else
self.update_attribute(:execute_at, self.interval.to_i.seconds.from_now)
end
end
end
end
29 changes: 19 additions & 10 deletions lib/arask/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@ def self.on_exception(email: nil, from: 'robot@server')
puts "Arask could not parse parameter for on_exception!" unless email.class == String
end

def self.create(script: nil, task: nil, interval:, run_first_time: false)
case interval
when :hourly
interval = 1.hour
when :daily
interval = 1.day
when :monthly
interval = 1.month
def self.create(script: nil, task: nil, interval: nil, cron: nil, run_first_time: false)
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
if interval.nil?
puts 'Arask: You did not specify either cron: or interval:! When should the task run?'
return
end
interval = interval.to_s.to_i
unless task.nil?
script = "Rake::Task['#{task}'].invoke"
end
Expand All @@ -28,7 +37,7 @@ def self.create(script: nil, task: nil, interval:, run_first_time: false)
if job
Arask.jobs_touched << job.id
else
job = AraskJob.create(job: script, interval: interval, execute_at: interval.seconds.from_now)
job = AraskJob.create(job: script, interval: interval)
Arask.jobs_touched << job.id
if run_first_time===true
job.run
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/arask/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def initialize(*args, &block)
Arask::InstallGenerator.source_root File.expand_path('../../arask', __FILE__)
copy_file '../../arask/initialize.rb', 'config/initializers/arask.rb'

generate 'migration', 'create_arask_jobs job:string execute_at:datetime:index interval:integer'
generate 'migration', 'create_arask_jobs job:string execute_at:datetime:index interval:string'
end
end

0 comments on commit 66d2b60

Please sign in to comment.