Ruby on Rails, delayed_job

2019-09-02 02:11发布

问题:

I am using this gem for my application. I cannot find parameters that I need.I have two functions in my controller: create and analyse. I want to run an analyse-method after 5 min when a create-method was called or I have one more function in a model: process I want to run an anylse-method(from contoller) after 5 min when a process-method was called.

I have found smt like this:

handle_asynchronously :in_the_future, :run_at => Proc.new { 5.minutes.from_now }

but it does not say after wich function it should be called.

edit: I attempted to delay my job and it seemed to save everything, the problem is that after 1 min it did not run a perform_analysis method. What can be the problem?

model:

after_create :process_name, :perform_analysis

 def perform_analysis
   list=Analysis.do_picture_analyse
   update_image_url(list)

  end
 handle_asynchronously :perform_analysis, :run_at => Proc.new { 1.minutes.from_now }

SQL (8.3ms) INSERT INTO "delayed_jobs" ("attempts", "created_at", "failed_at", "handler", "last_error", "locked_at", "locked_by", "priority", "queue", "run_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["attempts", 0], ["created_at", Sat, 16 Mar 2013 14:30:19 UTC +00:00], ["failed_at", nil], ["handler", "--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/ActiveRecord:Arraydb\n attributes:\n id: 45\n arraydb_file_name: ND2_CD8_133A.CEL\n arraydb_content_type: !binary |-\n YXBwbGljYXRpb24vb2N0ZXQtc3RyZWFt\n arraydb_file_size: 12110237\n defined: 'yes'\n patient: ND\n tissue: CD8\n stimulus: \n chip_image: \n chip_info: \n user_id: \n hist: \n hist_RMA: \n created_at: 2013-03-16 14:30:17.698742003 Z\n updated_at: 2013-03-16 14:30:19.297934640 Z\nmethod_name: :perform_analysis_without_delay\nargs: []\n"], ["last_error", nil], ["locked_at", nil], ["locked_by", nil], ["priority", 0], ["queue", nil], ["run_at", Sat, 16 Mar 2013 14:31:19 UTC +00:00], ["updated_at", Sat, 16 Mar 2013 14:30:19 UTC +00:00]]

Thanks in advance

回答1:

You can use a callback to trigger the method. This will trigger the method :in_the_future to be queued up in the as a delayed job to run 5 min after the create action is complete.

after_create :in_the_future