Last active
April 12, 2016 20:31
-
-
Save timothypage/7ce3d8317cc9ce018caa1fb84cb4c23c to your computer and use it in GitHub Desktop.
scheduler.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sequel.migration do | |
change do | |
create_table(:tasks) do | |
primary_key :id | |
String :task_name | |
String :class_name | |
DateTime :last_ran | |
String :frequency # ie 4.hours | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'logger' | |
require './config/initializers/sidekiq.rb' | |
require './db/connect' | |
require 'active_support' | |
require 'active_support/core_ext' | |
@log = Logger.new(STDOUT) | |
def task_should_be_run?( task ) | |
begin | |
(Time.now - task.last_ran) > eval(task.frequency) | |
rescue => e | |
@log.error e.message | |
end | |
end | |
def run_task( task ) | |
@log.info "queueing: #{task.task_name}" | |
task.last_ran = Time.now | |
task.save | |
begin | |
task.class_name.constantize.perform_async | |
rescue NameError => e | |
@log.error "Couldn't find worker named: #{task.class_name}" | |
rescue NoMethodError => e | |
@log.error "Couldn't find valid worker named: #{task.class_name}. NoMethodError" | |
rescue => e | |
@log.error e.message | |
end | |
end | |
while true do | |
@log.info "heartbeat" | |
Task.all.each do |task| | |
if task.last_ran.nil? | |
run_task(task) | |
elsif task_should_be_run?( task ) | |
run_task(task) | |
end | |
end | |
sleep(60) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment