Ruby on rails Rails:模型方法不使用';t在开发中作为cron任务执行
我目前正在用RubyonRails编写一个应用程序,它从YouTube获取数据(videoId作为字符串,标题作为字符串,缩略图url作为字符串,视频创建日期作为日期时间类型),并将其保存到数据库中 我以前在纯Ruby中测试过YouTube API,我知道输出应该是正确的,特别是它应该返回我前面提到的所有数据。然而,我似乎无法让它在Rails中作为使用gem的cron作业工作 下面我有两个与我的模型交互的方法。其中一个有效,另一个无效。不需要的是Ruby on rails Rails:模型方法不使用';t在开发中作为cron任务执行,ruby-on-rails,ruby,cron,youtube-api,whenever,Ruby On Rails,Ruby,Cron,Youtube Api,Whenever,我目前正在用RubyonRails编写一个应用程序,它从YouTube获取数据(videoId作为字符串,标题作为字符串,缩略图url作为字符串,视频创建日期作为日期时间类型),并将其保存到数据库中 我以前在纯Ruby中测试过YouTube API,我知道输出应该是正确的,特别是它应该返回我前面提到的所有数据。然而,我似乎无法让它在Rails中作为使用gem的cron作业工作 下面我有两个与我的模型交互的方法。其中一个有效,另一个无效。不需要的是self.create\u youtube\u p
self.create\u youtube\u posts
,需要的是我创建的测试方法,该方法使用where gem(self.create\u test\u posts
)检查它是否作为cron作业执行
我的问题是,我甚至看不到第一个错误消息,代码根本不执行。如果有人能向我解释为什么一种方法有效而另一种方法无效,或者只是简单地把我推向正确的方向,你的帮助将不胜感激。请注意,这不是一个在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
every 1.day do
runner "Post.create_youtube_posts", :environment => :development
end
如果您的CRON在给定的时间段内成功执行,以下是可能帮助您进行调试的命令列表。
grep CRON/var/log/syslog
——检查CRON运行的历史记录bundle exec where或where-i
——更新crontabcrontab-l
——查看更新的cron(适用于所有用户)crontab-e,针对特定用户:crontab-e-u用户名
——列出用户cron的任务crontab-r
——从cron后台处理程序中删除crontab条目,但不从crontab文件中删除。仅供参考:
Minute-Hour-Day of Month-Day of Week用户命令
(0-59)(0-23)(1-31)(1-12或1-12月)(0-6或太阳周六)
0 2***root/usr/bin/find
如果您的CRON在给定的时间段内成功执行,以下是可以帮助您进行调试的命令列表。grep CRON/var/log/syslog
——检查CRON运行的历史记录bundle exec where或where-i
——更新crontabcrontab-l
——查看更新的cron(适用于所有用户)crontab-e,针对特定用户:crontab-e-u用户名
——列出用户cron的任务crontab-r
——从cron后台处理程序中删除crontab条目,但不从crontab文件中删除。仅供参考:
Minute-Hour-Day of Month-Day of Week用户命令
(0-59)(0-23)(1-31)(1-12或1-12月)(0-6或太阳周六)
0 2***root/usr/bin/find
我已经设法解决了我的问题。正如我所料,问题出在我的Ruby代码上。下面是我在post.rb文件中所做的更改。我想我的变量没有被传递
app/models/post.rb——已更新
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
我已经设法解决了我的问题。正如我所料,问题出在我的Ruby代码上。下面是我在post.rb文件中所做的更改。我想我的变量没有被传递 app/models/post.rb——已更新
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
crontab-l
的输出是什么:ytscrape 0***/bin/bash-l-c'cd/home/kos/rails\u projects/ytscrape&&bin/rails runner-e development'\'Post.创建youtube\u posts'\'35;为ytscrape 0***/bin/bash-l-c'cd/home/kos/rails\u projects\u/ytscrape创建youtube\u posts'\'s生成任务时结束为:ytscrape生成任务时开始:/home/kos/rails\u projects/ytscrap/config/schedule.rb0***/bin/bash-l-c'cd/home/kos/rails\u projects/ytscrap&&bin/rails runner-e development'\''Post.create\u youtube\u posts'\''''''''''''''''.\n在生成任务时结束:/home/kos/rails\rails\u projects/ytscrap/config/schedule.rb的时候,我认为我的cron工作正常。因为当我用我的工作方法运行它时,一切似乎都正常。crontab-l
\35;每当为以下对象生成任务时就开始:ytscrape 0***/bin/bash-l-c'cd/home/kos/rails\u projects/ytscrape&&bin/rails runner-e development'\'\'''Post.创建youtube\u posts'\'''''.\35;每当为:ytscrape生成任务时就结束无论何时为/home/kos/rails\u projects/ytscrap/config/schedule.rb 0***/bin/bash-l-c'cd/home/kos/rails\u projects/ytscrap&&bin/rails runner-e development'Post.create\u youtube\u posts''''''''''.\n无论何时为/home/kos/rails\rails\u projects\ytscrap/ytscrap/config/schedule.rb生成任务,我认为我的cron都可以正常工作是因为当我用我的工作方法运行它时,一切似乎都正常。crontab-l
\35;每当为以下对象生成任务时就开始:ytscrape 0***/bin/bash-l-c'cd/home/kos/rails\u projects/ytscrape&&bin/rails runner-e development'\'\'''Post.创建youtube\u posts'\'''''.\35;每当为:ytscrape生成任务时就结束无论何时为/home/kos/rails\u projects/ytscrap/config/schedule.rb 0***/bin/bash-l-c'cd/home/kos/rails\u projects/ytscrap&&bin/rails runner-e development'Post.create\u youtube\u posts''''''''''.\n无论何时为/home/kos/rails\rails\u projects\ytscrap/ytscrap/config/schedule.rb生成任务,我认为我的cron都可以正常工作是因为当我用我的工作方法运行它时,一切看起来都很好。谢谢你的回复。我的克朗工作得很好。我以为这只是Ruby代码的问题。我把我的答案贴在下面谢谢你的回复。我的克朗工作得很好。我以为这只是Ruby代码的问题。我把我的答案贴在下面谢谢你的回复。我的克朗工作得很好。我以为这只是Ruby代码的问题。我把答案贴在下面