簡體   English   中英

如何使用delay_job延遲控制器動作?

[英]How to use delayed_job to delay a controller action?

如何才能對此動作實施delay_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

使用以下內容創建app/workers/my_job.rb

class MyJob < Struct.new(:p1, :p2)
  def perform
    # put all your hard work here.
    # APIClass.get_videos_from_outer(p1, p2)
    # or something.
  end
end

然后調用:

Delayed::Job.enqueue(MyJob.new(foo, bar), 0, 2.hours.from_now)

或者其他的東西。

這可能很有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM