我在我的应用程序中使用订阅和计划以及Stripe。我的计划实体在模型上有很多“可视”数据,并且与共享标识符的支付网关有关。
我已经有了一个生成基本计划数据的迁移。像这样:
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'
)
我想创建一个作业,当系统正常时,从我的本地数据库中获取所有数据,然后创建条纹计划。我的代码是这样的:
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
答案 0 :(得分:2)
我认为您正在将rake任务与ActiveJob混淆。
如果要在rails console
中运行作业,则只需执行SyncLocalPlansWithStripe.perform_now
。参见https://guides.rubyonrails.org/active_job_basics.html#enqueue-the-job
如注释中所建议,您还可以使用Rails运行器直接从命令行运行作业:rails runner "SyncLocalPlansWithStripe.perform_now"
或者,如果您希望将此操作作为rake任务运行,则需要为此创建一个。参见https://guides.rubyonrails.org/command_line.html#custom-rake-tasks