Created
December 13, 2014 15:25
-
-
Save Jberczel/f588a68b3de25647e1e9 to your computer and use it in GitHub Desktop.
heroku scheduler workaround
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
class BiDailyScheduler < HerokuScheduler | |
def should_run? | |
(Time.now.hour % 12) == hour | |
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 'test_helper' | |
class BiDailySchedulerTest < ActiveSupport::TestCase | |
def setup | |
@hour = Time.now.hour | |
end | |
test 'should perform task if current hour' do | |
assert BiDailyScheduler.new(@hour).should_run? | |
end | |
test 'performs task at 12 hours from current hour' do | |
assert BiDailyScheduler.new(@hour + 12).should_run? | |
end | |
test 'should not perform task if different hour' do | |
refute BiDailyScheduler.new(@hour + 1).should_run? | |
end | |
test 'should not perform task 16 hours from current hour' do | |
refute BiDailyScheduler.new(@hour + 16).should_run? | |
end | |
test 'should perform scheduled task' do | |
block_code = BiDailyScheduler.new(@hour).schedule { 'performed a task' } | |
assert_equal 'performed a task', block_code | |
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
class HerokuScheduler | |
attr_reader :hour | |
def initialize(hour=2) | |
@hour = hour % 12 | |
end | |
def schedule | |
yield if should_run? | |
end | |
def should_run? | |
true | |
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
namespace :scrape do | |
desc "Scrape guitar forum" | |
task larrivee: :environment do | |
BiDailyScheduler.new(4).schedule do | |
scrape Larrivee | |
end | |
end | |
task martin: :environment do | |
BiDailyScheduler.new(5).schedule do | |
scrape Martin | |
end | |
end | |
end | |
def scrape(object) | |
puts "scraping #{object} pages..." | |
scraper = "#{object}Scraper".constantize.new | |
scraper.parse_pages.create_posts(object) | |
puts "scraping complete" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment