Class: UploadScratchCleanupWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/workers/upload_scratch_cleanup_worker.rb

Overview

Reaps abandoned scratch files under +config.x.temp_storage_path+.

Upload.temp_location hands every caller its own directory so two uploads
sharing a basename can't overwrite each other, and Upload#set_local_file_path
keeps those files as a read-through cache for Upload#to_file. Nothing ever
removed them: before the per-directory change the tree was bounded by the
number of DISTINCT basenames, and to_file's remote branch already wrote an
unbounded tmp_<id>_<timestamp> per download. Isolation turns "bounded by
distinct names" into "one directory per scratch file", so the tree now needs
a sweeper rather than merely deserving one.

Deliberately age-based rather than reference-based: local_file_path is a
cache, never the source of truth. Anything reaped is re-fetched from the
datastore on the next #to_file, so a too-eager sweep costs a download, never
data.

Scheduled daily via config/sidekiq_production_schedule.yml, mirroring
+GeneratedPdfCleanupWorker+.

Constant Summary collapse

STALE_AFTER =

Comfortably longer than any single job's lifetime, so an in-flight
multi-step PDF build can't have its scratch file pulled mid-run.

2.days

Instance Method Summary collapse

Instance Method Details

#performObject

Runs the job.

Returns:

  • (Object)

    the result



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/workers/upload_scratch_cleanup_worker.rb', line 33

def perform
  root = Rails.application.config.x.temp_storage_path
  return Rails.logger.info('[UploadScratchCleanupWorker] no scratch root') unless File.directory?(root)

  cutoff = STALE_AFTER.ago
  removed_files = 0
  removed_dirs = 0

  Dir.children(root).each do |entry|
    path = File.join(root, entry)
    next if File.mtime(path) > cutoff

    if File.directory?(path)
      FileUtils.rm_rf(path)
      removed_dirs += 1
    else
      File.delete(path)
      removed_files += 1
    end
  rescue Errno::ENOENT
    next # raced with another sweep or a live job — nothing to do
  rescue StandardError => e
    Rails.logger.warn("[UploadScratchCleanupWorker] could not remove #{path}: #{e.class}: #{e.message}")
  end

  Rails.logger.info(
    "[UploadScratchCleanupWorker] removed #{removed_dirs} director(ies) and #{removed_files} loose file(s) " \
    "older than #{STALE_AFTER.inspect}"
  )
end