控制器对延迟作业的操作

编程入门 行业动态 更新时间:2024-10-21 03:33:49
本文介绍了控制器对延迟作业的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在上载制表符分隔的文档并在控制器中进行处理.一切正常,但是在大文件上可能要花费一些时间.我想将其移到delay_job上,该应用程序在我的应用程序中的其他位置上工作过,但是由于它在控制器中,因此无法以相同的方式调用.

I am uploading a tab-delimited document and processing in the controller. Everything works fine, but can take some time on a large file. I want to move this to a delay_job, which I have working elsewhere in my app, but as this is in the controller, cannot be called in the same way.

该表单调用process_file操作,该操作又调用salesupload操作.我应该如何将其转变为后台工作?

The form calls on the process_file action, which in turn calls on the salesupload action. How should I turn this into a background job?

class SalesController < ApplicationController def salesupload(file) uploaded_io = file.read numrows = "uploaded_io.size" FasterCSV.parse(uploaded_io, {:headers => true, :col_sep =>"\t"}).each do |row_data| full, upc, _discard, isrc = row_data[26].match(/^([^_]+)(_(.+))?/).to_a new_record = AppleSale.new( 'provider' => row_data[0], 'provider_country' => row_data[1], 'vendor_identifier' => row_data[2] ) new_record.save end end def process_file file = params[:apple_sale][:tsv_file] salesupload(file) end end

推荐答案

当我必须执行此操作时,我发现控制器中定义的方法必须是类方法.我不记得为什么会这样,我认为这与拥有更明确的接收者有关.因此,我要做的是将salesupload方法设为类方法,然后在其上调用.delay.

I found when I had to do this that the method defined in the controller has to be a class method. I can't remember why this was, I think it had to do with having a more explicit receiver. So what I would do is make the salesupload method a class method, and then just call .delay on it.

def self.salesupload(files) # code end def process_file file = params[:apple_sale][:tsv_file] SalesController.delay.salesupload(file) head :no_content end

您应该很好!我还通过AJAX调用了原始方法(在本例中为process_file),然后附加了head :no_content,以便它返回某些内容而无需重定向或任何其他操作.

And you should be good to go! I also made my original method (process_file in this case) called via AJAX, and then I appended the head :no_content so that it returned something without needing a redirect or anything.

更多推荐

控制器对延迟作业的操作

本文发布于:2023-11-27 09:17:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   控制器   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!