有一系列状态机回调调用ActiveJob::Base
类来处理订单生命周期的连续阶段。
我遇到的问题是序列中的第二个作业没有排队,并且没有错误打印到终端。
第二项工作是
class WriteEncodedPreviewImageToFilesJob < ActiveJob::Base
queue_as :imagery_queue
def perform(order)
run_phase_of_order_lifecycle(order)
end
private
def run_phase_of_order_lifecycle(order)
begin
BackgroundWorkers::EncodedPreviewImageWriter.work(order)
ensure
order.render_final_image
end
end
end
这是在这里调用的:
class PreAuthorizeStripePaymentJob < ActiveJob::Base
queue_as :stripe_queue
def perform(order)
run_phase_of_order_lifecycle(order)
end
private
def run_phase_of_order_lifecycle(order)
response = create_stripe_charge(order)
if response['failure_code'] == nil
order.stripe_charge_id = response['id']
order.save!
order.write_encoded_preview_image_to_files # this line here!!!
end
response
end
def create_stripe_charge(order)
Stripe::Charge.create(
:amount => (order.original[:order][:price] * 100).round,
:currency => 'gbp',
:capture => false,
:source => order.stripe_id,
:description => "Charge for Order with id: #{order.id}",
:receipt_email => order.original[:order][:user_email]
)
end
end
由于此模块中定义的回调(请向下滚动):
module OrderLifecycle
extend ActiveSupport::Concern
include ActiveSupport::Callbacks
included do
state_machine :initial => :pending do
# States
event :pre_authorize_stripe_payment do
transition :pending => :stripe_payment_pre_authorized
end
event :write_encoded_preview_image_to_files do
transition :stripe_payment_pre_authorized => :encoded_preview_image_written_to_files # this line here!!! and...
end
event :render_final_image do
transition :encoded_preview_image_written_to_files => :final_image_rendered
end
event :upload_composition_to_parse do
transition :final_image_rendered => :composition_uploaded_to_parse
end
event :upload_print_file_to_printer do
transition :composition_uploaded_to_parse => :print_file_uploaded_to_printer
end
event :call_to_process_message do
transition :print_file_uploaded_to_printer => :process_message_called
end
event :capture_stripe_payment do
transition :process_message_called => :stripe_payment_captured
end
event :mark_as_complete do
transition :stripe_payment_captured => :complete
end
# Callbacks
before_transition :on => :pre_authorize_stripe_payment do |order|
PreAuthorizeStripePaymentJob.perform_later order
end
before_transition :on => :write_encoded_preview_image_to_files do |order|
WriteEncodedPreviewImageToFilesJob.perform_later order # this line here!!!
end
before_transition :on => :render_final_image do |order|
RenderFinalImageJob.perform_later order
end
before_transition :on => :upload_composition_to_parse do |order|
UploadCompositionToParseJob.perform_later order
end
before_transition :on => :upload_print_file_to_printer do |order|
UploadPrintFileToPrinterJob.perform_later order
end
before_transition :on => :call_to_process_message do |order|
CallToProcessMessageJob.perform_later order
end
before_transition :on => :capture_stripe_payment do |order|
CaptureStripePaymentJob.perform_later order
end
end
end
end
我的config/sidekiq.yml
是
---
:verbose: true
:queues:
- imagery_queue
- parse_queue
- stripe_queue
- printer_api_queue
当我最初启动Sidekiq时,终端的输出是:
2015-12-14T03:00:25.339Z 7664 TID-akza0 INFO: Booting Sidekiq 3.5.0 with redis options {:url=>"redis://localhost:6379"}
m,
`$b
.ss, $$b .,d$
`$$P,d$P' .,md$$P' ____ _ _ _ _
,$$$$$$bmmd$$$^' / ___|(_) __| | ___| | _(_) __ _
,d$$$$$$$$$$$P \___ \| |/ _` |/ _ \ |/ / |/ _` |
$s^' `"^$$$' ___) | | (_| | __/ <| | (_| |
$: ,$$P |____/|_|\__,_|\___|_|\_\_|\__, |
`b :$$ |_|
$$:
$$
.d$$
2015-12-14T03:00:28.076Z 7664 TID-akza0 INFO: Running in ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
2015-12-14T03:00:28.077Z 7664 TID-akza0 INFO: See LICENSE and the LGPL-3.0 for licensing details.
2015-12-14T03:00:28.077Z 7664 TID-akza0 INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org
2015-12-14T03:00:28.091Z 7664 TID-akza0 DEBUG: Middleware: Sidekiq::Middleware::Server::Logging, Sidekiq::Middleware::Server::RetryJobs, Sidekiq::Middleware::Server::ActiveRecord
2015-12-14T03:00:28.091Z 7664 TID-akza0 INFO: Starting processing, hit Ctrl-C to stop
2015-12-14T03:00:28.104Z 7664 TID-wjesw DEBUG: {:queues=>["imagery_queue", "parse_queue", "stripe_queue", "printer_api_queue"], :labels=>[], :concurrency=>25, :require=>".", :environment=>nil, :timeout=>8, :poll_interval_average=>nil, :average_scheduled_poll_interval=>15, :error_handlers=>[#<Sidekiq::ExceptionHandler::Logger:0x00000002a28dc8>], :lifecycle_events=>{:startup=>[], :quiet=>[], :shutdown=>[]}, :dead_max_jobs=>10000, :dead_timeout_in_seconds=>15552000, :verbose=>true, :config_file=>"config/sidekiq.yml", :strict=>true, :tag=>"xxxxxxxxx"}
答案 0 :(得分:0)
问题在于提前调用write_encoded_preview_image_to_files
方法。替换write_encoded_preview_image_to_files!
并插入binding.pry
透露:
9: def run_phase_of_order_lifecycle(order)
10: response = create_stripe_charge(order)
11: if response['failure_code'] == nil
12: order.stripe_charge_id = response['id']
13: order.save!
14: binding.pry
15: order.write_encoded_preview_image_to_files!
16: end
=> 17: response
18: end
[1] pry(#<PreAuthorizeStripePaymentJob>)> order.state
=> "pending"
[2] pry(#<PreAuthorizeStripePaymentJob>)> order.write_encoded_preview_image_to_files!
StateMachines::InvalidTransition: Cannot transition state via :write_encoded_preview_image_to_files from :pending (Reason(s): State cannot transition via "write encoded preview image to files")
from /home/*****/.rvm/gems/ruby-2.2.2/gems/state_machines-0.4.0/lib/state_machines/event.rb:224:in `block in add_actions'
[3] pry(#<PreAuthorizeStripePaymentJob>)> order.write_encoded_preview_image_to_files
然后在处理作业后检查rails控制台中的订单状态:
2.2.2 :044 > Order.last.state
Order Load (1.2ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."id" DESC LIMIT 1
=> "stripe_payment_pre_authorized"
2.2.2 :045 >
因此,可以通过向OrderLifecycle
模块添加以下回调来解决此问题:
after_transition :on => :pre_authorize_stripe_payment do |order|
order.write_encoded_preview_image_to_files
end