我怎样才能为这个动作/动作实现delayed_job gem?它从 API 获取 plist 并将这些值插入数据库。我只是希望它作为后台进程插入数据......
def get_videos_from_outer(page=params[:page], kid=params[:kid], totalCount="")
@videos = Video.where(program_id: params[:p_id])
@program = Program.find(params[:p_id])
@fetch = Typhoeus::Request.post("URL", :params => {:commandtype=>"getprogramepisodes", :feedtype=>"plist",:id=>kid.to_i, :page=>page.to_i})
@plist = Plist::parse_xml(@fetch.body.force_encoding 'utf-8')
@totalCount = @plist.second.first['totalCount']
if !totalCount.blank?
@totalCount = totalCount
end
import_outer_videos(@plist, kid, page.to_i, @totalCount.to_i)
end
和import_outer_videos
方法。
private
def import_outer_videos(plist, kid, page, totalCount)
@totalCount = totalCount
plist.second.each_with_index do |t, i|
# First page has odd data and here we're getting rid off them
if page.to_i==1
if i > 0
@new = Video.create(:thumb_path=>t['tnPath'], :vid=>t['id'], :title=>t['title'], :type=>t['type'], :kid=>kid, :program_id=>@program.id)
end
else
@new = Video.create(:thumb_path=>t['tnPath'], :vid=>t['id'], :title=>t['title'], :type=>t['type'], :kid=>kid, :program_id=>@program.id)
end
end
if page.to_i < (@totalCount.to_i/20) + 1
page = page.to_i + 1
get_videos_from_outer(page.to_i, kid.to_i, @totalCount)
else
redirect_to :action=>"index", :p_id=>params[:p_id]
end
if @new.errors.blank?
flash[:notice]="#{@totalCount} videos has been transfered."
else
flash[:notice]="No new video."
end
end