Skip to content

Instantly share code, notes, and snippets.

@timothypage
Last active April 12, 2016 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timothypage/7ce3d8317cc9ce018caa1fb84cb4c23c to your computer and use it in GitHub Desktop.
Save timothypage/7ce3d8317cc9ce018caa1fb84cb4c23c to your computer and use it in GitHub Desktop.
scheduler.rb
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
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