2015-04-27 48 views
0

我還是很新的rails和編程一般請幫助我,無論如何,謝謝提前!爲什麼這個機器人在本地運行,但不在heroku上?

我寫了一個小型機器人,可以在我的本地系統上完美工作,但是當我上傳它時什麼也不做。

我的html.erb只是調用下面的bot方法。這是一個無限循環,但在本地服務器上,它只允許機器人在後臺運行,而頁面永久加載,這對我很有用。我知道這個應用程序是正確部署的,因爲我評論了這個bot方法,並且只是在空白頁上打印了一些東西,而且完美地工作。所以它與我的機器人方法中的某些東西有關。 唯一的問題是,當我離開在heroku環境上運行的bot方法時,會彈出一個頁面,提示「我們很抱歉發生了什麼問題」,並告訴我檢查日誌,除了日誌不會給我任何錯誤平通知:

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 

任何想法?

+0

您好,歡迎堆棧溢出。所以我的第一個簡單的建議就是「printf調試」......也就是說,在每個主要分支(循環之前,就在循環之內)添加記錄器調用,每一個都有不同的消息,說明代碼中的哪個點最多......並查看代碼在死亡之前到達的位置。 –

+0

我的其他建議是:刪除循環...測試如果機器人只是嘗試一次,然後結束會發生什麼...... –

回答

0

對於無法更改的HTTP請求,Heroku有request timeout of 30 seconds。所以在離開頁面加載的時候,它會在請求超時時終止。

Heroku的運行方式是使用background worker

+0

你知道是否有一種方法來構建應用程序,以便它只能使用工人類型dyno作爲我的1免費dyno而不是必須使用網絡dyno,因爲我期望創建的整個過程無論如何都是服務器端? – Alex

+0

或者如果我在heroku中達到特定的動態小時限制時,是否有辦法自動停止所有運行,以便我可以在750小時的限制內關閉所有內容?如果我連續使用兩個dynos運行這個程序,那麼我認爲這個程序將不得不在一個月的一半。 – Alex

0

你在使用Watir嗎?

要使用在Heroku PhantomJS,你需要使用一個Heroku PhantomJS buildpack

檢查這個答案還有:

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

+0

我在嘗試,但我認爲這個頁面解決方案修復了它。 http://stackoverflow.com/questions/12495463/how-to-run-phantomjs-on-heroku – Alex

+0

感謝您指出我在正確的方向!現在看起來我正在運行其他評論所說的我的頁面超時。 – Alex

+0

Heroku超時問題是一個已知問題,如果您的請求需要更多的時間,您最多需要花30秒的時間,如果您希望獲得更多的選擇,請選擇2個選項,1-延遲工作並在後臺運行,以便您的請求能夠快速響應,2-如果你真的需要現在處理它,你可以將請求分成多個請求,每個請求可以少於30秒,因此請使用ajax從前端發送請求,並且每個請求都可以發送第二,甚至你可以將它們全部發送並追蹤完成。 –

相關問題