### Complete Setup Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md A comprehensive example showing how to configure sidekiq-status in a Rails application. ```ruby # config/initializers/sidekiq.rb require 'sidekiq' require 'sidekiq-status' # Client configuration (used everywhere jobs are enqueued) Sidekiq.configure_client do |config| config.redis = { url: ENV.fetch('REDIS_URL', 'redis://localhost:6379') } # Configure client middleware with 30-minute default expiration Sidekiq::Status.configure_client_middleware( config, expiration: 30 * 60 ) end # Server configuration (used in Sidekiq worker process) Sidekiq.configure_server do |config| config.redis = { url: ENV.fetch('REDIS_URL', 'redis://localhost:6379') } # Configure server middleware with 30-minute default expiration Sidekiq::Status.configure_server_middleware( config, expiration: 30 * 60 ) # Server also needs client middleware for jobs enqueued from within workers Sidekiq::Status.configure_client_middleware( config, expiration: 30 * 60 ) end # Web UI configuration (if using Sidekiq Web) if defined?(Sidekiq::Web) require 'sidekiq-status/web' Sidekiq::Status::Web.default_per_page = 50 Sidekiq::Status::Web.per_page_opts = [25, 50, 100, 250] end ``` -------------------------------- ### Started At Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example demonstrating how to use the `started_at` method to display the job's start time. ```ruby start_time = Sidekiq::Status.started_at(job_id) if start_time puts "Started at: #{Time.at(start_time)}" end ``` -------------------------------- ### Manual Development Setup - Clone and Setup Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Steps to clone the repository and install Ruby dependencies using Bundler. ```bash git clone https://github.com/kenaniah/sidekiq-status.git cd sidekiq-status bundle install ``` -------------------------------- ### Setup Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/web-module.md Example configuration for Sidekiq-Status web module in a Rails application. ```ruby # config/initializers/sidekiq.rb require 'sidekiq' require 'sidekiq-status' require 'sidekiq/web' require 'sidekiq-status/web' # Must be after Sidekiq::Web is loaded # Configure middleware (as usual) Sidekiq.configure_client do |config| Sidekiq::Status.configure_client_middleware config end Sidekiq.configure_server do |config| Sidekiq::Status.configure_server_middleware config Sidekiq::Status.configure_client_middleware config end # Configure web interface Sidekiq::Status::Web.default_per_page = 50 Sidekiq::Status::Web.per_page_opts = [25, 50, 100, 250] # In Rails config/routes.rb mount Sidekiq::Web => '/sidekiq' # Statuses tab automatically available # Access at: http://localhost:3000/sidekiq/statuses ``` -------------------------------- ### Get All Method Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example of retrieving all data for a job. ```ruby all_data = Sidekiq::Status.get_all(job_id) puts all_data['status'] # => "working" puts all_data['at'] # => "42" puts all_data['total'] # => "100" puts all_data['custom_field'] # => "custom_value" (or nil) ``` -------------------------------- ### Docker Compose Setup - Start and Enter Environment Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Commands to start the development environment using Docker Compose and to enter the application container. ```bash # Start development environment docker compose -f .devcontainer/docker-compose.yml up -d # Enter the container docker compose -f .devcontainer/docker-compose.yml exec app bash ``` -------------------------------- ### Complete Middleware Setup Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/middleware.md A complete example of setting up both client and server middleware for Sidekiq::Status. ```ruby # config/sidekiq.rb (or config/initializers/sidekiq.rb in Rails) require 'sidekiq' require 'sidekiq-status' Sidekiq.configure_client do |config| config.redis = { url: ENV.fetch('REDIS_URL', 'redis://localhost:6379') } # Client middleware tracks jobs when they're enqueued Sidekiq::Status.configure_client_middleware( config, expiration: 30 * 60 # 30 minutes ) end Sidekiq.configure_server do |config| config.redis = { url: ENV.fetch('REDIS_URL', 'redis://localhost:6379') } # Server middleware tracks job execution and completion Sidekiq::Status.configure_server_middleware( config, expiration: 30 * 60 # 30 minutes ) # Server also needs client middleware for jobs enqueued from worker#perform Sidekiq::Status.configure_client_middleware( config, expiration: 30 * 60 ) end ``` -------------------------------- ### Manual Development Setup - Install Dependencies Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Instructions for manually installing Ruby and Redis, including version verification and service management for different operating systems. ```bash # Ruby 3.2+ required ruby --version # Verify version # Install Redis (macOS) brew install redis brew services start redis # Install Redis (Ubuntu/Debian) sudo apt-get install redis-server sudo systemctl start redis-server ``` -------------------------------- ### Get Method Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example of retrieving custom fields using the get method. ```ruby custom_value = Sidekiq::Status.get(job_id, :my_custom_field) processing_step = Sidekiq::Status.get(job_id, :step) ``` -------------------------------- ### Docker Compose Setup - Install Dependencies and Stop Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Commands to install dependencies within the Docker Compose environment and to stop the environment. ```bash # Install dependencies bundle install # Stop environment docker compose -f .devcontainer/docker-compose.yml down ``` -------------------------------- ### Client Middleware Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Example of configuring client middleware with a specific expiration time. ```ruby Sidekiq.configure_client do |config| Sidekiq::Status.configure_client_middleware( config, expiration: 30 * 60 # 30 minutes ) end ``` -------------------------------- ### Server Middleware Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Example of configuring server middleware with a specific expiration time. ```ruby Sidekiq.configure_server do |config| Sidekiq::Status.configure_server_middleware( config, expiration: 60 * 60 # 1 hour ) end ``` -------------------------------- ### Appraisal Workflow - Install All Dependencies Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Commands to install base dependencies and then generate and install version-specific Gemfiles using Appraisal. ```bash # Install base dependencies bundle install # Generate and install appraisal gemfiles bundle exec appraisal install ``` -------------------------------- ### Installation using gem install Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Install the sidekiq-status gem directly. ```bash gem install sidekiq-status ``` -------------------------------- ### At Method Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example of retrieving the current progress count. ```ruby current = Sidekiq::Status.at(job_id) puts "Processed: #{current} items" ``` -------------------------------- ### Example Redis Keys Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/types.md Provides examples of Redis keys generated by Sidekiq::Status. ```plaintext sidekiq:status:550e8400-e29b-41d4-a716-446655440000 sidekiq:status:abc123def456 sidekiq:status:my-custom-job-id-12345 ``` -------------------------------- ### VS Code Dev Containers Setup Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Steps to clone the repository, open it in VS Code, and reopen it within a development container for an automated setup. ```bash git clone https://github.com/kenaniah/sidekiq-status.git cd sidekiq-status code . # Open in VS Code ``` -------------------------------- ### Started At Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Retrieves the Unix timestamp when a job started executing. ```ruby def started_at(job_id) get(job_id, :started_at)&.to_i end ``` -------------------------------- ### ETA Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example demonstrating how to use the `eta` method to estimate the remaining time for a job. ```ruby remaining_seconds = Sidekiq::Status.eta(job_id) if remaining_seconds minutes = (remaining_seconds / 60).round puts "Estimated #{minutes} minutes remaining" end ``` -------------------------------- ### Example Middleware Chain Setup Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/middleware.md Demonstrates how to configure both client and server middleware for Sidekiq::Status, setting a default expiration of 30 minutes. It's important to configure the client middleware on the server as well for jobs enqueued from within other jobs. ```ruby # Client-side configuration Sidekiq.configure_client do |config| Sidekiq::Status.configure_client_middleware( config, expiration: 30 * 60 # 30 minutes default ) end # Server also needs client middleware for perform_async called from within jobs Sidekiq.configure_server do |config| Sidekiq::Status.configure_server_middleware( config, expiration: 30 * 60 ) Sidekiq::Status.configure_client_middleware( config, expiration: 30 * 60 ) end ``` -------------------------------- ### Example Configuration Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/middleware.md Example of how to configure both client and server middleware in Sidekiq. ```ruby require 'sidekiq' require 'sidekiq-status' Sidekiq.configure_server do |config| config.redis = { url: ENV.fetch('REDIS_URL', 'redis://localhost:6379') } # Server middleware tracks job execution and completion Sidekiq::Status.configure_server_middleware( config, expiration: 60 * 60 * 24 # 1 day ) # Server also needs client middleware for jobs enqueued from within workers Sidekiq::Status.configure_client_middleware( config, expiration: 60 * 60 * 24 ) end ``` -------------------------------- ### Configuration Quick Reference - Web Interface Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/INDEX.md Web interface configuration example. ```ruby # Web interface configuration Sidekiq::Status::Web.default_per_page = 50 Sidekiq::Status::Web.per_page_opts = [25, 50, 100] ``` -------------------------------- ### Client Middleware Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/middleware.md Example of how to configure the client middleware in Sidekiq. ```ruby require 'sidekiq' require 'sidekiq-status' Sidekiq.configure_client do |config| Sidekiq::Status.configure_client_middleware( config, expiration: 60 * 60 # 1 hour ) end Sidekiq.configure_server do |config| Sidekiq::Status.configure_server_middleware( config, expiration: 60 * 60 ) Sidekiq::Status.configure_client_middleware( config, expiration: 60 * 60 ) end ``` -------------------------------- ### Routes Configuration Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Example of how to mount Sidekiq::Web in routes, with production authentication. ```ruby if Rails.env.production? # Protect Sidekiq Web in production authenticate :user, lambda { |u| u.admin? } do mount Sidekiq::Web => '/sidekiq' end else mount Sidekiq::Web => '/sidekiq' end # Statuses UI is automatically available at /sidekiq/statuses ``` -------------------------------- ### Total Items Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example demonstrating how to use the `total` method to display job progress. ```ruby total_items = Sidekiq::Status.total(job_id) current = Sidekiq::Status.at(job_id) puts "Progress: #{current}/#{total_items}" ``` -------------------------------- ### Initializer Configuration Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Example of how to configure Sidekiq-Status middleware in an initializer file. ```ruby require 'sidekiq' require 'sidekiq-status' Sidekiq.configure_client do |config| config.redis = { url: ENV['REDIS_URL'] } Sidekiq::Status.configure_client_middleware( config, expiration: Rails.env.production? ? 60 * 60 : 30 * 60 ) end Sidekiq.configure_server do |config| config.redis = { url: ENV['REDIS_URL'] } config.server_middleware do |chain| chain.insert_before Sidekiq::ServerMiddleware, MyMiddleware end Sidekiq::Status.configure_server_middleware( config, expiration: Rails.env.production? ? 60 * 60 : 30 * 60 ) Sidekiq::Status.configure_client_middleware( config, expiration: Rails.env.production? ? 60 * 60 : 30 * 60 ) end if defined?(Sidekiq::Web) require 'sidekiq-status/web' Sidekiq::Status::Web.default_per_page = 50 end ``` -------------------------------- ### Status Method Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example of using the status method to check job status. ```ruby job_id = MyJob.perform_async status = Sidekiq::Status.status(job_id) if status == :working puts "Job is still processing" elsif status == :complete puts "Job finished successfully" end ``` -------------------------------- ### Configuration Quick Reference - Client Middleware Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/INDEX.md Client middleware configuration example. ```ruby # Client middleware configuration Sidekiq.configure_client do |config| Sidekiq::Status.configure_client_middleware( config, expiration: 30 * 60 # TTL in seconds ) end ``` -------------------------------- ### Per-Worker Configuration - Variable Expiration Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Example demonstrating how to set different expiration times for different worker classes. ```ruby class QuickTask include Sidekiq::Worker include Sidekiq::Status::Worker def expiration 5 * 60 # 5 minutes end def perform # Short-lived task end end class LongRunningTask include Sidekiq::Worker include Sidekiq::Status::Worker def expiration 24 * 60 * 60 # 1 day end def perform # Long-running task end end ``` -------------------------------- ### Complete Working Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/worker-module.md A comprehensive example of a Sidekiq worker using Sidekiq::Status, demonstrating initialization, progress tracking, error handling, and graceful shutdown. ```ruby class ComplexJob include Sidekiq::Worker include Sidekiq::Status::Worker # Override default expiration for this job def expiration @expiration ||= 3600 # 1 hour end def perform(user_id, file_path) begin # Store initial metadata store( user_id: user_id, file_path: file_path, phase: 'initialization', errors: 0 ) # Load data and set total data = load_file(file_path) total(data.length) # Process items data.each_with_index do |item, index| begin process_item(item) # Update progress at(index + 1, "Processing item #{index + 1} of #{data.length}") # Store incremental results store( current_item_id: item.id, items_processed: index + 1 ) # Raises Stopped if stop was requested rescue => e error_count = (retrieve(:errors) || '0').to_i store( errors: error_count + 1, last_error: e.message ) end end # Mark completion store(phase: 'complete') at(data.length, 'All items processed') rescue Stopped # Handle graceful stop store(phase: 'stopped_by_user') raise rescue Exception => e # Handle fatal errors store(phase: 'failed', error: e.message) raise end end private def load_file(path) # Implementation end def process_item(item) # Implementation end end # Usage and monitoring from outside job_id = ComplexJob.perform_async(user_id, file_path) ``` -------------------------------- ### Percentage Complete Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example demonstrating how to use the `pct_complete` method to display job completion percentage. ```ruby percent = Sidekiq::Status.pct_complete(job_id) puts "#{percent}% complete" ``` -------------------------------- ### Ended At Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example demonstrating how to use the `ended_at` method to calculate the job's duration. ```ruby end_time = Sidekiq::Status.ended_at(job_id) if end_time start = Sidekiq::Status.started_at(job_id) duration = end_time - start if start puts "Job duration: #{duration} seconds" if duration end ``` -------------------------------- ### Example Usage of 'at' Method Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/worker-module.md An example demonstrating how to use the `at` method within a Sidekiq worker to report progress and status messages. ```ruby class BatchProcessingJob include Sidekiq::Worker include Sidekiq::Status::Worker def perform(file_path) items = load_items(file_path) total(items.length) # Set total first items.each_with_index do |item, index| process_item(item) at(index + 1, "Processing item #{index + 1} of #{items.length}") # Will raise Stopped if stop was requested end end end # From outside job_id = BatchProcessingJob.perform_async('data.csv') # ... check progress ... if Sidekiq::Status.pct_complete(job_id) > 50 Sidekiq::Status.stop!(job_id) # Request graceful stop # Next .at() call will raise Stopped end ``` -------------------------------- ### Configuration Quick Reference - Server Middleware Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/INDEX.md Server middleware configuration example. ```ruby # Server middleware configuration Sidekiq.configure_server do |config| Sidekiq::Status.configure_server_middleware( config, expiration: 30 * 60 ) Sidekiq::Status.configure_client_middleware( config, expiration: 30 * 60 ) end ``` -------------------------------- ### Updated At Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example demonstrating how to use the `updated_at` method to calculate the age of the last status update. ```ruby last_update = Sidekiq::Status.updated_at(job_id) if last_update age_seconds = Time.now.to_i - last_update puts "Last updated #{age_seconds} seconds ago" end ``` -------------------------------- ### Installation using Gemfile Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Add the sidekiq-status gem to your application's Gemfile. ```ruby gem 'sidekiq-status' ``` -------------------------------- ### ActiveJob Support Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Example of how to include Sidekiq::Status::Worker in an ActiveJob base class. ```ruby # app/jobs/application_job.rb class ApplicationJob < ActiveJob::Base include Sidekiq::Status::Worker end # app/jobs/my_job.rb class MyJob < ApplicationJob def perform(*args) # your code goes here end end ``` -------------------------------- ### Enqueued At Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example demonstrating how to use the `enqueued_at` method to display the job's enqueue time. ```ruby enqueue_time = Sidekiq::Status.enqueued_at(job_id) if enqueue_time puts "Enqueued at: #{Time.at(enqueue_time)}" end ``` -------------------------------- ### Status Predicate Methods Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Example of using status predicate methods to check job status. ```ruby job_id = MyJob.perform_async(123, 456) if Sidekiq::Status.working?(job_id) puts "Job is running" end unless Sidekiq::Status.complete?(job_id) puts "Job not done yet" end ``` -------------------------------- ### Conversion Examples Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/types.md Provides examples of how to convert retrieved string values from Redis into other data types like Integer, Float, Boolean, JSON, and Timestamps. ```ruby # Integer conversion current = Sidekiq::Status.get(job_id, :at).to_i total = Sidekiq::Status.total(job_id) # Already converted to_i # Float conversion rate = Sidekiq::Status.get(job_id, :processing_rate).to_f # Boolean conversion (careful: "false" string is truthy) is_done = Sidekiq::Status.get(job_id, :is_done) == "true" # JSON conversion metadata = JSON.parse(Sidekiq::Status.get(job_id, :metadata) || '{}') # Timestamp conversion started = Sidekiq::Status.started_at(job_id) if started start_time = Time.at(started) end ``` -------------------------------- ### Sidekiq::Web not loaded Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Example of how Sidekiq::Web might be missing the sidekiq-status/web requirement. ```ruby # Missing in initializer # require 'sidekiq-status/web' ``` -------------------------------- ### Environment Variables for Redis Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Example of configuring Redis connection URL using environment variables. ```ruby Sidekiq.configure_client do |config| config.redis = { url: ENV.fetch('REDIS_URL', 'redis://localhost:6379') } end Sidekiq.configure_server do |config| config.redis = { url: ENV.fetch('REDIS_URL', 'redis://localhost:6379') } end ``` -------------------------------- ### Example Usage of 'total' Method Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/worker-module.md An example showing how to use the `total` method in a Sidekiq worker to define the total number of items before processing. ```ruby class DataImportJob include Sidekiq::Worker include Sidekiq::Status::Worker def perform(csv_file_path) data = CSV.read(csv_file_path) total(data.length) # Set total before processing data.each_with_index do |row, index| import_row(row) at(index + 1, "Imported row #{index + 1}") # Percentage automatically calculated: (index+1)/data.length * 100 end end end ``` -------------------------------- ### store_status Example Usage Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/storage-module.md Example showing how `store_status` is called by the ServerMiddleware to update a job's status and associated timestamps. ```ruby # Called by ServerMiddleware job_id = "xyz789" Sidekiq::Status.store_status(job_id, :working, 1800) # Stores: {status: "working", started_at: , updated_at: } ``` -------------------------------- ### Hashes Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/types.md Explains that `get_all` returns a hash where values are strings, and provides an example. ```ruby all_data = Sidekiq::Status.get_all(job_id) # {"status" => "working", ...} ``` -------------------------------- ### Progress and Completion Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Examples of retrieving progress information for a job. ```ruby # Get progress information Sidekiq::Status.at(job_id) # Current progress (e.g., 42) Sidekiq::Status.total(job_id) # Total items to process (e.g., 100) Sidekiq::Status.pct_complete(job_id) # Percentage complete (e.g., 42) Sidekiq::Status.message(job_id) # Current status message ``` -------------------------------- ### Testing Configuration (Inline Mode) Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Example of requiring inline mode for testing to skip Redis. ```ruby # spec/spec_helper.rb or test/test_helper.rb require 'sidekiq/testing/inline' require 'sidekiq-status/testing/inline' # Jobs will run synchronously in tests # Status will always return :complete if job succeeds ``` -------------------------------- ### Set up sidekiq-status Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/INDEX.md Configuration example for setting up sidekiq-status middleware for both client and server, including setting an expiration time. ```ruby require 'sidekiq' require 'sidekiq-status' Sidekiq.configure_client do |config| Sidekiq::Status.configure_client_middleware( config, expiration: 30 * 60 ) end Sidekiq.configure_server do |config| Sidekiq::Status.configure_server_middleware( config, expiration: 30 * 60 ) Sidekiq::Status.configure_client_middleware( config, expiration: 30 * 60 ) end ``` -------------------------------- ### Middleware not configured Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Example of forgetting to configure the server middleware for sidekiq-status. ```ruby # Forgot to configure middleware Sidekiq.configure_server do |config| # Missing: Sidekiq::Status.configure_server_middleware(config) end ``` -------------------------------- ### store_for_id Example Usage Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/storage-module.md Example demonstrating how to use the `store_for_id` method from custom code to update job status fields and set expiration. ```ruby # From custom code (not from within a worker) job_id = "abc123" Sidekiq::Status.redis_adapter do |conn| storage = Sidekiq::Status::Storage.new storage.extend Sidekiq::Status::Storage storage.store_for_id( job_id, { progress: 50, status_message: "Halfway done" }, expiration = 1800 # 30 minutes ) end ``` -------------------------------- ### Handling a Failed Job Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Example of how to monitor a job's status from outside and react when it becomes :failed. ```ruby job_id = MyJob.perform_async(data) loop do status = Sidekiq::Status.status(job_id) case status when :failed puts "Job failed" all_data = Sidekiq::Status.get_all(job_id) # all_data may contain exception details if the worker stored them break when :complete puts "Job succeeded" break else sleep 1 end end ``` -------------------------------- ### Timing Information Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Examples of retrieving timing data for a job. ```ruby # Get timing data (returns Unix timestamps as integers, or nil) Sidekiq::Status.enqueued_at(job_id) # When job was enqueued Sidekiq::Status.started_at(job_id) # When job started processing Sidekiq::Status.updated_at(job_id) # Last update time Sidekiq::Status.ended_at(job_id) # When job finished # Estimated time to completion (in seconds, or nil) Sidekiq::Status.eta(job_id) # Based on current progress rate ``` -------------------------------- ### Use the web interface Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/INDEX.md Example of how to mount the sidekiq-status web interface routes within a Rails application. ```ruby require 'sidekiq/web' require 'sidekiq-status/web' # In config/routes.rb (Rails) mount Sidekiq::Web => '/sidekiq' ``` -------------------------------- ### Testing Best Practices - Running Tests in CI/CD Style Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Commands for a typical CI/CD workflow, including installing all dependencies, running the full test suite, and checking for dependency issues. ```bash # Full test suite (like GitHub Actions) bundle exec appraisal install bundle exec appraisal rake spec # Check for dependency issues bundle exec bundle-audit check --update ``` -------------------------------- ### Get All Method Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Retrieve all fields and values for a job's status hash. ```ruby def get_all(job_id) read_hash_for_id(job_id) end ``` -------------------------------- ### Reducing Expiration Time Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Example of reducing the expiration time for high-throughput, short-lived jobs. ```ruby Sidekiq::Status.configure_server_middleware( config, expiration: 5 * 60 # 5 minutes ) ``` -------------------------------- ### Common Development Tasks Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md A collection of common development tasks, including starting the Redis server, running Sidekiq workers, starting an IRB session, and generating test coverage reports. ```bash # Start Redis for testing redis-server # Run Sidekiq worker with test environment bundle exec sidekiq -r ./spec/environment.rb # Start IRB with sidekiq-status loaded bundle exec irb -r ./lib/sidekiq-status # Generate test coverage report COVERAGE=true bundle exec rake spec open coverage/index.html ``` -------------------------------- ### Increasing Expiration Time Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Example of increasing the expiration time for long-running jobs. ```ruby Sidekiq::Status.configure_server_middleware( config, expiration: 24 * 60 * 60 # 1 day ) ``` -------------------------------- ### ApplicationJob Configuration Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Example of including Sidekiq::Status::Worker in an ActiveJob base class and setting a default expiration. ```ruby class ApplicationJob < ActiveJob::Base include Sidekiq::Status::Worker # Optional: set default expiration for all jobs def expiration @expiration ||= 30 * 60 end end ``` -------------------------------- ### Progress Tracking Not Working: .total() not called before .at() Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Example where progress tracking fails because .total() was not called prior to .at(). ```ruby def perform at(10, "Processing") # No total() called first # pct_complete defaults to 100: (10/100)*100 = 10% end ``` -------------------------------- ### Handling an Interrupted Job Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Example of how to check if a job's status is :interrupted and how to restart it if necessary. ```ruby # From outside job_id = MyJob.perform_async(data) # Later... status = Sidekiq::Status.status(job_id) if status == :interrupted puts "Job was interrupted by a system signal" # Decide whether to retry manually or discard new_job_id = MyJob.perform_async(data) # Restart end ``` -------------------------------- ### Retrieving Job Data Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/INDEX.md Example of retrieving batch configuration and processed IDs for a job. ```ruby batch_config = JSON.parse(Sidekiq::Status.get(job_id, :batch_config)) processed_ids = JSON.parse(Sidekiq::Status.get(job_id, :processed_ids)) ``` -------------------------------- ### Handling :stopped Status Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Example of a worker that includes Sidekiq::Status::Worker and how to request a stop. ```ruby class MyJob include Sidekiq::Worker include Sidekiq::Status::Worker def perform(items) total(items.length) items.each_with_index do |item, idx| process(item) at(idx + 1, "Processing item #{idx + 1}") # Raises Worker::Stopped if stop was requested end end end # From outside job_id = MyJob.perform_async(large_list) # Request stop Sidekiq::Status.stop!(job_id) # Wait for status to change sleep 2 status = Sidekiq::Status.status(job_id) puts status # => :stopped (once the next .at() is called) ``` -------------------------------- ### Example Status Flow Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/middleware.md Illustrates the sequence of status changes for a job processed by the middleware. ```text Job submitted (ClientMiddleware runs) ↓ status = :queued enqueued_at = Job starts processing (ServerMiddleware runs) ↓ status = :working started_at = Job completes successfully ↓ status = :complete ended_at = --OR-- Job raises exception, will retry ↓ status = :retrying (no ended_at) Job raises exception, will not retry ↓ status = :failed ended_at = --OR-- Sidekiq::Status.stop!(job_id) called Next .at() call raises Worker::Stopped ↓ status = :stopped ended_at = ``` -------------------------------- ### ClientMiddleware Configuration Example Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/middleware.md Configures the client-side middleware for Sidekiq::Status, setting a default expiration of 1 hour for job status records. ```ruby Sidekiq.configure_client do |config| Sidekiq::Status.configure_client_middleware( config, expiration: 60 * 60 # 1 hour ) end ``` -------------------------------- ### Handling a Retrying Job Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Example of how to check the status of a job that is in the :retrying state and access its retry count. ```ruby class MyJob include Sidekiq::Worker include Sidekiq::Status::Worker sidekiq_options retry: 3 def perform(data) # Will retry up to 3 times on exception process_with_retries(data) end end # From outside job_id = MyJob.perform_async(flaky_data) # Check status status = Sidekiq::Status.status(job_id) if status == :retrying all_data = Sidekiq::Status.get_all(job_id) retry_count = all_data['retry_count'] || 0 puts "Job is retrying (attempt #{retry_count + 1})" end ``` -------------------------------- ### Requesting and Checking for a Stopped Job Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Example of how to request a graceful stop for a job and then check its status to confirm it was stopped. ```ruby job_id = MyJob.perform_async(100) # Request graceful stop Sidekiq::Status.stop!(job_id) # Check status sleep 1 status = Sidekiq::Status.status(job_id) if status == :stopped puts "Job was stopped" end ``` -------------------------------- ### Version Compatibility - Update gemfile constraints Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Example of updating Gemfile constraints to ensure compatible versions of Sidekiq and sidekiq-status are used. ```ruby gem 'sidekiq', '~> 8.0' # Use compatible version gem 'sidekiq-status' # Latest version ``` -------------------------------- ### Appraisal Workflow - Updating Dependencies Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Commands to regenerate Gemfiles after dependency changes and to install new dependencies or update specific versions. ```bash # Regenerate gemfiles after dependency changes: bundle exec appraisal generate # Install new dependencies bundle exec appraisal install # Update specific version (e.g., Sidekiq 7.x): bundle exec appraisal sidekiq-7.x bundle update ``` -------------------------------- ### Per-Job Expiration Tuning Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Example of overriding expiration for specific job types by defining an `expiration` method in the worker class. ```ruby class QuickTask include Sidekiq::Worker include Sidekiq::Status::Worker def expiration 2 * 60 # 2 minutes for quick tasks end def perform # Fast execution end end class ReportJob include Sidekiq::Worker include Sidekiq::Status::Worker def expiration 24 * 60 * 60 # 1 day for long reports end def perform # Long-running report generation end end ``` -------------------------------- ### Retrieve Job ID Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Example of how to get a job ID for status tracking. ```ruby job_id = MyJob.perform_async(*args) ``` -------------------------------- ### Tracking Progress and Storing Data Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Example of how to use Sidekiq::Status::Worker to track progress and store custom data within a job. ```ruby class MyJob include Sidekiq::Worker include Sidekiq::Status::Worker # Required for progress tracking def perform(*args) # Set total number of items to process total 100 # Update progress throughout your job (1..100).each do |i| # Do some work here... sleep 0.1 # Update progress with optional message at i, "Processing item #{i}" # This automatically calculates percentage: i/100 * 100 end end end ``` -------------------------------- ### Web Interface Configuration - per_page_opts Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Set the available pagination options in the dropdown for the web UI. ```ruby Sidekiq::Status::Web.per_page_opts = [10, 25, 50, 100, 250] ``` -------------------------------- ### At Method Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Get the current progress count for a job. ```ruby def at(job_id) get(job_id, :at).to_i end ``` -------------------------------- ### Jobs have expired Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Example demonstrating how a job can expire and be removed from Redis. ```ruby job_id = MyJob.perform_async sleep 31 * 60 # Default expiration is 30 minutes # Job is now gone from Redis and won't appear in web UI ``` -------------------------------- ### Custom Data Retrieval Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Examples of retrieving custom data associated with a job. ```ruby # Get specific custom field Sidekiq::Status.get(job_id, :field_name) # Returns string or nil # Get all job data as hash data = Sidekiq::Status.get_all(job_id) # Returns: { # "status" => "working", # "updated_at" => "1640995200", # "enqueued_at" => "1640995100", # "started_at" => "1640995150", # "at" => "42", # "total" => "100", # "pct_complete" => "42", # "message" => "Processing...", # "custom_field" => "custom_value" # } ``` -------------------------------- ### Scanning for Status Keys Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/types.md Demonstrates how to use `Sidekiq::Status.redis_adapter` to scan for all status keys in Redis and extract job IDs. ```ruby Sidekiq::Status.redis_adapter do |conn| # Scan for all status keys conn.scan(match: 'sidekiq:status:*', count: 100).map do |key| key.split(':').last # Extract job_id end end ``` -------------------------------- ### Client Middleware Configuration Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Configure job status tracking when jobs are enqueued. ```ruby Sidekiq.configure_client do |config| Sidekiq::Status.configure_client_middleware(config, options_hash) end ``` -------------------------------- ### Server Middleware Configuration Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Configure job status tracking during job execution. ```ruby Sidekiq.configure_server do |config| Sidekiq::Status.configure_server_middleware(config, options_hash) end ``` -------------------------------- ### Good Practice for Storing Data Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/types.md Illustrates the recommended way to store custom data using descriptive names and warns against using reserved field names. ```ruby # DO: Use descriptive custom names store( processing_phase: 'validation', records_processed: 1000, error_count: 5 ) # DON'T: Use reserved names store( status: 'custom_value', # Will be overwritten! enqueued_at: 999 # Will be overwritten! ) ``` -------------------------------- ### Progress Tracking Not Working: Progress values not numeric Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Demonstrates incorrect usage of non-numeric values for .total() and .at(). ```ruby def perform total("100") # String instead of integer at("10") # String instead of integer end ``` -------------------------------- ### schedule_batch Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/storage-module.md Get a batch of scheduled jobs from Redis' sorted set. ```ruby def schedule_batch(options) Sidekiq::Status.wrap_redis_connection(options[:conn]).schedule_batch("schedule", options.merge(limit: BATCH_LIMIT)) end ``` -------------------------------- ### retry_attempt_number Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/middleware.md Get the current retry attempt number. ```ruby def retry_attempt_number(msg) if msg['retry_count'] msg['retry_count'] + 1 else 0 end end ``` -------------------------------- ### Get Method Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Retrieve a single field from a job's status hash. ```ruby def get(job_id, field) read_field_for_id(job_id, field) end ``` -------------------------------- ### Web Interface Configuration - default_per_page Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/configuration.md Set the number of jobs shown per page by default in the web UI. ```ruby Sidekiq::Status::Web.default_per_page = 50 ``` -------------------------------- ### Basic Status Queries Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Examples of checking the current status of a job using boolean methods. ```ruby # Get current status as symbol status = Sidekiq::Status.status(job_id) # Returns: :queued, :working, :retrying, :complete, :failed, :stopped, :interrupted, or nil after expiry # Check specific status with boolean methods Sidekiq::Status.queued?(job_id) # true if job is queued Sidekiq::Status.working?(job_id) # true if job is currently running Sidekiq::Status.retrying?(job_id) # true if job is retrying after failure Sidekiq::Status.complete?(job_id) # true if job completed successfully Sidekiq::Status.failed?(job_id) # true if job failed permanently Sidekiq::Status.interrupted?(job_id) # true if job was interrupted Sidekiq::Status.stopped?(job_id) # true if job was manually stopped ``` -------------------------------- ### Appraisal Workflow - Running Tests Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Commands to run the test suite against all supported Sidekiq versions or specific versions. ```bash # Test all Sidekiq versions: bundle exec appraisal rake spec # Test specific version (e.g., Sidekiq 7.0.x): bundle exec appraisal sidekiq-7.0 rake spec # Test against Sidekiq 8.x: bundle exec appraisal sidekiq-8.x rake spec # Quick test with current Gemfile: bundle exec rake spec # or rake spec ``` -------------------------------- ### Configuration Options for Web Interface Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Customizes pagination settings for the web interface. ```ruby # Configure pagination (default: 25 per page) Sidekiq::Status::Web.default_per_page = 50 Sidekiq::Status::Web.per_page_opts = [25, 50, 100, 200] # The web interface will show these options in a dropdown ``` -------------------------------- ### Docker Development Shortcuts Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Convenient Docker commands for running tests, accessing an interactive shell, or testing specific Sidekiq versions within a container. ```bash # Quick test run using Docker docker compose run --rm sidekiq-status bundle exec rake spec # Interactive shell in container docker compose run --rm sidekiq-status bash # Test specific Sidekiq version in Docker docker compose run --rm sidekiq-status bundle exec appraisal sidekiq-8.x rake spec ``` -------------------------------- ### Get Job Message Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/sidekiq-status-module.md Retrieves the current status message for a given job ID. ```ruby def message(job_id) get(job_id, :message) end ``` -------------------------------- ### Progress Tracking Not Working: Percentage always 100 Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Illustrates a scenario where percentage might appear stuck at 100%, explaining it's often correct behavior. ```ruby def perform total(100) (1..10).each do |i| at(i) # When i=1: (1/100)*100 = 1% # This is correct! end end ``` -------------------------------- ### Including the Web Module Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Ensures the necessary require statements are present to enable the web interface. ```ruby require 'sidekiq/web' require 'sidekiq-status/web' # Must be after sidekiq/web ``` -------------------------------- ### per_page_opts Method Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/web-module.md Getter for current pagination options. ```ruby def self.per_page_opts @per_page_opts || DEFAULT_PER_PAGE_OPTS end ``` -------------------------------- ### retry_attempts_from Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/middleware.md Get the configured maximum retry attempts from the job message. ```ruby def retry_attempts_from(msg_retry, default) msg_retry.is_a?(Integer) ? msg_retry : default end ``` -------------------------------- ### display_args Helper Method Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/middleware.md Formats job arguments for display in the web interface. It attempts to use Sidekiq's `display_args` method and falls back to JSON encoding of the raw arguments for older Sidekiq versions. Handles potential exceptions during formatting. ```ruby def display_args(msg, queue) job = JOB_CLASS.new(msg, queue) return job.display_args.to_a.empty? ? "{}" : job.display_args.to_json rescue Exception => e # For Sidekiq ~> 2.7 return msg['args'].to_a.empty? ? nil : msg['args'].to_json end ``` -------------------------------- ### Configuration Helper Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/middleware.md Helper method to configure the server middleware. ```ruby def self.configure_server_middleware(sidekiq_config, server_middleware_options = {}) sidekiq_config.server_middleware do |chain| chain.add Sidekiq::Status::ServerMiddleware, server_middleware_options end end ``` -------------------------------- ### Common Patterns - Monitoring Job Progress (Polling) Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/INDEX.md Example of polling for job progress using sidekiq-status methods. ```ruby job_id = MyJob.perform_async(data) # Simple polling loop while !Sidekiq::Status.complete?(job_id) && !Sidekiq::Status.failed?(job_id) pct = Sidekiq::Status.pct_complete(job_id) msg = Sidekiq::Status.message(job_id) puts "Progress: #{pct}% - #{msg}" sleep 1 end status = Sidekiq::Status.status(job_id) puts "Final status: #{status}" ``` -------------------------------- ### Configuration Helper Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/middleware.md A helper method to configure the client middleware for Sidekiq. ```ruby def self.configure_client_middleware(sidekiq_config, client_middleware_options = {}) sidekiq_config.client_middleware do |chain| chain.add Sidekiq::Status::ClientMiddleware, client_middleware_options end end ``` -------------------------------- ### Store custom fields Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/INDEX.md Example of storing custom data in a worker, highlighting allowed and disallowed field names. ```ruby # Good: custom field names store( import_batch: 1, processed_count: 500, error_count: 2 ) # Bad: reserved names will be overwritten store(status: "custom") # Don't do this ``` -------------------------------- ### per_page_opts= Method Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/api-reference/web-module.md Setter for available pagination options. ```ruby def self.per_page_opts=(arr) @per_page_opts = arr end ``` -------------------------------- ### Testing Issues - Mock status calls in tests Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Example of using RSpec to mock `Sidekiq::Status` calls for testing purposes. ```ruby # RSpec example allow(Sidekiq::Status).to receive(:status).and_return(:complete) allow(Sidekiq::Status).to receive(:pct_complete).and_return(100) ``` -------------------------------- ### Version Compatibility - Check version compatibility Source: https://github.com/kenaniah/sidekiq-status/blob/main/README.md Shows how to check the current Ruby and Sidekiq versions against the requirements for sidekiq-status 4.x. ```ruby # sidekiq-status 4.x requirements: # Ruby 3.2+ # Sidekiq 7.0+ puts "Ruby: #{RUBY_VERSION}" puts "Sidekiq: #{Sidekiq::VERSION}" ``` -------------------------------- ### Storing Error Information in a Worker Source: https://github.com/kenaniah/sidekiq-status/blob/main/_autodocs/errors.md Example of how a worker can store error details before re-raising an exception, to be accessible via Sidekiq::Status. ```ruby class MyJob include Sidekiq::Worker include Sidekiq::Status::Worker def perform(data) begin process(data) rescue => e # Store error info before re-raising store(error: e.message, error_class: e.class.name) raise end end end # From outside job_id = MyJob.perform_async(bad_data) # ... later ... if Sidekiq::Status.failed?(job_id) error = Sidekiq::Status.get(job_id, :error) puts "Error: #{error}" end ```