In ./config/initializers
I've created a file called task_scheduler.rb
and it contains the following code:
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. But it stops working when I deploy the app to Heroku (free).
I'm using Phusion Passenger 4.0.27
as the application server.
My Gemfile looks like this:
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:
web: bundle exec passenger start -p $PORT --max-pool-size 3
I have no workers running. I'm using one free standard web
Dyno.
Any idea why rufus-scheduler
is not working while deployed to Heroku?
UPDATE
I know I could create a customer .rake
file and use the free Heroku Scheduler add-on
to execute the task. But I'm wondering if there's a way to get the rufus-scheduler
and free heroku
dyno combination to work.