简体   繁体   中英

How can i execute a job properly with rails?

The routes are like this: get:'/hi', to: 'tables#execute'

I have this controller but I don't know how can I execute it

class TableController < ApplicationController
  def execute
    CurrentJob.set(wait: 2.minutes).perform_later(self)
    render plain: 'OK'
  end
end

I am getting the below error:

ActiveJob::SerializationError in TableController#execute

Unsupported argument type: TableController

Note: I have modified my controller like this:

def execute
    CurrentJob.perform_later params[:name]
    render plain: 'OK'
end

Let me know how to execute it.

perform_later takes what you want to pass along to the job. These are passed to the job's perform method. It only takes certain types which it knows how to serialize .

By default the arguments must be either String, Integer, Float, NilClass, TrueClass, FalseClass, BigDecimal, Symbol, Date, Time, DateTime, ActiveSupport::TimeWithZone, ActiveSupport::Duration, Hash, ActiveSupport::HashWithIndifferentAccess, Array, Range, or GlobalID::Identification instances, although this can be extended by adding custom serializers.

A Controller object is not allowed, and also doesn't make much sense to pass to a background job. It's the controller's job to decide what to pass to the job, probably something from params .

See Creating A Job in Active Job Basics for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM