Class: Decidim::Cdtb::Storage::LocalSharding

Inherits:
Task
  • Object
show all
Defined in:
lib/decidim/cdtb/storage/local_sharding.rb

Overview

Given that all assets has already been copied from S3 to storage/, this task performs the sharding of the downloaded files.

This step is required because in S3 all assets are stored flat at the same level (directory), but local service stores the files with sharding.

Instance Attribute Summary

Attributes inherited from Task

#num_applied, #title

Instance Method Summary collapse

Methods inherited from Task

#execute!, #finish, #init

Methods included from TasksUtils

#do_log_error, #do_log_info, #log_task_end, #log_task_failure, #log_task_info, #log_task_step, #log_task_title, #logger

Constructor Details

#initializeLocalSharding

Returns a new instance of LocalSharding.



12
13
14
15
# File 'lib/decidim/cdtb/storage/local_sharding.rb', line 12

def initialize
  progress_bar= { title: "ActiveStorage::Blob" }
  super("S3 to local: DO SHARDING", progress_bar:)
end

Instance Method Details

#do_execution(context) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/decidim/cdtb/storage/local_sharding.rb', line 26

def do_execution(context)
  progress_bar= context[:progress_bar]

  ActiveStorage::Blob.find_each do |blob|
    path= ActiveStorage::Blob.service.path_for(blob.key)
    src_file= Rails.root.join("tmp/storage", blob.key)
    if File.exist?(src_file)
      shard_asset(blob, path)
      @num_applied+= 1
    else
      logger.warn "File Not Found or directory: #{path}"
    end
    progress_bar.increment
  end
end

#end_execution(_ctx) ⇒ Object



42
43
44
# File 'lib/decidim/cdtb/storage/local_sharding.rb', line 42

def end_execution(_ctx)
  log_task_info("#{@num_applied} blobs sharded")
end

#prepare_execution(_ctx) ⇒ Object



17
18
19
20
# File 'lib/decidim/cdtb/storage/local_sharding.rb', line 17

def prepare_execution(_ctx)
  @num_blobs= ActiveStorage::Blob.count
  log_task_info("Checking #{@num_blobs} blobs...")
end

#total_itemsObject



22
23
24
# File 'lib/decidim/cdtb/storage/local_sharding.rb', line 22

def total_items
  @num_blobs
end