2020-01-01 04:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
2021-04-28 02:02:33 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use App\Story;
|
|
|
|
use App\StoryView;
|
2021-09-04 02:45:56 +00:00
|
|
|
use App\Jobs\StoryPipeline\StoryExpire;
|
|
|
|
use App\Jobs\StoryPipeline\StoryRotateMedia;
|
|
|
|
use App\Services\StoryService;
|
2020-01-01 04:58:21 +00:00
|
|
|
|
|
|
|
class StoryGC extends Command
|
|
|
|
{
|
2021-04-28 02:02:33 +00:00
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'story:gc';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Clear expired Stories';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2021-09-04 02:45:56 +00:00
|
|
|
$this->archiveExpiredStories();
|
|
|
|
$this->rotateMedia();
|
2021-04-28 02:02:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 02:45:56 +00:00
|
|
|
protected function archiveExpiredStories()
|
2021-04-28 02:02:33 +00:00
|
|
|
{
|
2021-09-04 02:45:56 +00:00
|
|
|
$stories = Story::whereActive(true)
|
2021-09-04 07:59:50 +00:00
|
|
|
->where('created_at', '<', now()->subHours(24))
|
2021-04-28 02:02:33 +00:00
|
|
|
->get();
|
|
|
|
|
|
|
|
foreach($stories as $story) {
|
2021-09-04 02:45:56 +00:00
|
|
|
StoryExpire::dispatch($story)->onQueue('story');
|
2021-04-28 02:02:33 +00:00
|
|
|
}
|
2021-09-04 02:45:56 +00:00
|
|
|
}
|
2021-04-28 02:02:33 +00:00
|
|
|
|
2021-09-04 02:45:56 +00:00
|
|
|
protected function rotateMedia()
|
|
|
|
{
|
|
|
|
$queue = StoryService::rotateQueue();
|
2021-04-28 02:02:33 +00:00
|
|
|
|
2021-09-04 02:45:56 +00:00
|
|
|
if(!$queue || count($queue) == 0) {
|
|
|
|
return;
|
2021-04-28 02:02:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 02:45:56 +00:00
|
|
|
collect($queue)
|
|
|
|
->each(function($id) {
|
|
|
|
$story = StoryService::getById($id);
|
|
|
|
if(!$story) {
|
|
|
|
StoryService::removeRotateQueue($id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if($story->created_at->gt(now()->subMinutes(20))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
StoryRotateMedia::dispatch($story)->onQueue('story');
|
|
|
|
StoryService::removeRotateQueue($id);
|
2021-04-28 02:02:33 +00:00
|
|
|
});
|
|
|
|
}
|
2020-01-01 04:58:21 +00:00
|
|
|
}
|