job.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class Job < ApplicationModel
  3. include ChecksClientNotification
  4. include ChecksConditionValidation
  5. include ChecksHtmlSanitized
  6. include ChecksPerformValidation
  7. include HasTimeplan
  8. include Job::Assets
  9. store :condition
  10. store :perform
  11. validates :name, presence: true
  12. before_save :updated_matching, :update_next_run_at
  13. sanitized_html :note
  14. =begin
  15. verify each job if needed to run (e. g. if true and times are matching) and execute it
  16. Job.run
  17. =end
  18. def self.run
  19. start_at = Time.zone.now
  20. jobs = Job.where(active: true)
  21. jobs.each do |job|
  22. next if !job.executable?
  23. job.run(false, start_at)
  24. end
  25. true
  26. end
  27. =begin
  28. execute a single job if needed (e. g. if true and times are matching)
  29. job = Job.find(123)
  30. job.run
  31. force to run job (ignore times are matching)
  32. job.run(true)
  33. =end
  34. def run(force = false, start_at = Time.zone.now)
  35. logger.debug { "Execute job #{inspect}" }
  36. ticket_ids = start_job(start_at, force)
  37. return if ticket_ids.nil?
  38. ticket_ids&.each_slice(10) do |slice|
  39. run_slice(slice)
  40. end
  41. finish_job
  42. end
  43. def executable?(start_at = Time.zone.now)
  44. return false if !active
  45. # only execute jobs older than 1 min to give admin time to make last-minute changes
  46. return false if updated_at > 1.minute.ago
  47. # check if job got stuck
  48. return false if running == true && last_run_at && 1.day.ago < last_run_at
  49. # check if jobs need to be executed
  50. # ignore if job was running within last 10 min.
  51. return false if last_run_at && last_run_at > start_at - 10.minutes
  52. true
  53. end
  54. def matching_count
  55. ticket_count, _tickets = Ticket.selectors(condition, limit: 1, execution_time: true)
  56. ticket_count || 0
  57. end
  58. private
  59. def next_run_at_calculate(time = Time.zone.now)
  60. return nil if !active
  61. if last_run_at && (time - last_run_at).positive?
  62. time += 10.minutes
  63. end
  64. timeplan_calculation.next_at(time)
  65. end
  66. def updated_matching
  67. self.matching = matching_count
  68. end
  69. def update_next_run_at
  70. self.next_run_at = next_run_at_calculate
  71. end
  72. def finish_job
  73. Transaction.execute(reset_user_id: true) do
  74. mark_as_finished
  75. end
  76. end
  77. def mark_as_finished
  78. self.running = false
  79. self.last_run_at = Time.zone.now
  80. save!
  81. end
  82. def start_job(start_at, force)
  83. Transaction.execute(reset_user_id: true) do
  84. if start_job_executable?(start_at, force) && start_job_ensure_matching_count && start_job_in_timeplan?(start_at, force)
  85. ticket_count, tickets = Ticket.selectors(condition, limit: 2_000, execution_time: true)
  86. logger.debug { "Job #{name} with #{ticket_count} tickets" }
  87. mark_as_started(ticket_count)
  88. tickets&.pluck(:id) || []
  89. end
  90. end
  91. end
  92. def start_job_executable?(start_at, force)
  93. return true if executable?(start_at) || force
  94. if next_run_at && next_run_at <= Time.zone.now
  95. save!
  96. end
  97. false
  98. end
  99. def start_job_ensure_matching_count
  100. matching = matching_count
  101. if self.matching != matching
  102. self.matching = matching
  103. save!
  104. end
  105. true
  106. end
  107. def start_job_in_timeplan?(start_at, force)
  108. return true if in_timeplan?(start_at) || force
  109. if next_run_at && next_run_at <= Time.zone.now
  110. save!
  111. end
  112. false
  113. end
  114. def mark_as_started(ticket_count)
  115. self.processed = ticket_count || 0
  116. self.running = true
  117. self.last_run_at = Time.zone.now
  118. save!
  119. end
  120. def run_slice(slice)
  121. Transaction.execute(disable_notification: disable_notification, reset_user_id: true) do
  122. _, tickets = Ticket.selectors(condition, limit: 2_000, execution_time: true)
  123. tickets
  124. &.where(id: slice)
  125. &.each do |ticket|
  126. ticket.perform_changes(self, 'job')
  127. end
  128. end
  129. end
  130. end