### Install Sidekiq::Tasks manually Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md This snippet demonstrates how to install the sidekiq-tasks gem directly using the gem command, typically when not using Bundler. ```bash gem install sidekiq-tasks ``` -------------------------------- ### Install Sidekiq::Tasks via Bundler Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md This snippet shows how to add the sidekiq-tasks gem to your project's Gemfile using Bundler. ```bash bundle add sidekiq-tasks ``` -------------------------------- ### Configure Sidekiq Enqueuing Options Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md Sets global Sidekiq options for task enqueuing. By default, tasks are enqueued with `queue: "default"` and `retry: false`. This example changes the default to `queue: "low"` and `retry: false`. ```ruby Sidekiq::Tasks.configure do |config| # Default options are {queue: "default", retry: false} config.sidekiq_options = {queue: "low", retry: false} end ``` -------------------------------- ### Enable a Rake Task with a Magic Comment Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md This Ruby code example shows how to enable a specific Rake task (`:my_task`) for use with Sidekiq::Tasks by adding a `# sidekiq-tasks:enable` comment above its definition. This ensures the task is discoverable and manageable through the Sidekiq web interface. ```ruby # sidekiq-tasks:enable task :my_task do puts "my_task" end ``` -------------------------------- ### Configure Authorization for Web Interface Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md Sets up authorization for the Sidekiq-Tasks web interface. This example uses a lambda function to check if the current user is a super admin via `env["warden"].user(:admin_user).super_admin?`. ```ruby Sidekiq::Tasks.configure do |config| config.authorization = ->(env) do env["warden"].user(:admin_user).super_admin? end end ``` -------------------------------- ### Disable a Rake Task with a Magic Comment Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md This Ruby code example shows how to explicitly disable a Rake task (`:my_task`) from being managed by Sidekiq::Tasks. By adding the `# sidekiq-tasks:disable` comment, the task will not appear in the Sidekiq web interface, adhering to the principle of least privilege. ```ruby # sidekiq-tasks:disable task :my_task do puts "my_task" end ``` -------------------------------- ### Create Custom ScriptStrategy Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md Defines a custom strategy `ScriptStrategy` that inherits from `Sidekiq::Tasks::Strategies::Base`. It loads tasks from a YAML file, builds metadata, and executes them as system commands. Caution: direct system command execution can be a security risk. ```ruby class ScriptStrategy < Sidekiq::Tasks::Strategies::Base def load_tasks YAML.load_file("config/scripts.yml") end def build_task_metadata(task) Sidekiq::Tasks::TaskMetadata.new( name: task["name"], desc: task["desc"], args: task["args"], file: task["file"] ) end def execute_task(name, args = {}) system "ruby #{name} #{args.values.join(" ")}" end end Sidekiq::Tasks.configure do |config| config.strategies = [ScriptStrategy.new] end ``` -------------------------------- ### Configure Default Strategies with RakeTask Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md Configures Sidekiq-Tasks to use the RakeTask strategy, enabling all tasks in the lib folder and disabling specific ones via comments. This is the primary method for setting up task loading. ```ruby Sidekiq::Tasks.configure do |config| config.strategies = [ Sidekiq::Tasks::Strategies::RakeTask.new( rules: [ Sidekiq::Tasks::Strategies::Rules::TaskFromLib.new, Sidekiq::Tasks::Strategies::Rules::DisableWithComment.new, ] ) ] end ``` -------------------------------- ### Configure Routes for Sidekiq::Tasks Web Interface Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md This Ruby code snippet illustrates how to integrate the Sidekiq::Tasks web interface into your Rails application by requiring the necessary files in `config/routes.rb`. This makes the task management interface accessible at `/sidekiq/tasks`. ```ruby require "sidekiq/web" require "sidekiq/tasks/web" ``` -------------------------------- ### Define a Rake Task with Arguments for Sidekiq::Tasks Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md This Ruby code defines a Rake task (`:my_task`) that accepts two arguments, `arg1` and `arg2`. Sidekiq::Tasks automatically detects these arguments and provides a form in the web interface to input their values, which are passed as strings to the task. ```ruby task :my_task, [:arg1, :arg2] => :environment do |_, args| puts "arg1: #{args[:arg1]}, arg2: #{args[:arg2]}" end ``` -------------------------------- ### Enable All Rake Tasks within a Namespace Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md This Ruby code demonstrates how to enable all Rake tasks defined within a specific namespace (`:my_namespace`) for Sidekiq::Tasks. By placing the `# sidekiq-tasks:enable` comment before the namespace block, all tasks within it become available in the Sidekiq web interface. ```ruby # sidekiq-tasks:enable namespace :my_namespace do task :my_task do puts "my_task" end task :another_task do puts "another_task" end end ``` -------------------------------- ### Implement FileMatchesFoo Rule Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md Creates a custom rule `FileMatchesFoo` that inherits from `Sidekiq::Tasks::Strategies::Rules::Base`. This rule filters tasks, only allowing those whose filenames match the pattern 'foo'. ```ruby class FileMatchesFoo < Sidekiq::Tasks::Strategies::Rules::Base def respected?(task) task["file"].match?(/foo/) end end Sidekiq::Tasks.configure do |config| config.strategies = [ScriptStrategy.new(rules: [FileMatchesFoo.new])] end ``` -------------------------------- ### Override Enqueue Task Method Source: https://github.com/victorauthiat/sidekiq-tasks/blob/master/README.md Demonstrates overriding the `enqueue_task` method within a custom strategy to use a specific Sidekiq job (`ScriptJob`) for enqueuing. It's crucial that this method returns the JID of the executed Sidekiq job. ```ruby class ScriptStrategy < Sidekiq::Tasks::Strategies::Base def enqueue_task(name, params = {}) ScriptJob.perform_async(name, params) end end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.