Module: CMDx::Middlewares::Timeout
Overview
Middleware for enforcing execution time limits on tasks.
The Timeout middleware provides execution time control by wrapping task execution with Ruby’s Timeout module. It automatically fails tasks that exceed the configured time limit and provides detailed error information including the exceeded limit.
Constant Summary collapse
- DEFAULT_LIMIT =
Default timeout limit in seconds when none is specified.
3
Instance Method Summary collapse
-
#call(task, **options) { ... } ⇒ Object
Middleware entry point that enforces execution time limits.
Instance Method Details
#call(task, **options) { ... } ⇒ Object
Middleware entry point that enforces execution time limits.
Evaluates the condition from options and applies timeout control if enabled. Supports various timeout limit configurations including numeric values, task method calls, and dynamic proc evaluation.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cmdx/middlewares/timeout.rb', line 58 def call(task, **, &) return yield unless Utils::Condition.evaluate(task, ) limit = case callable = [:seconds] when Numeric then callable when Symbol then task.send(callable) when Proc then task.instance_eval(&callable) else callable.respond_to?(:call) ? callable.call(task) : DEFAULT_LIMIT end ::Timeout.timeout(limit, TimeoutError, "execution exceeded #{limit} seconds", &) rescue TimeoutError => e task.result.tap { |r| r.fail!("[#{e.class}] #{e.}", cause: e, limit:) } end |