-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: джоб для очистки локальных файлов
- Loading branch information
1 parent
8b3f710
commit 3a3a9c1
Showing
9 changed files
with
101 additions
and
18 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# coding: utf-8 | ||
require 'resque-integration' | ||
|
||
class AmazonS3UploadJob | ||
include Resque::Integration | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# coding: utf-8 | ||
|
||
module Apress | ||
module AmazonAssets | ||
class CleanPrivateAssetsJob | ||
include Resque::Integration | ||
|
||
unique | ||
|
||
def self.execute | ||
CleanLocalFilesService.new(PrivateAsset, PrivateAsset::STORAGE_TIME_OF_LOCAL_FILE).call | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# coding: utf-8 | ||
|
||
module Apress | ||
module AmazonAssets | ||
class CleanPublicAssetsJob | ||
include Resque::Integration | ||
|
||
unique | ||
|
||
def self.execute | ||
CleanLocalFilesService.new(PublicAsset, PublicAsset::STORAGE_TIME_OF_LOCAL_FILE).call | ||
end | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
app/services/apress/amazon_assets/clean_local_files_service.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# coding: utf-8 | ||
|
||
module Apress | ||
module AmazonAssets | ||
class CleanLocalFilesService | ||
# Чистка локальных файлов по дате обновления | ||
# scope - начальный скоуп | ||
# storage_time_of_local_file - время хранения локальных файлов | ||
def initialize(scope, storage_time_of_local_file) | ||
@scope = scope | ||
@storage_time_of_local_file = storage_time_of_local_file | ||
end | ||
|
||
def call | ||
@scope | ||
.where( | ||
"remote_file_name IS NOT NULL AND local_file_name IS NOT NULL AND local_updated_at < ?", | ||
@storage_time_of_local_file.ago.utc | ||
) | ||
.find_each do |asset| | ||
asset.local = nil | ||
asset.save(:validate => false) | ||
end | ||
end | ||
end | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
require 'apress/amazon_assets' | ||
require 'apress/amazon_assets' |
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
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
39 changes: 39 additions & 0 deletions
39
spec/services/apress/amazon_assets/clean_local_files_service_spec.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# coding: utf-8 | ||
require 'spec_helper' | ||
|
||
describe Apress::AmazonAssets::CleanLocalFilesService do | ||
context 'delete old assets' do | ||
let(:old_asset) do | ||
create :private_asset, | ||
:with_local_png, | ||
local_updated_at: (Apress::AmazonAssets::PrivateAsset::STORAGE_TIME_OF_LOCAL_FILE.ago.utc - 1.day) | ||
end | ||
let(:not_old_asset) do | ||
create :private_asset, | ||
:with_local_png, | ||
local_updated_at: (Apress::AmazonAssets::PrivateAsset::STORAGE_TIME_OF_LOCAL_FILE.ago.utc + 1.day) | ||
end | ||
|
||
before do | ||
allow_any_instance_of(Apress::AmazonAssets::PrivateAsset).to receive(:queue_upload_to_s3) | ||
VCR.use_cassette 'copy_to_remote', erb: {path: old_asset.local.url.sub(%r{.*/system/}, '')} do | ||
old_asset.copy_to_remote | ||
end | ||
VCR.use_cassette 'copy_to_remote', erb: {path: not_old_asset.local.url.sub(%r{.*/system/}, '')} do | ||
not_old_asset.copy_to_remote | ||
end | ||
described_class.new( | ||
Apress::AmazonAssets::PrivateAsset, | ||
Apress::AmazonAssets::PrivateAsset::STORAGE_TIME_OF_LOCAL_FILE | ||
).call | ||
end | ||
|
||
it do | ||
expect(old_asset.reload.local_file_name).to be_nil | ||
end | ||
|
||
it do | ||
expect(not_old_asset.reload.local_file_name).not_to be_nil | ||
end | ||
end | ||
end |