소스 검색

Merge pull request #788 from evaryont/jobs-show-agent-name

Show the agent's name and user in the jobs panel
Andrew Cantino 9 년 전
부모
커밋
f10e8d020c
3개의 변경된 파일28개의 추가작업 그리고 4개의 파일을 삭제
  1. 14 0
      app/helpers/jobs_helper.rb
  2. 4 1
      app/views/jobs/index.html.erb
  3. 10 3
      spec/controllers/jobs_controller_spec.rb

+ 14 - 0
app/helpers/jobs_helper.rb

@@ -18,4 +18,18 @@ module JobsHelper
       'in ' + distance_of_time_in_words(time, now)
     end
   end
+
+  # Given an queued job, parse the stored YAML to retrieve the ID of the Agent
+  # meant to be ran.
+  #
+  # Can return nil, or an instance of Agent.
+  def agent_from_job(job)
+    begin
+      Agent.find_by_id(YAML.load(job.handler).args[0])
+    rescue ArgumentError
+      # We can get to this point before all of the agents have loaded (usually,
+      # in development)
+      nil
+    end
+  end
 end

+ 4 - 1
app/views/jobs/index.html.erb

@@ -11,6 +11,7 @@
         <table class='table table-striped events'>
           <tr>
             <th>Status</th>
+            <th>Agent</th>
             <th>Created</th>
             <th>Next Run</th>
             <th>Attempts</th>
@@ -19,9 +20,11 @@
           </tr>
 
         <% @jobs.each do |job| %>
+          <% agent = agent_from_job(job) %>
           <tr>
             <td><%= status(job) %></td>
-            <td title='<%= job.created_at %>'><%= time_ago_in_words job.created_at %> ago</td>
+            <td><%= agent ? link_to(agent.name, agent_path(agent)) : "(deleted)" %></td>
+            <td title='<%= job.created_at %>'><%= time_ago_in_words job.created_at %> ago <%= agent ? "for #{agent.user.username}" : '' %></td>
             <td title='<%= job.run_at %>'>
               <% if !job.failed_at %>
                 <%= relative_distance_of_time_in_words job.run_at %>

+ 10 - 3
spec/controllers/jobs_controller_spec.rb

@@ -4,8 +4,15 @@ describe JobsController do
 
   describe "GET index" do
     before do
-      Delayed::Job.create!
-      Delayed::Job.create!
+      async_handler_yaml =
+        "--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/class 'Agent'\nmethod_name: :async_check_without_delay\nargs:\n- %d\n"
+
+      Delayed::Job.create!(handler: async_handler_yaml % [agents(:jane_website_agent).id])
+      Delayed::Job.create!(handler: async_handler_yaml % [agents(:bob_website_agent).id])
+      Delayed::Job.create!(handler: async_handler_yaml % [agents(:jane_weather_agent).id])
+      agents(:jane_website_agent).destroy
+      Delayed::Job.create!(handler: async_handler_yaml % [agents(:bob_weather_agent).id], locked_at: Time.now, locked_by: 'test')
+
       expect(Delayed::Job.count).to be > 0
     end
 
@@ -19,7 +26,7 @@ describe JobsController do
       expect(users(:jane)).to be_admin
       sign_in users(:jane)
       get :index
-      expect(assigns(:jobs).length).to eq(2)
+      expect(assigns(:jobs).length).to eq(4)
     end
   end