简体   繁体   English

如何使用 Rails 正确执行作业?

[英]How can i execute a job properly with rails?

The routes are like this: get:'/hi', to: 'tables#execute'路由是这样的: get:'/hi', to: 'tables#execute'

I have this controller but I don't know how can I execute it我有这个 controller 但我不知道如何执行它

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:注意:我已经像这样修改了我的 controller:

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. perform_later接受你想要传递给工作的东西。 These are passed to the job's perform method.这些被传递给作业的perform方法。 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.默认情况下,arguments 必须是 String、Integer、Float、NilClass、TrueClass、FalseClass、BigDecimal、Symbol、Date、Time、DateTime、ActiveSupport::TimeWithZone、ActiveSupport::Duration、Hash、ActiveSupport::HashWithIndifferentAccess、Array、Range,或 GlobalID::Identification 实例,尽管这可以通过添加自定义序列化程序来扩展。

A Controller object is not allowed, and also doesn't make much sense to pass to a background job. Controller object 是不允许的,传递给后台作业也没有多大意义。 It's the controller's job to decide what to pass to the job, probably something from params .控制器的工作是决定将什么传递给作业,可能是来自params的东西。

See Creating A Job in Active Job Basics for more.有关更多信息,请参阅 Active Job Basics 中的Creating A Job

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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