Rails: 模型方法在开发中不作为 cron 任务执行

Rails: Model method doesn't execute as a cron task in development

我目前正在 Ruby Rails 上编写一个应用程序,它获取数据(videoId 作为字符串,标题作为字符串,缩略图 url 作为字符串,以及日期当视频创建为日期时间类型时)从 YouTube 并将其保存到数据库。

我之前在纯 Ruby 中测试过 YouTube API,我知道输出应该是正确的,特别是它应该 return 我之前提到的所有数据。但是,我似乎无法让它在 Rails 中作为 cron 作业在 gem.

时使用

下面有两种与我的模型交互的方法。其中一个有效,另一个无效。没有的是 self.create_youtube_posts 而有的是我创建的测试方法,用于检查它是否作为 cron 作业执行,每当 gem (self.create_test_posts).

我的问题是我什至没有看到第一个错误消息,代码根本不执行。如果有人可以向我解释为什么一种方法行得通而另一种方法行不通,或者只是简单地推动我朝着正确的方向前进,将不胜感激您的帮助。请注意,这不是在 YouTube API 上寻求帮助的问题,而是帮助我的 Ruby 代码。

app/models/post.rb

class Post < ActiveRecord::Base

    # This method is not working!
    def self.create_youtube_posts

        require 'faraday'
        require 'rubygems'
        require 'google/api_client'
        require 'trollop'

        developer_key = "MY_API_KEY_GOES_HERE"
        youtube_api_service_name = "youtube"
        youtube_api_version = "v3"

        opts = Trollop::options do
          opt :maxResults, 'Max results', :type => :int, :default => 18
          opt :order, 'Order', :type => String, :default => 'viewCount' 
          opt :publishedAfter, 'Date', :default => (Time.now - 1.day).to_datetime
        end

        client = Google::APIClient.new(:key => developer_key,
                               :authorization => nil)
        youtube = client.discovered_api(youtube_api_service_name, youtube_api_version)

        # Call the search.list method to retrieve results matching the specified
        # query term.
        opts[:part] = 'id,snippet'
        search_response = client.execute!(:api_method => youtube.search.list,
                                          :parameters => opts)

        search_response.data.items.each do |search_result|
            case search_result.id.kind
                when 'youtube#video'

                    # vi is short for videoId
                    vi = '<iframe width="425" height="349" src="http://www.youtube.com/embed/'
                       + "#{search_result.id.videoId}" 
                       + '" frameborder="0" allowfullscreen></iframe>'

                    # t is short for title
                    t = "#{search_result.snippet.title}"

                    # tn is short for thumbnail
                    tn = "#{search_result.snippet.thumbnails.high.url}"

                    # dt is short for datetime
                    dt = "#{search_result.snippet.publishedAt}"

                    @post = Post.new(videoid: vi, 
                                 title: t, 
                                 thumbnail: tn, 
                                 date: dt)
                    @post.save                  
            end
        end
    end

    # This method is working fine!
    def self.create_test_posts
        @test_post = Post.new(videoid: "Sample video id", 
                              title: "Sample Title", 
                              thumbnail: "Sample Thumbnail", 
                              date: (Time.now).to_datetime)
        @test_post.save
    end
end

作为额外参考,我包含了我用来生成 cron 作业的 schedule.rb 文件。

config/schedule.rb

every 1.day do
    runner "Post.create_youtube_posts", :environment => :development
end

如果您的 CRON 在给定的时间和期间成功执行,以下是可以帮助您进行调试的命令列表。

grep CRON /var/log/syslog -- 查看CRON的历史记录运行
bundle exec whenever or whenever -i -- 更新 crontab
crontab -l -- 查看更新后的 cron(适用于所有用户)
crontab -e, for a specific user: crontab -e -u username -- 列出用户 cron 的任务
crontab -r -- 从 cron 假脱机程序中删除您的 crontab 条目,但不从 crontab 文件中删除。

仅供参考:
分时日月日用户命令<br>(0-59) (0-23) (1-31) (1-12 或 Jan-Dec) (0-6 或星期六)<br>0 2 * * * root /usr/bin/find

我已经设法解决了我的问题。正如我所料,问题出在我的 Ruby 代码上。以下是我在 post.rb 文件中所做的更改。我认为我的变量不知何故没有通过。

app/models/post.rb --updated

search_response.data.items.each do |search_result|
    case search_result.id.kind
        when 'youtube#video'

            post = Post.new(videoid: "#{search_result.id.videoId}", 
                            title: "#{search_result.snippet.title}", 
                            thumbnail: "#{search_result.snippet.thumbnails.high.url}", 
                            date: "#{search_result.snippet.publishedAt}")
            post.save                   
    end
end