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



13
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
# File 'lib/sidekiq/tasks/web/extension.rb', line 13

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.get "/tasks" do
    authorize!

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

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

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

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

    erb(read_view(:task), locals: {task: @task})
  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!

    task.enqueue(args)

    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