1
0

jobs_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class JobsController < ApplicationController
  2. before_action :authenticate_admin!
  3. def index
  4. @jobs = Delayed::Job.order("coalesce(failed_at,'1000-01-01'), run_at asc").page(params[:page])
  5. respond_to do |format|
  6. format.html { render layout: !request.xhr? }
  7. format.json { render json: @jobs }
  8. end
  9. end
  10. def destroy
  11. @job = Delayed::Job.find(params[:id])
  12. respond_to do |format|
  13. if !running? && @job.destroy
  14. format.html { redirect_to jobs_path, notice: "Job deleted." }
  15. format.json { render json: "", status: :ok }
  16. else
  17. format.html { redirect_to jobs_path, alert: 'Can not delete a running job.' }
  18. format.json { render json: "", status: :unprocessable_entity }
  19. end
  20. end
  21. end
  22. def run
  23. @job = Delayed::Job.find(params[:id])
  24. @job.last_error = nil
  25. respond_to do |format|
  26. if !running? && @job.update_attributes!(run_at: Time.now, failed_at: nil)
  27. format.html { redirect_to jobs_path, notice: "Job enqueued." }
  28. format.json { render json: @job, status: :ok }
  29. else
  30. format.html { redirect_to jobs_path, alert: 'Can not enqueue a running job.' }
  31. format.json { render json: "", status: :unprocessable_entity }
  32. end
  33. end
  34. end
  35. def destroy_failed
  36. Delayed::Job.where.not(failed_at: nil).delete_all
  37. respond_to do |format|
  38. format.html { redirect_to jobs_path, notice: "Failed jobs removed." }
  39. format.json { render json: '', status: :ok }
  40. end
  41. end
  42. private
  43. def running?
  44. (@job.locked_at || @job.locked_by) && @job.failed_at.nil?
  45. end
  46. end