查看Rake任务中的日志(生产)

时间:2015-03-11 18:21:31

标签: ruby-on-rails logging heroku rake

当我在生产中运行这个rake任务(到登台应用程序)时,我希望得到一个答案来获取日志到终端。

我创建了一个rake任务来更新数据库中的现有记录。我在本地尝试过,它运行得很快,得到了我想要的输出。在当地,我有 4295 曲目。现在我试图在我的staging DB上运行它,它有 17,418 曲目。

我运行heroku run rake db:track_rank -a staging-appname并获取此信息:

Running `rake db:track_rank` attached to terminal... up, run.8608
Connecting to database specified by DATABASE_URL
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Starting the New Relic agent in "production" environment.
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : To prevent agent startup add a NEWRELIC_AGENT_ENABLED=false environment variable or modify the "production" section of your newrelic.yml.
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Reading configuration from config/newrelic.yml
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Environment: production
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : No known dispatcher detected.
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Application: Marmoset Music
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : delayed_job not available: No DJ worker present
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing Sinatra instrumentation
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing Dalli Memcache instrumentation
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing Net instrumentation
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing deferred Rack instrumentation
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing Rack::Builder middleware instrumentation
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing Rails 3+ middleware instrumentation
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing ActiveRecord instrumentation
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing legacy Excon instrumentation
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing DelayedJob instrumentation [part 1/2]
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing Rails 3 Controller instrumentation
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing Rails 3.1/3.2 view instrumentation
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Installing Rails3 Error instrumentation
** [NewRelic][03/11/15 18:04:40 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Finished instrumentation
** [NewRelic][03/11/15 18:04:41 +0000 49202e1b-0783-4094-bf1a-969e03b61bab (3)] INFO : Reporting to: https://rpm.newrelic.com/accounts/375675/applications/2431422
Updating track's rank points in group #1
Dalli/SASL authenticating as 021dd5
Dalli/SASL: 021dd5

它已经在那里坐了大约12分钟,所以我不确定它是否真的对#1组和#34;做了什么。

如何将一些日志发送到终端以查看发生了什么?

这是我的佣金任务:

desc "Updates all exisiting tracks rank"


namespace :db do
    task :track_rank => :environment do 

      batch_index = 1
        puts "Updating track's rank points in group ##{batch_index}"

            three_weeks_ago = 21.days.ago.to_date

            Track.find_in_batches do |group|

                group.each do |track|


                    listens = track.listens_count
                    downloads = track.downloads_count
                    purchases = track.purchases_count
                    staff_pick = 0
                    new_track = 0


                    if track.staff_pick?
                        staff_pick += 1
                    end

                    if track.created_at.to_date > three_weeks_ago
                        new_track += 2000
                    end


                    downloads *= 60
                    purchases *= 150
                    staff_pick *= 1000

                    rank_total = listens + downloads + purchases + staff_pick + new_track

                    if rank_total > 0
                        track.increment!(:rank, by = rank_total)
                    end

              end       
              batch_index +=1       
          end       

        puts "Tracks updated!"
    end
end

3 个答案:

答案 0 :(得分:0)

最简单的方法是让Rails记录rake任务进度和/或结果。只需改变一下:

puts "Tracks updated!"

到这个

Rails.logger.ingo "Tracks updated!" 

您也可以在中间添加一些调试日志,但这取决于您。

答案 1 :(得分:0)

运行heroku logs

您也可以过滤日志,查看types of logs

答案 2 :(得分:0)

似乎Heroku在记录rake任务时遇到问题。为了避免这种情况,您需要以分离模式运行rake任务。

heroku run:detached rake db:track_rank

来源:https://www.salsify.com/blog/engineering/capturing-logs-from-rake-tasks-and-rails-console-on-heroku