【问题标题】:Why does this bot run locally but not on heroku?为什么这个机器人在本地运行而不是在 Heroku 上运行?
【发布时间】:2015-04-27 06:44:35
【问题描述】:

我对 Rails 和编程还是很陌生,无论如何请帮助我,提前谢谢!

我编写了一个小型机器人,它可以在我的本地系统上完美运行,但是当我上传它时什么也没做。

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

这是控制器.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

宝石文件

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

有什么想法吗?

【问题讨论】:

  • 嗨,欢迎来到堆栈溢出。所以我的第一个简单的建议是“printf 调试”......即,在每个主要分支(循环之前,循环内部等)添加记录器调用,每个都有不同的消息来解释你在代码中的哪个点直到...,看看代码在它死前到达什么位置。
  • 我的另一个建议是:删除循环...测试如果机器人只尝试一次然后结束会发生什么...

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


【解决方案1】:

Heroku 有一个request timeout of 30 seconds 用于无法更改的 HTTP 请求。因此,虽然离开页面加载会起作用,但它最终会在请求超时时终止。

Heroku 的运行方式是使用 background worker

【讨论】:

  • 你知道是否有一种方法可以构建应用程序,以便它只能使用工作类型测功机作为我的 1 个免费测功机,而不必使用网络测功机,因为我在整个过程中无论如何,想要创建服务器端?
  • 或者,如果我在 heroku 中达到某个 dyno 小时限制,是否有办法自动停止一切运行,这样我就可以在 750 小时的免费小时限制下关闭所有东西?如果我用两个测功机连续运行这个程序,我认为必须在这个月的一半。
【解决方案2】:

你在使用 Watir 吗?

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

也检查这个答案:

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

【讨论】:

  • 我正在尝试,但我认为这个页面解决方案修复了它。 stackoverflow.com/questions/12495463/…
  • 感谢您为我指明正确的方向!现在看起来我遇到了其他评论所说的我的页面超时的地方。
  • Heroku 超时问题是一个已知问题,您的请求最多应花费 30 秒,如果您希望您的请求需要更多时间,您有 2 个选项,1- 将其设为延迟作业并在背景,因此您的请求将快速响应,2-如果您现在确实需要处理它,并且您可以将请求分成多个请求,每个请求可能需要不到 30 秒,因此请继续使用 ajax 和 in 从前端发送请求每个成功,您可以发送第二个,甚至可以将它们全部发送并跟踪它们何时全部完成。
猜你喜欢
  • 2012-11-07
  • 2018-02-05
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 2020-09-21
  • 2014-04-07
相关资源
最近更新 更多