简体   繁体   English

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

[英]How to get rufus-scheduler working with a Rails app deployed to Heroku?

In ./config/initializers I've created a file called task_scheduler.rb and it contains the following code: ./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

The rufus-scheduler works when testing in an development environment. rufus-scheduler在开发环境中进行测试时可以正常工作。 But it stops working when I deploy the app to Heroku (free). 但是当我将应用程序部署到Heroku(免费)时它停止工作。

I'm using Phusion Passenger 4.0.27 as the application server. 我正在使用Phusion Passenger 4.0.27作为应用程序服务器。

My Gemfile looks like this: 我的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

The Procfile required by Phusion Passenger contains the following: Phusion Passenger所需的Procfile包含以下内容:

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

I have no workers running. 我没有工人跑。 I'm using one free standard web Dyno. 我正在使用一个免费的标准web Dyno。

Any idea why rufus-scheduler is not working while deployed to Heroku? 知道为什么rufus-scheduler在部署到Heroku时不能正常工作吗?

UPDATE UPDATE

I know I could create a customer .rake file and use the free Heroku Scheduler add-on to execute the task. 我知道我可以创建一个客户.rake文件并使用免费的Heroku Scheduler add-on来执行任务。 But I'm wondering if there's a way to get the rufus-scheduler and free heroku dyno combination to work. 但我想知道是否有办法让rufus-scheduler和free heroku dyno组合起作用。

Here's an example of how to get it to work on Rails: 这是一个如何让它在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

Take a look at the documentation . 看一下文档

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

我刚刚添加到我的Procfile解决了这个问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM