Class: Sidekiq::Web::Config

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sidekiq/web/config.rb

Overview

Configure the Sidekiq::Web instance in this process:

require "sidekiq/web"
Sidekiq::Web.configure do |config|
  config.register(MyExtension, name: "myext", tab: "TabName", index: "tabpage/")
end

This should go in your ‘config/routes.rb` or similar. It does not belong in your initializer since Web should not be loaded in some processes (like an actual Sidekiq process). See `examples/webui-ext` for a sample web extension.

Constant Summary collapse

OPTIONS =
{
  # By default we support direct uploads to p.f.c since the UI is a JS SPA
  # and very difficult for us to vendor or provide ourselves. If you are worried
  # about data security and wish to self-host, you can change these URLs.
  profile_view_url: "https://profiler.firefox.com/public/%s",
  profile_store_url: "https://api.profiler.firefox.com/compressed-store",
  # Will be false in Sidekiq 9.0.
  # CSRF is unnecessary if you are using SameSite=(Strict|Lax) cookies.
  csrf: true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



58
59
60
61
62
63
64
65
# File 'lib/sidekiq/web/config.rb', line 58

def initialize
  @options = OPTIONS.dup
  @locales = LOCALES
  @views = VIEWS
  @tabs = DEFAULT_TABS.dup
  @middlewares = []
  @custom_job_info_rows = []
end

Instance Attribute Details

#app_urlObject

Adds the “Back to App” link in the header



56
57
58
# File 'lib/sidekiq/web/config.rb', line 56

def app_url
  @app_url
end

#custom_job_info_rowsObject

Allows users to add custom rows to all of the Job tables, e.g. Retries, Dead, Scheduled, with custom links to other systems, see _job_info.erb and test in web_test.rb

Sidekiq::Web.configure do |cfg|
  cfg.custom_job_info_rows << JobLogLink.new
end

class JobLogLink
  def add_pair(job)
    yield "External Logs", "<a href='https://example.com/logs/#{job.jid}'>Logs for #{job.jid}</a>"
  end
end


48
49
50
# File 'lib/sidekiq/web/config.rb', line 48

def custom_job_info_rows
  @custom_job_info_rows
end

#localesObject (readonly)

Returns the value of attribute locales.



51
52
53
# File 'lib/sidekiq/web/config.rb', line 51

def locales
  @locales
end

#middlewaresObject (readonly)

Returns the value of attribute middlewares.



53
54
55
# File 'lib/sidekiq/web/config.rb', line 53

def middlewares
  @middlewares
end

#tabsObject (readonly)

Returns the value of attribute tabs.



50
51
52
# File 'lib/sidekiq/web/config.rb', line 50

def tabs
  @tabs
end

#viewsObject (readonly)

Returns the value of attribute views.



52
53
54
# File 'lib/sidekiq/web/config.rb', line 52

def views
  @views
end

Instance Method Details

#register_extension(extclass, name:, tab:, index:, root_dir: nil, cache_for: 86400, asset_paths: nil) {|_self| ... } ⇒ Object Also known as: register

Register a class as a Sidekiq Web UI extension. The class should provide one or more tabs which map to an index route. Options:

Web extensions will have a root ‘web/` directory with `locales/`, `assets/` and `views/` subdirectories.

Parameters:

  • extclass (Class)

    Class which contains the HTTP actions, required

  • name (String)

    the name of the extension, used to namespace assets

  • tab (String | Array)

    labels(s) of the UI tabs

  • index (String | Array)

    index route(s) for each tab

  • root_dir (String) (defaults to: nil)

    directory location to find assets, locales and views, typically ‘web/` within the gemfile

  • asset_paths (Array) (defaults to: nil)

    one or more directories under Top Level Namespace/assets/name to be publicly served, e.g. [“js”, “css”, “img”]

  • cache_for (Integer) (defaults to: 86400)

    amount of time to cache assets, default one day

Yields:

  • (_self)

Yield Parameters:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sidekiq/web/config.rb', line 86

def register_extension(extclass, name:, tab:, index:, root_dir: nil, cache_for: 86400, asset_paths: nil)
  tab = Array(tab)
  index = Array(index)
  tab.zip(index).each do |tab, index|
    tabs[tab] = index
  end
  if root_dir
    locdir = File.join(root_dir, "locales")
    locales << locdir if File.directory?(locdir)

    if asset_paths && name
      # if you have {root}/assets/{name}/js/scripts.js
      # and {root}/assets/{name}/css/styles.css
      # you would pass in:
      #   asset_paths: ["js", "css"]
      # See script_tag and style_tag in web/helpers.rb
      assdir = File.join(root_dir, "assets")
      assurls = Array(asset_paths).map { |x| "/#{name}/#{x}" }
      assetprops = {
        urls: assurls,
        root: assdir,
        cascade: true
      }
      assetprops[:header_rules] = [[:all, {"cache-control" => "private, max-age=#{cache_for.to_i}"}]] if cache_for
      middlewares << [[Rack::Static, assetprops], nil]
    end
  end

  yield self if block_given?
  extclass.registered(Web::Application)
end

#use(*args, &block) ⇒ Object



69
70
71
# File 'lib/sidekiq/web/config.rb', line 69

def use(*args, &block)
  middlewares << [args, block]
end