如何在 Rails Ruby 中仅 运行 一次 ActiveJob?
How to run an ActiveJob only once in Ruby on Rails?
我在我的应用程序中与 Stripe 一起使用订阅和计划。我的计划实体在模型上有很多 "visual" 数据,并且与共享标识符的支付网关相关。
我已经有了生成基本计划数据的迁移。像这样:
class CreateBasicPlanData < ActiveRecord::Migration[5.1]
def change
Plan.create(name:'Hobby',
visibility: 'Low Visibility',
card_description:'Free portfolio listing good for beginners.',
features_description:'<ul><li>5 Portfolio Images</li><li>Messaging to other Talent</li><li>Basic Search Ranking</li></ul>',
price: 0,
css:'plan-hobby',
number_albums: 1,
number_photos_per_album: 5,
payment_gateway_plan_identifier: 'hobby'
)
Plan.create(name:'Professional', card_description:'Solid portfolio for those wanting more exposure and booking opportunities.',
visibility: 'High Visibility',
features_description:'<strong>Everything included in Hobby <em>PLUS:</em></strong><ul><li>25 Portfolio Images</li><li>Intermediate Search Ranking</li><li>Multi-state portfolio listing</li></ul>',
price: 4.99,
css:'plan-professional',
number_albums: 5,
number_photos_per_album: 25,
payment_gateway_plan_identifier: 'professional'
)
我想创建一个作业,当系统正常时,从我的本地数据库获取所有数据,并创建 Stripe 计划。我的代码是这样的:
class SyncLocalPlansWithStripe < ActiveJob::Base
def perform
plans = Plan.all
#delete all the plans on stripe
Plan.transaction do
begin
puts 'Start deleting'
Stripe::Plan.list.each do |plan_to_delete|
plan_to_delete.delete
end
puts 'End deleting'
end
end
Plan.transaction do
begin
plans.each do |plan|
PaymentGateway::CreatePlanService.new(plan).run
end
rescue PaymentGateway::CreatePlanServiceError => e
puts "Error message: #{e.message}"
puts "Exception message: #{e.exception_message}"
end
end
end
我的问题是。我怎样才能 运行 这个工作,当我想要的时候,从控制台只需要一次?
类似 rake:job 运行 sync_local_plans_with_stripe
我认为您混淆了 rake 任务和 ActiveJob。
如果你想从 rails console
中 运行 一个作业,你可以只执行 SyncLocalPlansWithStripe.perform_now
。参见 https://guides.rubyonrails.org/active_job_basics.html#enqueue-the-job
如评论中所建议,您也可以使用 Rails 运行 直接从命令行 运行 作业:rails runner "SyncLocalPlansWithStripe.perform_now"
或者,如果您更愿意 运行 将此作为抽取任务,那么您需要为此创建一个抽取任务。参见 https://guides.rubyonrails.org/command_line.html#custom-rake-tasks
我在我的应用程序中与 Stripe 一起使用订阅和计划。我的计划实体在模型上有很多 "visual" 数据,并且与共享标识符的支付网关相关。
我已经有了生成基本计划数据的迁移。像这样:
class CreateBasicPlanData < ActiveRecord::Migration[5.1]
def change
Plan.create(name:'Hobby',
visibility: 'Low Visibility',
card_description:'Free portfolio listing good for beginners.',
features_description:'<ul><li>5 Portfolio Images</li><li>Messaging to other Talent</li><li>Basic Search Ranking</li></ul>',
price: 0,
css:'plan-hobby',
number_albums: 1,
number_photos_per_album: 5,
payment_gateway_plan_identifier: 'hobby'
)
Plan.create(name:'Professional', card_description:'Solid portfolio for those wanting more exposure and booking opportunities.',
visibility: 'High Visibility',
features_description:'<strong>Everything included in Hobby <em>PLUS:</em></strong><ul><li>25 Portfolio Images</li><li>Intermediate Search Ranking</li><li>Multi-state portfolio listing</li></ul>',
price: 4.99,
css:'plan-professional',
number_albums: 5,
number_photos_per_album: 25,
payment_gateway_plan_identifier: 'professional'
)
我想创建一个作业,当系统正常时,从我的本地数据库获取所有数据,并创建 Stripe 计划。我的代码是这样的:
class SyncLocalPlansWithStripe < ActiveJob::Base
def perform
plans = Plan.all
#delete all the plans on stripe
Plan.transaction do
begin
puts 'Start deleting'
Stripe::Plan.list.each do |plan_to_delete|
plan_to_delete.delete
end
puts 'End deleting'
end
end
Plan.transaction do
begin
plans.each do |plan|
PaymentGateway::CreatePlanService.new(plan).run
end
rescue PaymentGateway::CreatePlanServiceError => e
puts "Error message: #{e.message}"
puts "Exception message: #{e.exception_message}"
end
end
end
我的问题是。我怎样才能 运行 这个工作,当我想要的时候,从控制台只需要一次?
类似 rake:job 运行 sync_local_plans_with_stripe
我认为您混淆了 rake 任务和 ActiveJob。
如果你想从 rails console
中 运行 一个作业,你可以只执行 SyncLocalPlansWithStripe.perform_now
。参见 https://guides.rubyonrails.org/active_job_basics.html#enqueue-the-job
如评论中所建议,您也可以使用 Rails 运行 直接从命令行 运行 作业:rails runner "SyncLocalPlansWithStripe.perform_now"
或者,如果您更愿意 运行 将此作为抽取任务,那么您需要为此创建一个抽取任务。参见 https://guides.rubyonrails.org/command_line.html#custom-rake-tasks