Skip to content

Instantly share code, notes, and snippets.

@mabsboza
Created July 14, 2023 23:30
Show Gist options
  • Save mabsboza/b3cda7de868e505cd05508d9bd4abe63 to your computer and use it in GitHub Desktop.
Save mabsboza/b3cda7de868e505cd05508d9bd4abe63 to your computer and use it in GitHub Desktop.
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
@regina1987
Copy link

regina1987 commented Jul 15, 2023

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 15, sorry no se que es eso

-linea 40, son cienytos acceses para cada worker, estas seguro que quires todos poner en orden??? es mucha info

  • tema de last access hay que pensar mas que podemos hacer, esta enganchando muchas accesos

-log_exception no se ese methdodo y como usarlo , van a salir error donde?? surante el job en que momento? para cada worker?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment