Created
July 14, 2023 23:30
-
-
Save mabsboza/b3cda7de868e505cd05508d9bd4abe63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DeactivateInactiveContractorWorkers | |
def perform | |
Site.where(app: 'mall').includes(staffing_contractors: :non_archived_jobs).each do |site| | |
next unless site.contractor_worker_expiration_days | |
@expiration_days = site.contractor_worker_expiration_days | |
site.staffing_contractors do |contractor| | |
next if has_active_job?(contractor) | |
active_contractor_workers = contractor.alive_contractor_workers | |
active_contractor_workers.each do |worker| | |
if last_update_expired?(worker) && last_access_expired?(worker) | |
begin | |
worker.banish! | |
rescue => exc | |
log_exception(exc) | |
end | |
end | |
end | |
end | |
end | |
end | |
private | |
def last_update_expired?(_worker) | |
elapsed_days_since_last_update = (Date.current - _worker.updated_at.to_date).to_i | |
@expiration_days < elapsed_days_since_last_update | |
end | |
def last_access_expired?(_worker) | |
access = last_access(_worker) | |
return true if access.nil? | |
elapsed_days_since_last_access = (Date.current - access.to_date).to_i | |
@expiration_days < elapsed_days_since_last_access | |
end | |
def last_access(_worker) | |
_worker.access.order("access DESC").first.try(:access) | |
end | |
def has_active_job?(_contractor) | |
if _contractor.non_archived_jobs | |
.where_scheduled('not_finished', on: @expiration_days.days.ago) | |
.present? | |
end | |
end | |
def log_exception(_exc) | |
Rails.logger.error 'Error while automatically disabling contractor worker' | |
Rails.logger.error _exc.message | |
Rails.logger.error _exc.backtrace.join("\n") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
la idea en general me gusta
hay detalles que pdriamos mejorar:
active_contractor_workers = contractor.alive_contractor_workers
active_contractor_workers.each do |worker|
lines 10-11. para que este variable intermedia active_contcator_workers
-linea 40, son cienytos acceses para cada worker, estas seguro que quires todos poner en orden??? es mucha info
-log_exception no se ese methdodo y como usarlo , van a salir error donde?? surante el job en que momento? para cada worker?