moe-lk /
bulk-upload
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Console\Commands; |
||
| 4 | |||
| 5 | use App\Mail\IncorrectTemplate; |
||
| 6 | use App\Mail\TerminatedReport; |
||
| 7 | use App\Models\Upload; |
||
| 8 | use App\Models\User; |
||
| 9 | use Illuminate\Support\Carbon; |
||
| 10 | use Illuminate\Support\Facades\DB; |
||
| 11 | use Illuminate\Support\Facades\Mail; |
||
| 12 | |||
| 13 | class ProcessTerminatedFiles extends ImportStudents |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The name and signature of the console command. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $signature = 'process:terminated'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The console command description. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $description = 'Command description'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Create a new command instance. |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | public function __construct() |
||
| 35 | { |
||
| 36 | parent::__construct(); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Execute the console command. |
||
| 41 | * |
||
| 42 | * @return mixed |
||
| 43 | */ |
||
| 44 | public function handle() |
||
| 45 | { |
||
| 46 | $files = $this->getFiles(); |
||
| 47 | try { |
||
| 48 | if(!empty($files)){ |
||
| 49 | array_walk($files,array($this,'process')); |
||
| 50 | unset($files); |
||
| 51 | exit(); |
||
|
0 ignored issues
–
show
|
|||
| 52 | |||
| 53 | }else{ |
||
| 54 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
| 55 | $output->writeln('No files found,Waiting for files'); |
||
| 56 | exit(); |
||
|
0 ignored issues
–
show
|
|||
| 57 | |||
| 58 | } |
||
| 59 | |||
| 60 | }catch (Exception $e){ |
||
|
0 ignored issues
–
show
|
|||
| 61 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
| 62 | $output->writeln($e); |
||
| 63 | sleep(300); |
||
| 64 | $this->handle(); |
||
| 65 | |||
| 66 | } |
||
| 67 | |||
| 68 | } |
||
| 69 | |||
| 70 | protected function getFiles(){ |
||
| 71 | $files = Upload::where('is_processed', '=', 3) |
||
| 72 | ->where('is_email_sent','=',0) |
||
| 73 | ->where('updated_at', '<=', Carbon::now()->tz('Asia/Colombo')->subHours(3)) |
||
| 74 | ->limit(50) |
||
| 75 | ->get()->toArray(); |
||
| 76 | return $files; |
||
| 77 | } |
||
| 78 | |||
| 79 | protected function process($file){ |
||
| 80 | $time = Carbon::now()->tz('Asia/Colombo'); |
||
| 81 | $this->processSheet($file); |
||
| 82 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
| 83 | $now = Carbon::now()->tz('Asia/Colombo'); |
||
| 84 | $output->writeln('=============== Time taken to batch ' .$now->diffInMinutes($time)); |
||
| 85 | |||
| 86 | } |
||
| 87 | |||
| 88 | protected function processSheet($file){ |
||
| 89 | $this->startTime = Carbon::now()->tz('Asia/Colombo'); |
||
|
0 ignored issues
–
show
|
|||
| 90 | $user = User::find($file['security_user_id']); |
||
| 91 | $output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
||
| 92 | $output->writeln('##########################################################################################################################'); |
||
| 93 | $output->writeln('Processing the file: '.$file['filename']); |
||
| 94 | if ($this->checkTime()) { |
||
| 95 | Mail::to($user->email)->send(new TerminatedReport($file)); |
||
|
0 ignored issues
–
show
|
|||
| 96 | DB::table('uploads') |
||
| 97 | ->where('id', $file['id']) |
||
| 98 | ->update(['is_processed' => 3, 'is_email_sent' => 1,'updated_at' => now()]); |
||
| 99 | } else { |
||
| 100 | exit(); |
||
|
0 ignored issues
–
show
|
|||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | } |
||
| 107 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.