Update story garbage collection, handle non active stories and new ephemeral story media directory

This commit is contained in:
Daniel Supernault 2021-04-27 20:02:33 -06:00
parent 71aeb8a25d
commit c43f8bcce8
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
1 changed files with 106 additions and 82 deletions

View File

@ -3,103 +3,127 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\{ use Illuminate\Support\Facades\DB;
DB, use Illuminate\Support\Facades\Storage;
Storage use App\Story;
}; use App\StoryView;
use App\{
Story,
StoryView
};
class StoryGC extends Command class StoryGC extends Command
{ {
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'story:gc'; protected $signature = 'story:gc';
/** /**
* The console command description. * The console command description.
* *
* @var string * @var string
*/ */
protected $description = 'Clear expired Stories'; protected $description = 'Clear expired Stories';
/** /**
* Create a new command instance. * Create a new command instance.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
} }
/** /**
* Execute the console command. * Execute the console command.
* *
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
$this->directoryScan(); $this->directoryScan();
$this->deleteViews(); $this->deleteViews();
$this->deleteStories(); $this->deleteStories();
} }
protected function directoryScan() protected function directoryScan()
{ {
$day = now()->day; $hour = now()->hour;
if($day != 3) { if($hour !== 1) {
return; return;
} }
$monthHash = substr(hash('sha1', date('Y').date('m')), 0, 12); $monthHash = substr(hash('sha1', date('Y').date('m')), 0, 12);
$t1 = Storage::directories('public/_esm.t1'); $t1 = Storage::directories('public/_esm.t1');
$t2 = Storage::directories('public/_esm.t2'); $t2 = Storage::directories('public/_esm.t2');
$dirs = array_merge($t1, $t2); $dirs = array_merge($t1, $t2);
foreach($dirs as $dir) { foreach($dirs as $dir) {
$hash = last(explode('/', $dir)); $hash = last(explode('/', $dir));
if($hash != $monthHash) { if($hash != $monthHash) {
$this->info('Found directory to delete: ' . $dir); $this->info('Found directory to delete: ' . $dir);
$this->deleteDirectory($dir); $this->deleteDirectory($dir);
} }
} }
}
protected function deleteDirectory($path) $mh = hash('sha256', date('Y').'-.-'.date('m'));
{ $monthHash = date('Y').date('m').substr($mh, 0, 6).substr($mh, 58, 6);
Storage::deleteDirectory($path); $dirs = Storage::directories('public/_esm.t3');
}
protected function deleteViews() foreach($dirs as $dir) {
{ $hash = last(explode('/', $dir));
StoryView::where('created_at', '<', now()->subMinutes(1441))->delete(); if($hash != $monthHash) {
} $this->info('Found directory to delete: ' . $dir);
$this->deleteDirectory($dir);
}
}
}
protected function deleteStories() protected function deleteDirectory($path)
{ {
$stories = Story::where('created_at', '<', now()->subMinutes(1441))->take(50)->get(); Storage::deleteDirectory($path);
}
if($stories->count() == 0) { protected function deleteViews()
exit; {
} StoryView::where('created_at', '<', now()->subMinutes(1441))->delete();
}
foreach($stories as $story) { protected function deleteStories()
if(Storage::exists($story->path) == true) { {
Storage::delete($story->path); $stories = Story::where('created_at', '>', now()->subMinutes(30))
} ->whereNull('active')
DB::transaction(function() use($story) { ->get();
StoryView::whereStoryId($story->id)->delete();
$story->delete(); foreach($stories as $story) {
}); if(Storage::exists($story->path) == true) {
} Storage::delete($story->path);
} }
DB::transaction(function() use($story) {
StoryView::whereStoryId($story->id)->delete();
$story->delete();
});
}
$stories = Story::where('created_at', '<', now()
->subMinutes(1441))
->get();
if($stories->count() == 0) {
exit;
}
foreach($stories as $story) {
if(Storage::exists($story->path) == true) {
Storage::delete($story->path);
}
DB::transaction(function() use($story) {
StoryView::whereStoryId($story->id)->delete();
$story->delete();
});
}
}
} }