Reputation: 355
Im using subscriptions and plans in my application together with Stripe. My Plan entity has a lot of "visual" data on the model and is related with the payment gateway sharing a identifier.
I have already a migration that generates my basic plan data. Like this:
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'
)
I want to create a Job that, when the system is ok, get all the data from my local database, and create the Stripe Plans. My code is something like that:
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
My question is. How can I run this Job, when I want, only once, from the console?
Something like rake:job run sync_local_plans_with_stripe
Upvotes: 2
Views: 2184
Reputation: 1272
I think you are confusing rake tasks with ActiveJob.
If you want to run a job from within rails console
you can just execute SyncLocalPlansWithStripe.perform_now
. See https://guides.rubyonrails.org/active_job_basics.html#enqueue-the-job
As suggested in the comments you can also run the job directly from the command line using Rails runner: rails runner "SyncLocalPlansWithStripe.perform_now"
Or if you'd rather run this as a rake task then you'll need to create one for this instead. See https://guides.rubyonrails.org/command_line.html#custom-rake-tasks
Upvotes: 5