ruby - Rails: Model method doesn't execute as a cron task in development -
i writing application in ruby on rails fetches data (videoid string, title string, thumbnail url string, , date of when video created datetime type) youtube , saves database.
i have tested youtube api in pure ruby before , know output should correct, should return data mentioned previously. however, can't seem make work in rails cron job using whenever gem.
below have 2 methods interact model. 1 of them works , other 1 doesn't. 1 doesn't self.create_youtube_posts
, 1 test method created check whether gets executed cron job using whenever gem (self.create_test_posts
).
problem me don't see error messages first one, code doesn't execute. if explain me why 1 method work , other doesn't, or nudge me in right direction, appreciated. please note not question asking on youtube api, rather ruby code.
app/models/post.rb
class post < activerecord::base # method 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 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 search.list method retrieve results matching specified # query term. opts[:part] = 'id,snippet' search_response = client.execute!(:api_method => youtube.search.list, :parameters => opts) search_response.data.items.each |search_result| case search_result.id.kind when 'youtube#video' # vi short videoid vi = '<iframe width="425" height="349" src="http://www.youtube.com/embed/' + "#{search_result.id.videoid}" + '" frameborder="0" allowfullscreen></iframe>' # t short title t = "#{search_result.snippet.title}" # tn short thumbnail tn = "#{search_result.snippet.thumbnails.high.url}" # dt short datetime dt = "#{search_result.snippet.publishedat}" @post = post.new(videoid: vi, title: t, thumbnail: tn, date: dt) @post.save end end end # method 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
for reference included schedule.rb file use generate cron jobs.
config/schedule.rb
every 1.day runner "post.create_youtube_posts", :environment => :development end
here list of commands may debug if cron has executed @ given time , period.
grep cron /var/log/syslog
-- check history of cron run
bundle exec whenever or whenever -i
-- update crontab
crontab -l
-- see updated cron(for users)
crontab -e, specific user: crontab -e -u username
-- list users cron's task
crontab -r
-- removes crontab entry cron spooler, not crontab file.
reference:
minute hour day of month month day of week user command
(0-59) (0-23) (1-31) (1-12 or jan-dec) (0-6 or sun-sat)
0 2 * * * root /usr/bin/find
Comments
Post a Comment