1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Lsf\UniqueUid\UniqueUid; |
6
|
|
|
use App\Models\Security_user; |
7
|
|
|
use App\Models\Unique_user_id; |
8
|
|
|
use Exception; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
use Mohamednizar\MoeUuid\MoeUuid; |
|
|
|
|
11
|
|
|
|
12
|
|
|
class StudentsIdGen extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The name and signature of the console command. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'students:idgen {chunk} {max}'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command description. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $description = 'Command description'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create a new command instance. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->count = 0; |
|
|
|
|
36
|
|
|
$this->output = new \Symfony\Component\Console\Output\ConsoleOutput(); |
|
|
|
|
37
|
|
|
$this->students = new Security_user(); |
|
|
|
|
38
|
|
|
$this->uniqueUId = new UniqueUid(); |
|
|
|
|
39
|
|
|
$this->max = 0; |
|
|
|
|
40
|
|
|
$this->child = 0; |
|
|
|
|
41
|
|
|
parent::__construct(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Execute the console command. |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function handle() |
50
|
|
|
{ |
51
|
|
|
$students = $this->students->query() |
52
|
|
|
->where('is_student', 1) |
53
|
|
|
->limit(500000) |
54
|
|
|
->get()->toArray(); |
55
|
|
|
|
56
|
|
|
$students = array_chunk($students, $this->argument('chunk')); |
57
|
|
|
$this->processParallel($students, $this->argument('max')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function processParallel(array $arr, $procs = 4) |
61
|
|
|
{ |
62
|
|
|
// Break array up into $procs chunks. |
63
|
|
|
$chunks = array_chunk($arr, ceil((count($arr) / $procs))); |
|
|
|
|
64
|
|
|
$pid = -1; |
|
|
|
|
65
|
|
|
$children = array(); |
66
|
|
|
foreach ($chunks as $items) { |
67
|
|
|
$pid = pcntl_fork(); |
68
|
|
|
if ($pid === -1) { |
69
|
|
|
die('could not fork'); |
|
|
|
|
70
|
|
|
} else if ($pid === 0) { |
71
|
|
|
$this->output->writeln('started processes: ' . count($children)); |
72
|
|
|
// We are the child process. Pass a chunk of items to process. |
73
|
|
|
array_walk($items, array($this, 'process')); |
74
|
|
|
exit(0); |
|
|
|
|
75
|
|
|
} else { |
76
|
|
|
// We are the parent. |
77
|
|
|
$children[] = $pid; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
// Wait for children to finish. |
81
|
|
|
foreach ($children as $pid) { |
82
|
|
|
// We are still the parent. |
83
|
|
|
pcntl_waitpid($pid, $status); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
public function process($students) |
90
|
|
|
{ |
91
|
|
|
array_walk($students, array($this, 'updateNewUUID')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/* |
96
|
|
|
* @param $student |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
|
|
public function updateNewUUID($student) |
100
|
|
|
{ |
101
|
|
|
$this->uniqueUserId = new Unique_user_id(); |
|
|
|
|
102
|
|
|
if (!$this->uniqueUId::isValidUniqueId($student['openemis_no'])) { |
103
|
|
|
try { |
104
|
|
|
|
105
|
|
|
$newId = $this->uniqueUId::getUniqueAlphanumeric(); |
106
|
|
|
$student['openemis_no'] = $newId; |
107
|
|
|
$student = $this->uniqueUserId->updateOrInsertRecord($student); |
108
|
|
|
// $this->output->writeln('New NSID generated for :' . $student['id']); |
109
|
|
|
Security_user::query()->where('id', $student['id']) |
110
|
|
|
->update(['openemis_no' => $newId, 'username' => str_replace('-', '', $newId)]); |
111
|
|
|
} catch (\Exception $e) { |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
try { |
115
|
|
|
// $this->output->writeln('Updating student:' . $student['id']); |
116
|
|
|
$this->uniqueUserId->updateOrInsertRecord($student); |
117
|
|
|
} catch (\Exception $e) { |
118
|
|
|
dd($e); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths