Snippets
Created by
Idoenk . Heri Purnomo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | <?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use App\Models\Tracker;
use App\Models\Task;
use Carbon\Carbon;
use DB;
class PullMaster extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sdb:pull-master'
.' {--force : Force process without checking lockfile }'
;
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sync tables from other database. In env it specified as DB2_*';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct() {
parent::__construct();
$this->cacheFile = Cache::store('file');
$this->cmd_name = (new \ReflectionClass($this))->getShortName();
// define lockfile to stay in minutes
$this->expire_lockfile = 20;
// Size of chunked rows
$this->chunked_size = 30;
$this->overwrite_row = true;
// Main key process
$this->key_process = 'sdb:pullmaster-lock';
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
if (self::isThereProcess()) {
$this->info('There is still process running: '.$this->key_process);
$this->info('To allow command run anyway, use option: --force');
if($this->expireProcess) {
$expCarbon = Carbon::createFromTimestamp($this->expireProcess);
$this->info('Try again later: '.$expCarbon->format('d M Y, H:i:s'));
}
return null;
}
else {
$start_time = microtime(true);
\Log::info($this->cmd_name.' initiated.');
$this->info($this->cmd_name.' started...');
$this->info('');
$params = $ret = [];
$options = $this->options();
$params['verbose'] = $options['verbose'];
$is_verbose = !empty($params['verbose']);
$maptables = [
'task' => '\App\Models\Task',
'tracker' => '\App\Models\Tracker',
'tracker_tasks' => '\App\Models\TrackerTasks',
];
$todos = array_keys($maptables);
foreach($todos as $todo){
$model_name = (isset($maptables[$todo]) ? $maptables[$todo] : null);
if (empty($model_name))
continue;
$this->line(' > Proceeding: '.$todo);
$Model = null;
try{
$Model = new $model_name;
}catch (Exception $e) {
\Log::error('Unable initiating model: '.$model_name);
}
if (empty($Model))
continue;
$this->importTable($Model);
$this->line('');
drd('HALTED');
}
// end: foreach(todos)
}
}
/**
* Proceed import table
*/
private function importTable($Model){
/**
* Insert bulk of collection rows into its Model's table
* @param Eloquent collection results
* @return void
*/
$insertHandler = function($_rows) use ($Model){
if (empty($_rows) || (!empty($_rows) && $_rows->isEmpty())){
\Log::warn('Empty row');
return null;
}
$is_verbose = $this->option('verbose');
$count_inserted = 0;
foreach($_rows as $row){
$item_data = json_decode(json_encode($row), true);
// Find existing by id
$existing = $Model->find($row->id);
if (!empty($existing)){
if ($is_verbose)
$this->info(' > Row exists, ID: '.$row->id);
if ($this->overwrite_row){
// crossed-finger
$existing->forceDelete();
}
else{
if ($is_verbose)
$this->info(' > Skipping..');
}
}
if ($ret_item = $Model::create($item_data))
$count_inserted++;
}
// end: foreach
return $count_inserted;
};
// end: insertData;
$fields = '*';
// $fields = ['id', 'track_value'];
// Get task items
$items = $Model->setConnection('mysql2')
->select($fields)
->orderBy('id', 'ASC')
;
$count_items = $items->count();
$chunkSize = $this->chunked_size;
$chunk_step = 1;
$this->info(' > Proceed with chunked data: '.$chunkSize);
// Save ur arse, memory preserve
$items->chunk($chunkSize, function($rows) use ($insertHandler, &$chunk_step){
$this->info(' > Chunk-step #'.$chunk_step);
$insertHandler($rows);
$chunk_step++;
});
}
private function isThereProcess() {
$runningProcess = null;
if($this->option('force'))
self::destroyLockFile();
$key_process = $this->key_process;
$theCache = $this->cacheFile;
$lock_value = $theCache->get($key_process);
$now = Carbon::now();
$generateLockFile = function($expireAt=null) use ($theCache, $key_process) {
if (empty($expireAt))
$expireAt = Carbon::now();
$expireAt = $expireAt->addMinutes($this->expire_lockfile);
$this->info($this->cmd_name.' initiated: '.Carbon::now()->format('d M Y, H:i:s'));
$this->info('Lock file created. Expire in '.$this->expire_lockfile.' minutes ('.$expireAt->format('d M Y, H:i:s').')');
$theCache->put(
$key_process,
$expireAt->timestamp,
$this->expire_lockfile
);
$this->info('');
};
if (empty($lock_value)) {
$generateLockFile($now);
$runningProcess = false;
}
else {
if ($now->timestamp > $lock_value) {
$generateLockFile($now);
$runningProcess = false;
}
else {
$runningProcess = true;
$this->expireProcess = $lock_value;
}
}
return $runningProcess;
}
/**
* Once process is done we destroy lock-file like
* to allow another instance to run
*/
private function destroyLockFile() {
$theCache = $this->cacheFile;
if ($theCache->has($this->key_process)) {
$this->info('Destroying lockfile');
$theCache->forget($this->key_process);
}
return true;
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.