NodeDailyMaintenance.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Node;
  4. use App\Models\NodeDailyDataFlow;
  5. use App\Models\User;
  6. use App\Notifications\NodeDailyReport;
  7. use App\Notifications\NodeRenewal;
  8. use Carbon\Carbon;
  9. use Illuminate\Console\Command;
  10. use Illuminate\Database\Eloquent\Builder;
  11. use Log;
  12. use Notification;
  13. class NodeDailyMaintenance extends Command
  14. {
  15. protected $signature = 'node:maintenance';
  16. protected $description = '执行节点的日常维护,包括发送每日使用报告和检查续约提醒';
  17. public function handle(): void
  18. {
  19. $jobTime = microtime(true);
  20. if (sysConfig('node_daily_notification')) {
  21. $this->nodedailyReport();
  22. }
  23. if (sysConfig('node_renewal_notification')) {// 通知节点急需续约
  24. $this->checkNodeRenewDays();
  25. }
  26. $this->updateNodeRenewal();
  27. $jobTime = round(microtime(true) - $jobTime, 4);
  28. Log::info(__('----「:job」Completed, Used :time seconds ----', ['job' => $this->description, 'time' => $jobTime]));
  29. }
  30. private function nodedailyReport(): void
  31. {
  32. $nodeDailyLogs = NodeDailyDataFlow::with('node:id,name')->has('node')->whereDate('created_at', date('Y-m-d', strtotime('yesterday')))->orderBy('node_id')->get();
  33. $data = [];
  34. $sum_u = 0;
  35. $sum_d = 0;
  36. foreach ($nodeDailyLogs as $log) {
  37. $data[] = [
  38. 'name' => $log->node->name,
  39. 'upload' => formatBytes($log->u),
  40. 'download' => formatBytes($log->d),
  41. 'total' => formatBytes($log->u + $log->d),
  42. ];
  43. $sum_u += $log->u;
  44. $sum_d += $log->d;
  45. }
  46. if ($data) {
  47. $data[] = [
  48. 'name' => trans('notification.node.total'),
  49. 'upload' => formatBytes($sum_u),
  50. 'download' => formatBytes($sum_d),
  51. 'total' => formatBytes($sum_u + $sum_d),
  52. ];
  53. $superAdmins = User::role('Super Admin')->get();
  54. if ($superAdmins->isNotEmpty()) {
  55. Notification::send($superAdmins, new NodeDailyReport($data));
  56. }
  57. }
  58. }
  59. private function checkNodeRenewDays(): void
  60. {
  61. $now = Carbon::now();
  62. $notificationDates = [ // 通知日期 分别是 前1天,3天和7天
  63. $now->addDays(1)->format('Y-m-d'),
  64. $now->addDays(2)->format('Y-m-d'),
  65. $now->addDays(4)->format('Y-m-d'),
  66. ];
  67. $nodes = Node::whereNotNull('details')->whereIn('details->next_renewal_date', $notificationDates)->pluck('name', 'id')->toArray();
  68. if (! empty($nodes)) {
  69. Notification::send(User::find(1), new NodeRenewal($nodes));
  70. }
  71. }
  72. private function updateNodeRenewal(): void
  73. {
  74. // 获取符合条件的节点
  75. $nodes = Node::whereNotNull('details')
  76. ->where(function (Builder $query) {
  77. $query->where('details->subscription_term', '<>', null)
  78. ->where('details->next_renewal_date', '<=', Carbon::now()->format('Y-m-d'));
  79. })
  80. ->get();
  81. // 更新每个节点的 next_renewal_date
  82. foreach ($nodes as $node) {
  83. $details = $node->details;
  84. $details['next_renewal_date'] = Carbon::createFromFormat('Y-m-d', $details['next_renewal_date'])->add($details['subscription_term'])->format('Y-m-d');
  85. $node->details = $details;
  86. $node->save();
  87. }
  88. }
  89. }