job.rb 3.9 KB

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