clean_job.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. use App\Services\Boot;
  5. require __DIR__ . '/../app/predefine.php';
  6. require __DIR__ . '/../vendor/autoload.php';
  7. require __DIR__ . '/../config/.config.php';
  8. Boot::setTime();
  9. Boot::bootDb();
  10. $processed = [];
  11. $renew = [];
  12. $renew_c = function ($ids) use ($processed) {
  13. echo 'Renew Process START.';
  14. foreach ($ids as $id) {
  15. $bought = \App\Models\Bought::find($id);
  16. if ($bought == null) {
  17. echo 'Bought not found:' . $id . PHP_EOL;
  18. unlink(__DIR__ . '/../storage/' . $id . '.renew');
  19. $processed['renew'] = $id;
  20. } else {
  21. $bought->is_notified = true;
  22. if ($bought->save() == true) {
  23. unlink(__DIR__ . '/../storage/' . $id . '.renew');
  24. echo 'Renew Process successed for bought' . $id . PHP_EOL;
  25. $processed['renew'] = $id;
  26. }
  27. }
  28. }
  29. echo 'Renew Process END.' . PHP_EOL . PHP_EOL;
  30. };
  31. $offline = [];
  32. $offline_c = function ($ids) use ($processed) {
  33. echo 'Offline Process START.';
  34. foreach ($ids as $id) {
  35. $node = \App\Models\Node::find($id);
  36. if ($node == null) {
  37. echo 'Node not found:' . $id . PHP_EOL;
  38. unlink(__DIR__ . '/../storage/' . $id . '.offline');
  39. $processed['offline'] = $id;
  40. } else {
  41. $node->online = false;
  42. if ($node->save() == true) {
  43. unlink(__DIR__ . '/../storage/' . $id . '.offline');
  44. echo 'Offline Process successed for node' . $id . PHP_EOL;
  45. $processed['offline'] = $id;
  46. }
  47. }
  48. }
  49. echo 'Offline Process END.' . PHP_EOL . PHP_EOL;
  50. };
  51. $expire = [];
  52. $expire_c = function ($ids) use ($processed) {
  53. echo 'Expire Process START.';
  54. foreach ($ids as $id) {
  55. $user = \App\Models\User::find($id);
  56. if ($user == null) {
  57. echo 'User not found:' . $id . PHP_EOL;
  58. unlink(__DIR__ . '/../storage/' . $id . '.expire_in');
  59. $processed['expire'] = $id;
  60. } else {
  61. $user->expire_notified = true;
  62. if ($user->save() == true) {
  63. unlink(__DIR__ . '/../storage/' . $id . '.expire_in');
  64. echo 'Expire Process successed for user' . $id . PHP_EOL;
  65. $processed['expire'] = $id;
  66. }
  67. }
  68. }
  69. echo 'Expire Process END.' . PHP_EOL . PHP_EOL;
  70. };
  71. $gfw = [];
  72. $gfw_c = function ($ids) use ($processed) {
  73. echo 'GFW Process START.';
  74. foreach ($ids as $id) {
  75. $node = \App\Models\Node::find($id);
  76. if ($node == null) {
  77. echo 'Node not found:' . $id . PHP_EOL;
  78. unlink(__DIR__ . '/../storage/' . $id . '.gfw');
  79. $processed['gfw'] = $id;
  80. } else {
  81. $node->gfw_block = true;
  82. if ($node->save() == true) {
  83. unlink(__DIR__ . '/../storage/' . $id . '.gfw');
  84. echo 'GFW Process successed for node' . $id . PHP_EOL;
  85. $processed['gfw'] = $id;
  86. }
  87. }
  88. }
  89. echo 'GFW Process END.' . PHP_EOL . PHP_EOL;
  90. };
  91. $files = scandir(__DIR__ . '/../storage');
  92. foreach ($files as $origin_file) {
  93. $file = explode('.', $origin_file);
  94. if (count($file) == 2 && is_numeric($file[0])) {
  95. switch ($file[1]) {
  96. case 'renew':
  97. $renew[] = $file[0];
  98. break;
  99. case 'offline':
  100. $offline[] = $file[0];
  101. break;
  102. case 'expire_in':
  103. $expire[] = $file[0];
  104. break;
  105. case 'gfw':
  106. $gfw[] = $file[0];
  107. break;
  108. default:
  109. echo 'Unrecognized file: ' . $origin_file . PHP_EOL;
  110. }
  111. } else {
  112. continue;
  113. }
  114. }
  115. $renew_c($renew);
  116. $offline_c($offline);
  117. $expire_c($expire);
  118. $gfw_c($gfw);
  119. if (file_exists(__DIR__ . '/../storage/traffic_notified') == true) {
  120. $files = scandir(__DIR__ . '/../storage/traffic_notified');
  121. if ($files != false) {
  122. foreach ($files as $origin_file) {
  123. $file = explode('.', $origin_file);
  124. if (count($file) == 2 && is_numeric($file[0] && $file[1] == 'userid')) {
  125. $notified[] = $file[0];
  126. } else {
  127. echo 'Unrecognized file: ' . $origin_file . PHP_EOL;
  128. }
  129. }
  130. file_put_contents(__DIR__ . '/notified.json', json_encode($file));
  131. }
  132. $notified_c = function ($ids) use ($processed) {
  133. echo 'Notified Process START.';
  134. foreach ($ids as $id) {
  135. $user = \App\Models\User::find($id);
  136. if ($user == null) {
  137. echo 'User not found:' . $id . PHP_EOL;
  138. unlink(__DIR__ . '/../storage/traffic_notified/' . $id . '.userid');
  139. $processed['notified'] = $id;
  140. } else {
  141. $user->traffic_notified = true;
  142. if ($user->save() == true) {
  143. unlink(__DIR__ . '/../storage/traffic_notified/' . $id . '.userid');
  144. echo 'Notified Process successed for node' . $id . PHP_EOL;
  145. $processed['notified'] = $id;
  146. }
  147. }
  148. }
  149. echo 'Notified Process END.' . PHP_EOL . PHP_EOL;
  150. };
  151. } else {
  152. echo 'Notified Process Nothing to do.' . PHP_EOL . PHP_EOL;
  153. }
  154. file_put_contents(__DIR__ . '/processed.json', json_encode($processed));
  155. file_put_contents(__DIR__ . '/raw.json', json_encode([
  156. 'renew' => $renew,
  157. 'offline' => $offline,
  158. 'expire' => $expire,
  159. 'gfw' => $gfw,
  160. ]));
  161. exit(0);