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
| <?php function refactorData() { try { $timeStart = microtime(true); $startDesc = date('Y-m-d H:i:s') . ' 开始执行!' . PHP_EOL; $logFile = __DIR__ . '/refactor_data.log'; echo $startDesc; file_put_contents($logFile, $startDesc);
$updateData = []; $success = $failed = $exception = 0; foreach ($data as $datum) { if (false) { $failed++; continue; file_put_contents(logFile, $datum['id'] . '由于xxx执行失败' . PHP_EOL, FILE_APPEND); }
if (false) { $exception++; continue; file_put_contents(logFile, $datum['id'] . '由于xxx无法执行' . PHP_EOL, FILE_APPEND); }
file_put_contents($logFile, $datum['id'] . PHP_EOL, FILE_APPEND); $success++; }
$timeStop = microtime(true); $endDesc = date('Y-m-d H:i:s') . '执行结束!' . PHP_EOL; file_put_contents($logFile, $endDesc, FILE_APPEND); echo $endDesc;
$seconds = round($timeStop - $timeStart, 3); switch ($seconds) { case $seconds > 60 && $seconds < 3600: $elapsed = intval($seconds / 60) . 'm' . ($seconds % 60) . 's'; break; case $seconds > 3600 && $seconds < 86400: $elapsed = intval($seconds / 3600) . 'h' . intval($seconds % 3600 / 60) . 'm' . ($seconds % 60) . 's'; break; default : $elapsed = $seconds . 's'; }
$formatStorageUnit = function ($scale) { $gb = 1 << 30; $mb = 1 << 20; $kb = 1 << 10; switch ($scale) { case $scale >= $gb: $humanUnit = round($scale / $gb, 2) . 'GB'; break; case $scale >= $mb: $humanUnit = round($scale / $mb, 2) . 'MB'; break; case $scale >= $kb: $humanUnit = round($scale / $kb, 2) . 'KB'; break; default: $humanUnit = $scale . 'B'; }
return $humanUnit; };
$total = 999; $summary = '=============统计摘要信息================' . PHP_EOL . '本次共需处理' . $total . '条数据,成功' . $success . '条,失败' . $failed . '条,无法处理' . $exception . '条。' . PHP_EOL . '执行耗时' . $elapsed . ', 内存消耗' . $formatStorageUnit(memory_get_usage()) . ',内存消耗峰值' . $formatStorageUnit(memory_get_peak_usage()) . PHP_EOL;
file_put_contents($logFile, $summary, FILE_APPEND); echo $summary;
(new Mail())->sendMail([ 'title' => 'xxx脚本执行完成', 'body' => $summary, 'receiver' => '[email protected]' ]); } catch (Exception $e) { (new Mail())->sendMail([ 'title' => 'xxx脚本执行失败', 'body' => '执行失败,错误原因:' . $e->getMessage(), 'receiver' => '[email protected]' ]); } }
|