jobs_controller.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. class JobsController < ApplicationController
  2. before_action :authenticate_admin!
  3. def index
  4. @jobs = Delayed::Job.order(Arel.sql("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 { head :no_content }
  16. else
  17. format.html { redirect_to jobs_path, alert: 'Can not delete a running job.' }
  18. format.json { render json: @job.errors, 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!(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: @job.errors, status: :unprocessable_entity }
  32. end
  33. end
  34. end
  35. def retry_queued
  36. @jobs = Delayed::Job.awaiting_retry.update_all(run_at: Time.zone.now)
  37. respond_to do |format|
  38. format.html { redirect_to jobs_path, notice: "Queued jobs getting retried." }
  39. format.json { head :no_content }
  40. end
  41. end
  42. def destroy_failed
  43. Delayed::Job.where.not(failed_at: nil).delete_all
  44. respond_to do |format|
  45. format.html { redirect_to jobs_path, notice: "Failed jobs removed." }
  46. format.json { head :no_content }
  47. end
  48. end
  49. def destroy_all
  50. Delayed::Job.where(locked_at: nil).delete_all
  51. respond_to do |format|
  52. format.html { redirect_to jobs_path, notice: "All jobs removed." }
  53. format.json { head :no_content }
  54. end
  55. end
  56. private
  57. def running?
  58. (@job.locked_at || @job.locked_by) && @job.failed_at.nil?
  59. end
  60. end