StoryGC.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Storage;
  6. use App\Story;
  7. use App\StoryView;
  8. use App\Jobs\StoryPipeline\StoryExpire;
  9. use App\Jobs\StoryPipeline\StoryRotateMedia;
  10. use App\Services\StoryService;
  11. class StoryGC extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'story:gc';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Clear expired Stories';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return mixed
  38. */
  39. public function handle()
  40. {
  41. $this->archiveExpiredStories();
  42. $this->rotateMedia();
  43. }
  44. protected function archiveExpiredStories()
  45. {
  46. $stories = Story::whereActive(true)
  47. ->where('created_at', '<', now()->subHours(24))
  48. ->get();
  49. foreach($stories as $story) {
  50. StoryExpire::dispatch($story)->onQueue('story');
  51. }
  52. }
  53. protected function rotateMedia()
  54. {
  55. $queue = StoryService::rotateQueue();
  56. if(!$queue || count($queue) == 0) {
  57. return;
  58. }
  59. collect($queue)
  60. ->each(function($id) {
  61. $story = StoryService::getById($id);
  62. if(!$story) {
  63. StoryService::removeRotateQueue($id);
  64. return;
  65. }
  66. if($story->created_at->gt(now()->subMinutes(20))) {
  67. return;
  68. }
  69. StoryRotateMedia::dispatch($story)->onQueue('story');
  70. StoryService::removeRotateQueue($id);
  71. });
  72. }
  73. }