Callbacks
TL;DR
class User::Charge < ApplicationService
before_service_run :log_start
after_service_run :log_end
on_service_failure :notify_admin
around_step_run :benchmark_step
step :authorize
step :charge
private
def log_start(service)
Rails.logger.info "Starting #{service.class.name}"
end
def log_end(service)
Rails.logger.info "Finished #{service.class.name}"
end
def notify_admin(service)
AdminMailer.service_failed(service).deliver_later
end
def benchmark_step(service, step_name)
start = Time.current
yield
duration = Time.current - start
Rails.logger.info "Step #{step_name} took #{duration}s"
end
endAvailable Callbacks
Service Callbacks
Callback
When it runs
Arguments
Step Callbacks
Callback
When it runs
Arguments
Defining Callbacks
Using Symbols (Method Names)
Using Procs/Lambdas
Around Callbacks
Around Callbacks with Procs
Multiple Callbacks
Multiple Around Callbacks
Callback Inheritance
Deep Inheritance
Execution Order
Service Callbacks Order
Step Callbacks Order (for each step)
Use Cases
Logging
Benchmarking
Error Tracking
Audit Trail
Database Instrumentation
What's Next?
Last updated