Class: Sidekiq::Tasks::Web::Extension

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/tasks/web/extension.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sidekiq/tasks/web/extension.rb', line 14

def self.registered(app)
  app.helpers(Sidekiq::Tasks::Web::Helpers::ApplicationHelper)
  app.helpers(Sidekiq::Tasks::Web::Helpers::TagHelper)
  app.helpers(Sidekiq::Tasks::Web::Helpers::TaskHelper)
  app.helpers(Sidekiq::Tasks::Web::Helpers::PaginationHelper)
  app.helpers(Sidekiq::Tasks::Web::Helpers::SortHelper)

  app.get "/tasks" do
    authorize!

    @search = Sidekiq::Tasks::Web::Search.new(fetch_params(:count, :page, :filter, :sort, :direction))

    erb(read_view(:tasks), locals: {search: @search})
  end

  app.get "/tasks/:name" do
    authorize!

    @task = find_task!(env["rack.route_params"][:name])

    history = @task.history
    per_page = 10
    page = [fetch_param("page").to_i, 1].max
    total_pages = [(history.size.to_f / per_page).ceil, 1].max
    history_entries = history.slice((page - 1) * per_page, per_page) || []

    erb(
      read_view(:task),
      locals: {
        task: @task,
        history_entries: history_entries,
        history_page: page,
        history_total_pages: total_pages,
        history_total_count: history.size,
      }
    )
  rescue Sidekiq::Tasks::NotFoundError
    throw :halt, [404, {Rack::CONTENT_TYPE => "text/plain"}, ["Task not found"]]
  end

  app.post "/tasks/:name/enqueue" do
    authorize!

    if fetch_param("env_confirmation") != current_env
      throw :halt, [400, {Rack::CONTENT_TYPE => "text/plain"}, ["Invalid confirm"]]
    end

    task = find_task!(env["rack.route_params"][:name])
    args = Sidekiq::Tasks::Web::Params.new(task, fetch_param("args")).permit!

    current_user = Sidekiq::Tasks.config.current_user&.call(env)
    task.enqueue(args, user: current_user)

    redirect(task_url(root_path, task))
  rescue Sidekiq::Tasks::ArgumentError => e
    throw :halt, [400, {Rack::CONTENT_TYPE => "text/plain"}, [e.message]]
  rescue Sidekiq::Tasks::NotFoundError
    throw :halt, [404, {Rack::CONTENT_TYPE => "text/plain"}, ["Task not found"]]
  end
end