1
0

jobs_helper.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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.load(job.handler.to_s).try(:job_data)
  25. case data['job_class']
  26. when 'AgentCheckJob', 'AgentReceiveJob'
  27. Agent.find_by_id(data['arguments'][0])
  28. when 'AgentRunScheduleJob'
  29. "Run Agent schedule '#{data['arguments'][0]}'"
  30. when 'AgentCleanupExpiredJob'
  31. 'Run Event cleanup'
  32. when 'AgentPropagateJob'
  33. 'Run Event propagation'
  34. else
  35. false
  36. end
  37. rescue ArgumentError
  38. # We can get to this point before all of the agents have loaded (usually,
  39. # in development)
  40. nil
  41. end
  42. end