为什么这个机器人在本地运行但不在heroku上运行?

时间:2015-04-27 06:44:35

标签: ruby-on-rails ruby ruby-on-rails-4 heroku

我对轨道和编程一般都很陌生,请帮助我,提前谢谢!

我写了一个小机器人,它在我的本地系统上运行得很好,但是当我上传它时什么都不做。

我的html.erb只是调用下面的bot方法。它是一个无限循环,但在本地服务器上它只允许机器人基本上在后台运行,而页面永远加载,这对我有用。我知道该应用程序已正确部署,因为我注释掉了bot方法,只是在空白页面上打印了一些东西并且完美无缺。所以它与我的bot方法有关。 唯一的问题是,当我离开在heroku环境中运行的bot方法时,会弹出一个页面,说“我们抱歉出错了”并告诉我检查日志,除了日志没有给我任何错误只有一个ping通知:

Apr 26 23:34:08 guarded-falls-5003 heroku/router: at=info method=GET path="/" host=guarded-falls-5003.herokuapp.com request_id=ae3616c7-2ff6-4bdd-9738-03a2cc291f96 fwd="50.31.164.139" dyno=web.1 connect=2ms service=13ms status=500 bytes=1754

这是controller.rb

require 'rubygems' 
require 'watir-webdriver' 
require 'phantomjs'

def time(t)
      mm, ss = t.divmod(60)
      hh, mm = mm.divmod(60)        
      dd, hh = hh.divmod(24)       
      return "%d days, %d hours, %d minutes and %d seconds  " % [dd, hh, mm, ss]
  end 

  def remaining_time(delay)
    time = Time.now.to_f
    fin = delay + time

    while fin > time
      time = Time.now.to_f
      @finished = "Current delay is #{time(fin-time)} \r"
      sleep 1;
    end
    print "\n"
  end


  ####################################################################
  #                          bot                                     #
  ####################################################################

def bot

  # bots login information
  name = "*******"
  email = "**********"
  password = "*********"

  #channel they are posting to on output website
  channel = "Sports"


  ####################################################################
  # set the following url to the channel you would like to pull from #
  ####################################################################

  # input website (video)
  url = "**************"

  ####################################################################
  #                          bot code                                #
  ####################################################################

  video = ""

  ########################### Loop ###################################

  loop do

    # Starts the Browser, enters URL, and goes to the videos page
    browser = Watir::Browser.new :phantomjs
    browser.goto(url + "/videos")
    # click on the class for the link of the video. Note that this just clicks on the first one it finds
    browser.link(:class, "yt-uix-sessionlink yt-uix-tile-link  spf-link  yt-ui-ellipsis yt-ui-ellipsis-2").click

    # Checks if the current video is already stored as the video variable 
      if video != browser.url

        # Set video variable to current url
        video = browser.url

        # Close and open a new video because phantomjs bugs out when you try 
        # to change websites on an already existing window
        browser.close
        browser = Watir::Browser.new :phantomjs

        # goto output website sign in page and sign in
        browser.goto "**************" 
        browser.text_field(:id, "user_email").set(email)
        browser.text_field(:id, "user_password").set(password)
        browser.button(:value,"Sign in").click

        # Upload the video (resize because search bar is hidden at default size)
        browser.window.resize_to(1600, 1000)
        browser.text_field(:id, "q").set(video)
        browser.button(:text, "Upload").click
        browser.select_list(:id, "video_channel_id").select(channel)
        # browser.button(:text,"Create Video").click

        puts "uploaded #{video}"
        remaining_time(delay)

        $stdout.flush
        sleep(delay)

        # Exit Browser
        browser.close 

      else

        browser.close

        puts "Did not upload anything. The video has already been uploaded." 
        remaining_time(delay) 

        $stdout.flush
        sleep(delay)

      end



  end 

end

的Gemfile

source 'https://rubygems.org'

gem 'newrelic_rpm'
gem 'phantomjs'
gem 'watir-webdriver'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

Heroku对于无法更改的HTTP请求有request timeout of 30 seconds。因此,当离开页面加载时,它将最终在请求超时时终止。

Heroku运行方式的方法是使用background worker

答案 1 :(得分:0)

你在使用Watir吗?

要在Heroku上使用PhantomJS,您需要使用Heroku PhantomJS buildpack

检查这个答案:

Can you deploy Watir on Heroku to generate HTML Snapshots? If so, how?