Module: Sidekiq::AssuredJobs::Web

Defined in:
lib/sidekiq/assured_jobs/web.rb

Defined Under Namespace

Classes: OrphanedJobsManager

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



8
9
10
11
12
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
# File 'lib/sidekiq/assured_jobs/web.rb', line 8

def self.registered(app)
  # Add helper methods to the app
  app.helpers do
    def relative_time(time)
      return 'Unknown' unless time

      diff = Time.now - time
      case diff
      when 0..59
        "#{diff.to_i} seconds ago"
      when 60..3599
        "#{(diff / 60).to_i} minutes ago"
      when 3600..86399
        "#{(diff / 3600).to_i} hours ago"
      else
        "#{(diff / 86400).to_i} days ago"
      end
    end

    def distance_of_time_in_words(seconds)
      return 'Unknown' unless seconds

      case seconds
      when 0..59
        "#{seconds.to_i}s"
      when 60..3599
        "#{(seconds / 60).to_i}m"
      when 3600..86399
        "#{(seconds / 3600).to_i}h"
      else
        "#{(seconds / 86400).to_i}d"
      end
    end

    def truncate(text, length)
      return text unless text
      text.length > length ? "#{text[0, length]}..." : text
    end

    def csrf_tag
      # Sidekiq web uses Rack::Protection, get the token
      "<input type='hidden' name='authenticity_token' value='#{env['rack.session'][:csrf]}' />"
    end

    def root_path
      "#{env['SCRIPT_NAME']}/"
    end
  end

  app.get '/orphaned-jobs' do
    @orphaned_jobs = OrphanedJobsManager.get_orphaned_jobs
    @instances = OrphanedJobsManager.get_instances_info
    @total_count = @orphaned_jobs.size
    erb File.read(File.join(File.dirname(__FILE__), '../../../web/views/orphaned_jobs.erb'))
  end

  app.get '/orphaned-jobs/:jid' do
    jid = params[:jid]
    @job = OrphanedJobsManager.get_orphaned_job(jid)
    halt 404 unless @job
    erb File.read(File.join(File.dirname(__FILE__), '../../../web/views/orphaned_job.erb'))
  end

  app.post '/orphaned-jobs/:jid/retry' do
    jid = params[:jid]
    result = OrphanedJobsManager.retry_orphaned_job(jid)
    if result[:success]
      redirect to('/orphaned-jobs')
    else
      halt 400, result[:error]
    end
  end

  app.post '/orphaned-jobs/:jid/delete' do
    jid = params[:jid]
    result = OrphanedJobsManager.delete_orphaned_job(jid)
    if result[:success]
      redirect to('/orphaned-jobs')
    else
      halt 400, result[:error]
    end
  end

  app.post '/orphaned-jobs/bulk-action' do
    action = params[:action]
    jids = params[:jids] || []
    
    case action
    when 'retry'
      result = OrphanedJobsManager.bulk_retry_orphaned_jobs(jids)
    when 'delete'
      result = OrphanedJobsManager.bulk_delete_orphaned_jobs(jids)
    else
      halt 400, "Invalid action: #{action}"
    end

    if result[:success]
      redirect to('/orphaned-jobs')
    else
      halt 400, result[:error]
    end
  end

  app.get '/orphaned-jobs/stats' do
    content_type :json
    OrphanedJobsManager.get_stats.to_json
  end
end