jobs_helper.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. module JobsHelper
  2. def status(job)
  3. case
  4. when job.failed_at
  5. content_tag :span, 'failed', class: 'label label-danger'
  6. when job.locked_at && job.locked_by
  7. content_tag :span, 'running', class: 'label label-info'
  8. else
  9. content_tag :span, 'queued', class: 'label label-warning'
  10. end
  11. end
  12. def relative_distance_of_time_in_words(time)
  13. if time < (now = Time.now)
  14. time_ago_in_words(time) + ' ago'
  15. else
  16. 'in ' + distance_of_time_in_words(time, now)
  17. end
  18. end
  19. # Given an queued job, parse the stored YAML to retrieve the ID of the Agent
  20. # meant to be ran.
  21. #
  22. # Can return nil, or an instance of Agent.
  23. def agent_from_job(job)
  24. data = YAML.unsafe_load(job.handler.to_s).try(:job_data)
  25. return false unless data
  26. case data['job_class']
  27. when 'AgentCheckJob', 'AgentReceiveJob'
  28. Agent.find_by_id(data['arguments'][0])
  29. when 'AgentRunScheduleJob'
  30. "Run Agent schedule '#{data['arguments'][0]}'"
  31. when 'AgentCleanupExpiredJob'
  32. 'Run Event cleanup'
  33. when 'AgentPropagateJob'
  34. 'Run Event propagation'
  35. else
  36. false
  37. end
  38. rescue ArgumentError
  39. # We can get to this point before all of the agents have loaded (usually,
  40. # in development)
  41. nil
  42. end
  43. end