StoryGC.php 1.5 KB

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