如何让rufus-scheduler使用部署到Heroku的Rails应用程序?

时间:2013-12-10 09:20:08

标签: ruby-on-rails ruby heroku passenger rufus-scheduler

./config/initializers我创建了一个名为task_scheduler.rb的文件,它包含以下代码:

require 'rufus-scheduler'
require 'mechanize'

scheduler = Rufus::Scheduler.new

scheduler.every("1h") do
    puts "Starting Rufus Scheduler - Task 1 - Checking exampleShop for new orders"

    a = Mechanize.new

    a.get('http://exampleshop.nl/admin/') do |page|

        # Select the login form
        login_form = page.forms.first

        # Insert the username and password
        login_form.username = 'username'
        login_form.password = 'password'

        # Submit the login information
        dashboard_page = a.submit(login_form, login_form.buttons.first)

        # Check if the login was successfull
        puts check_1 = dashboard_page.title == 'Dashboard' ?  "CHECK 1 DASHBOARD SUCCESS" : "CHECK 1 DASHBOARD FAIL"

        # Visit the orders index page to scrape some standard information
        orders_page = a.click(dashboard_page.link_with(:text => /Bestellingen/))

        # pp orders_page # => http://pastebin.com/L3zASer6

        # Check if the visit is successful
        puts check_2 = orders_page.title == 'Bestellingen' ?  "CHECK 2 ORDERS SUCCESS" : "CHECK 2 ORDERS FAIL"

        # Search for all #singleOrder table row's and put them in variable all_single_orders
        all_single_orders = orders_page.search("#singleOrder") 

        # Scrape the needed information (the actual save to database is omitted)
        all_single_orders.each do |order|
            # Set links for each order
            order_link = order.at_css("a")['href']  #Assuming first link in row

            @order_id = order.search("#orderId").text                   
            @order_status = order.search("#orderStatus").text       
            @order_revenue = order.search("#orderAmount").text      

            # Visit a single order page to fetch more detailed information
            single_order_page = orders_page.link_with(:href => order_link).click

            @first_name = single_order_page.search(".firstName").text
            @last_name = single_order_page.search(".lastName").text
            @city = single_order_page.search(".city").text
            @postal_code = single_order_page.search(".postalCode").text
            @address = single_order_page.search(".address").text
            @email = single_order_page.search(".email").text
            @order_quantity = single_order_page.search(".orderQuantity").text

            order = Order.create(   order_id: @order_id, first_name: @first_name, last_name: @last_name, city: @city,
                                                        email: @email, postal_code: @postal_code, address: @address, order_quantity: @order_quantity,
                                                        order_revenue: @order_revenue, order_status: @order_status)
        end
    end

    puts "Ending Rufus Scheduler - Task 1 - Checking exampleShop for new orders"
end

rufus-scheduler在开发环境中进行测试时有效。但是当我将应用程序部署到Heroku(免费)时它会停止工作。

我正在使用Phusion Passenger 4.0.27作为应用程序服务器。

My Gemfile如下所示:

source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.0.1'
gem 'rufus-scheduler', '3.0.2'
gem 'pg'
gem 'mechanize'
gem 'bcrypt-ruby', '3.1.2'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
gem 'newrelic_rpm'

group :doc do
  gem 'sdoc', require: false
end

group :development do
    gem 'sqlite3'
end

group :production do
    gem 'rails_12factor'
    gem 'passenger'
    gem 'pg'
end

Phusion Passenger所需的Procfile包含以下内容:

web: bundle exec passenger start -p $PORT --max-pool-size 3

我没有工人跑。我正在使用一个免费的标准web Dyno。

知道为什么rufus-scheduler在部署到Heroku时无效?

更新

我知道我可以创建一个客户.rake文件,并使用免费的Heroku Scheduler add-on来执行任务。但我想知道是否有办法让rufus-scheduler和免费heroku dyno组合起作用。

3 个答案:

答案 0 :(得分:7)

以下是如何使其在Rails上运行的示例:

#
# config/initializers/scheduler.rb
require 'rufus-scheduler'

# Let's use the rufus-scheduler singleton
#
s = Rufus::Scheduler.singleton

# Awesome recurrent task...
#
s.every '1m' do
  Rails.logger.info "hello, it's #{Time.now}"
end

查看documentation

答案 1 :(得分:1)

您可以改为使用Heroku的Scheduler并将此任务移至使用Heroku的本机工具:https://devcenter.heroku.com/articles/scheduler

答案 2 :(得分:0)

我解决了这个问题,只是添加到我的Procfile

web: bundle exec passenger start -p $PORT --max-pool-size 3 --min-instances 2