加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails:after_create回调的delayed_job?

发布时间:2020-12-17 03:48:35 所属栏目:百科 来源:网络整理
导读:有没有办法使用delayed_job gem在后台运行after_create模型回调函数? 我有一个私有函数用作回调after_create:get_geolocation在用户注册后运行. 如何配置模型以在后台运行? 解决方法 是的,您应该能够从ActiveRecord回调中排队delayed_job任务.至 install
有没有办法使用delayed_job gem在后台运行after_create模型回调函数?

我有一个私有函数用作回调after_create:get_geolocation在用户注册后运行.

如何配置模型以在后台运行?

解决方法

是的,您应该能够从ActiveRecord回调中排队delayed_job任务.至 install and use delayed_job:

>将gem“delayed_job_active_record”添加到Gemfile并运行bundle install.
>运行以下命令在数据库中创建delayed_job支持表:

rails生成delayed_job:active_record

rake db:migrate
>在您的模型中:

class MyModel < ActiveRecord::Base
  after_commit :get_geolocation,on: :create

  private

  def get_geolocation
  end

  handle_asynchronously :get_geolocation
end

请注意,您应该是use after_commit instead of after_create to schedule your job,因此您可以避免在提交事务之前执行作业的情况.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读