confirmToProceed()) { return; } Event::listen(MoveAvatarEvent::class, function ($event) { $this->handleEvent($event->contact); }); $delay = now(); Contact::where('has_avatar', true) ->chunk(100, function ($contacts) use ($delay) { foreach ($contacts as $contact) { if ($contact->avatar_source === 'default') { $this->handleContact($contact, $delay); } } // add some delay, so we treat 100 contacts each minutes $delay = $delay->addMinutes(1); }); } private function handleContact($contact, $delay) { try { if ($this->option('dryrun')) { MoveContactAvatarToPhotosDirectory::dispatchNow($contact, true); } else { MoveContactAvatarToPhotosDirectory::dispatch($contact, false) ->delay($delay); } } catch (FileNotFoundException $e) { $this->warn(' ! File not found: '.$e->fileName, OutputInterface::VERBOSITY_VERBOSE); } } private function handleEvent($contact) { $this->info('Contact id:'.$contact->id.' | Avatar location:'.$contact->avatar_location.' | File name:'.$contact->avatar_file_name); } }