|
145 | 145 | # end |
146 | 146 | # > ``` |
147 | 147 | # |
| 148 | +# # Deferred Configuration |
| 149 | +# • _config.deferred.backend_ |
| 150 | +# |
| 151 | +# > Specifies the backend for deferred tasks. Supported values are `:disk`, which uses disk storage, or `nil`, which disables persistence of deferred tasks. |
| 152 | +# > The `:disk` backend accepts the following options: |
| 153 | +# > |
| 154 | +# > - `:path` - the path to the directory where deferred tasks will be stored. Defaults to `storage`. |
| 155 | +# > - `:prefix` - the prefix for the deferred task files. Defaults to `deferred-`. |
| 156 | +# > - `:fsync_frequency` - the frequency of `fsync` calls in seconds. Defaults to `0.5`. |
| 157 | +# |
| 158 | +# > ```ruby |
| 159 | +# config.deferred.backend = :disk, { path: "storage" } |
| 160 | +# > ``` |
| 161 | +# |
148 | 162 | # # Transient Settings |
149 | 163 | # |
150 | 164 | # The settings described in this section should be configured using **environment variables** and are either temporary or will become the default in the future. |
@@ -208,6 +222,10 @@ def openapi |
208 | 222 | @openapi ||= OpenAPI.new |
209 | 223 | end |
210 | 224 |
|
| 225 | + def deferred |
| 226 | + @deferred ||= Deferred.new |
| 227 | + end |
| 228 | + |
211 | 229 | def internal |
212 | 230 | @internal ||= Internal.new |
213 | 231 | end |
@@ -347,6 +365,85 @@ class OpenAPI |
347 | 365 | attr_accessor :tag_resolver |
348 | 366 | end |
349 | 367 |
|
| 368 | + class Deferred |
| 369 | + def initialize |
| 370 | + @backend_class = Rage::Deferred::Backends::Disk |
| 371 | + @backend_options = parse_disk_backend_options({}) |
| 372 | + |
| 373 | + @configured = false |
| 374 | + end |
| 375 | + |
| 376 | + def backend |
| 377 | + @backend_class.new(**@backend_options) |
| 378 | + end |
| 379 | + |
| 380 | + def backend=(config) |
| 381 | + @configured = true |
| 382 | + |
| 383 | + backend_id, opts = if config.is_a?(Array) |
| 384 | + [config[0], config[1]] |
| 385 | + else |
| 386 | + [config, {}] |
| 387 | + end |
| 388 | + |
| 389 | + @backend_class = case backend_id |
| 390 | + when :disk |
| 391 | + @backend_options = parse_disk_backend_options(opts) |
| 392 | + Rage::Deferred::Backends::Disk |
| 393 | + when nil |
| 394 | + Rage::Deferred::Backends::Nil |
| 395 | + else |
| 396 | + raise ArgumentError, "unsupported backend value; supported values are `:disk` and `nil`" |
| 397 | + end |
| 398 | + end |
| 399 | + |
| 400 | + def default_disk_storage_path |
| 401 | + Pathname.new("storage") |
| 402 | + end |
| 403 | + |
| 404 | + def default_disk_storage_prefix |
| 405 | + "deferred-" |
| 406 | + end |
| 407 | + |
| 408 | + def has_default_disk_storage? |
| 409 | + default_disk_storage_path.glob("#{default_disk_storage_prefix}*").any? |
| 410 | + end |
| 411 | + |
| 412 | + def configured? |
| 413 | + @configured |
| 414 | + end |
| 415 | + |
| 416 | + private |
| 417 | + |
| 418 | + def parse_disk_backend_options(opts) |
| 419 | + if opts.except(:path, :prefix, :fsync_frequency).any? |
| 420 | + raise ArgumentError, "unsupported backend options; supported values are `:path`, `:prefix`, `:fsync_frequency`" |
| 421 | + end |
| 422 | + |
| 423 | + parsed_options = {} |
| 424 | + |
| 425 | + parsed_options[:path] = if opts[:path] |
| 426 | + opts[:path].is_a?(Pathname) ? opts[:path] : Pathname.new(opts[:path]) |
| 427 | + else |
| 428 | + default_disk_storage_path |
| 429 | + end |
| 430 | + |
| 431 | + parsed_options[:prefix] = if opts[:prefix] |
| 432 | + opts[:prefix].end_with?("-") ? opts[:prefix] : "#{opts[:prefix]}-" |
| 433 | + else |
| 434 | + default_disk_storage_prefix |
| 435 | + end |
| 436 | + |
| 437 | + parsed_options[:fsync_frequency] = if opts[:fsync_frequency] |
| 438 | + (opts[:fsync_frequency].to_i * 1_000).round |
| 439 | + else |
| 440 | + 500 |
| 441 | + end |
| 442 | + |
| 443 | + parsed_options |
| 444 | + end |
| 445 | + end |
| 446 | + |
350 | 447 | # @private |
351 | 448 | class Internal |
352 | 449 | attr_accessor :rails_mode |
|
0 commit comments