Browse Source

refactor: clean Job file database (#904)

* refactor(job): clean file database

* fix(bought): wrong attribute name

你敢 pr 我就敢 merge
安楽岡小見 5 years ago
parent
commit
b9485251a2
6 changed files with 242 additions and 54 deletions
  1. 5 0
      sql/clean_job.sql
  2. 29 50
      src/Command/Job.php
  3. 12 0
      src/Models/Bought.php
  4. 14 0
      src/Models/Node.php
  5. 9 4
      src/Models/User.php
  6. 173 0
      utils/clean_job.php

+ 5 - 0
sql/clean_job.sql

@@ -0,0 +1,5 @@
+ALTER TABLE `bought` ADD `is_renewed` BOOLEAN NOT NULL DEFAULT FALSE AFTER `price`;
+ALTER TABLE `ss_node` ADD` online` BOOLEAN NOT NULL DEFAULT FALSE AFTER `mu_only`
+    ,ADD `gfw_block` BOOLEAN NOT NULL DEFAULT FALSE AFTER `online`;
+ALTER TABLE `user` ADD `expired` BOOLEAN NOT NULL DEFAULT FALSE AFTER `telegram_id`
+    ,ADD `traffic_notified` BOOLEAN NULL DEFAULT FALSE AFTER `expired`;

+ 29 - 50
src/Command/Job.php

@@ -348,8 +348,8 @@ class Job
         //自动续费
         $boughts = Bought::where('renew', '<', time())->where('renew', '<>', 0)->get();
         foreach ($boughts as $bought) {
+            /** @var Bought $bought */
             $user = User::where('id', $bought->userid)->first();
-
             if ($user == null) {
                 $bought->delete();
                 continue;
@@ -395,16 +395,13 @@ class Job
                     Mail::send($to, $subject, 'news/warn.tpl', [
                         'user' => $user,
                         'text' => $text
-                    ], [
                     ]);
                 } catch (Exception $e) {
                     echo $e->getMessage();
                 }
-
-                if (file_exists(BASE_PATH . '/storage/' . $bought->id . '.renew')) {
-                    unlink(BASE_PATH . '/storage/' . $bought->id . '.renew');
-                }
-            } elseif (!file_exists(BASE_PATH . '/storage/' . $bought->id . '.renew')) {
+                $bought->is_renewed = true;
+                $bought->save();
+            } elseif ($bought->is_renewed == false) {
                 $subject = Config::get('appName') . '-续费失败';
                 $to = $user->email;
                 $text = '您好,系统为您自动续费商品名:' . $shop->name . ',金额:' . $shop->price . ' 元 时,发现您余额不足,请及时充值。充值后请稍等系统便会自动为您续费。';
@@ -412,15 +409,10 @@ class Job
                     Mail::send($to, $subject, 'news/warn.tpl', [
                         'user' => $user,
                         'text' => $text
-                    ], [
                     ]);
                 } catch (Exception $e) {
                     echo $e->getMessage();
                 }
-                $myfile = fopen(BASE_PATH . '/storage/' . $bought->id . '.renew', 'wb+') or die('Unable to open file!');
-                $txt = '1';
-                fwrite($myfile, $txt);
-                fclose($myfile);
             }
         }
 
@@ -436,7 +428,7 @@ class Job
             $nodes = Node::all();
 
             foreach ($nodes as $node) {
-                if ($node->isNodeOnline() === false && !file_exists(BASE_PATH . '/storage/' . $node->id . '.offline')) {
+                if ($node->isNodeOnline() === false && $node->online == true) {
                     if (Config::get('useScFtqq') == true && Config::get('enable_detect_offline_useScFtqq') == true) {
                         $ScFtqq_SCKEY = Config::get('ScFtqq_SCKEY');
                         $text = '管理员您好,系统发现节点 ' . $node->name . ' 掉线了,请您及时处理。';
@@ -477,14 +469,9 @@ class Job
 
                     Telegram::Send($notice_text);
 
-                    $myfile = fopen(
-                        BASE_PATH . '/storage/' . $node->id . '.offline',
-                        'wb+'
-                    ) or die('Unable to open file!');
-                    $txt = '1';
-                    fwrite($myfile, $txt);
-                    fclose($myfile);
-                } elseif ($node->isNodeOnline() === true && file_exists(BASE_PATH . '/storage/' . $node->id . '.offline')) {
+                    $node->online = false;
+                    $node->save();
+                } elseif ($node->isNodeOnline() === true && $node->online == false) {
                     if (Config::get('useScFtqq') == true && Config::get('enable_detect_offline_useScFtqq') == true) {
                         $ScFtqq_SCKEY = Config::get('ScFtqq_SCKEY');
                         $text = '管理员您好,系统发现节点 ' . $node->name . ' 恢复上线了。';
@@ -524,8 +511,8 @@ class Job
                     }
 
                     Telegram::Send($notice_text);
-
-                    unlink(BASE_PATH . '/storage/' . $node->id . '.offline');
+                    $node->online == true;
+                    $node->save();
                 }
             }
         }
@@ -610,15 +597,11 @@ class Job
                 } catch (Exception $e) {
                     echo $e->getMessage();
                 }
-                $myfile = fopen(
-                    BASE_PATH . '/storage/' . $user->id . '.expire_in',
-                    'wb+'
-                ) or die('Unable to open file!');
-                $txt = '1';
-                fwrite($myfile, $txt);
-                fclose($myfile);
-            } elseif (strtotime($user->expire_in) > time() && file_exists(BASE_PATH . '/storage/' . $user->id . '.expire_in')) {
-                unlink(BASE_PATH . '/storage/' . $user->id . '.expire_in');
+                $user->expired == true;
+                $user->save();
+            } elseif (strtotime($user->expire_in) > time() && $user->expired == true) {
+                $user->expired == false;
+                $user->save();
             }
 
 
@@ -628,21 +611,21 @@ class Job
             }
             if (Config::get('notify_limit_mode') != false) {
                 $user_traffic_left = $user->transfer_enable - $user->u - $user->d;
-                $under_limit = 'false';
+                $under_limit = false;
 
                 if ($user->transfer_enable != 0) {
                     if (Config::get('notify_limit_mode') == 'per' &&
                         $user_traffic_left / $user->transfer_enable * 100 < Config::get('notify_limit_value')) {
-                        $under_limit = 'true';
+                        $under_limit = true;
                         $unit_text = '%';
                     }
                 } elseif (Config::get('notify_limit_mode') == 'mb' &&
                     Tools::flowToMB($user_traffic_left) < Config::get('notify_limit_value')) {
-                    $under_limit = 'true';
+                    $under_limit = true;
                     $unit_text = 'MB';
                 }
 
-                if ($under_limit == 'true' && !file_exists(BASE_PATH . '/storage/traffic_notified/' . $user->id . '.userid')) {
+                if ($under_limit == true && !file_exists(BASE_PATH . '/storage/traffic_notified/' . $user->id . '.userid')) {
                     $subject = Config::get('appName') . ' - 您的剩余流量过低';
                     $to = $user->email;
                     $text = '您好,系统发现您剩余流量已经低于 ' . Config::get('notify_limit_value') . $unit_text . ' 。';
@@ -650,20 +633,15 @@ class Job
                         Mail::send($to, $subject, 'news/warn.tpl', [
                             'user' => $user,
                             'text' => $text
-                        ], [
                         ]);
-                        $myfile = fopen(
-                            BASE_PATH . '/storage/traffic_notified/' . $user->id . '.userid',
-                            'wb+'
-                        ) or die('Unable to open file!');
-                        $txt = '1';
-                        fwrite($myfile, $txt);
-                        fclose($myfile);
+                        $user->traffic_noticed == true;
+                        $user->save();
                     } catch (Exception $e) {
                         echo $e->getMessage();
                     }
-                } elseif (($under_limit == 'false') && file_exists(BASE_PATH . '/storage/traffic_notified/' . $user->id . '.userid')) {
-                    unlink(BASE_PATH . '/storage/traffic_notified/' . $user->id . '.userid');
+                } elseif ($under_limit == false && $user->traffic_noticed == true) {
+                    $user->traffic_noticed == false;
+                    $user->save();
                 }
             }
 
@@ -839,12 +817,12 @@ class Job
                             $notice_text = '喵喵喵~ ' . $node->name . ' 节点被墙了喵~';
                         }
                         Telegram::Send($notice_text);
-                        $file_node = fopen(BASE_PATH . '/storage/' . $node->id . '.gfw', 'wb+');
-                        fclose($file_node);
+                        $node->gfw_block = true;
+                        $node->save();
                     } else {
                         //没有被墙
                         echo($node->id . ':true' . PHP_EOL);
-                        if (file_exists(BASE_PATH . '/storage/' . $node->id . '.gfw') == false) {
+                        if ($node->gfw_block == false) {
                             continue;
                         }
                         foreach ($adminUser as $user) {
@@ -864,7 +842,8 @@ class Job
                             $notice_text = '喵喵喵~ ' . $node->name . ' 节点恢复了喵~';
                         }
                         Telegram::Send($notice_text);
-                        unlink(BASE_PATH . '/storage/' . $node->id . '.gfw');
+                        $node->gfw_block = false;
+                        $node->save();
                     }
                 }
                 break;

+ 12 - 0
src/Models/Bought.php

@@ -2,6 +2,18 @@
 
 namespace App\Models;
 
+/**
+ * Bought Model
+ *
+ * @property-read   int     $id         id
+ * @property        int     $userid     User id
+ * @property        int     $shopid     Shop id
+ * @property        string  $datetime   Bought complete datetime
+ * @property        int     $renew      Time to renew this bought
+ * @property        string  $coupon     Coupon applied to this bought
+ * @property        float   $price      Price after coupon applied
+ * @property        bool    $is_renewed If this bought is renewed
+ */
 class Bought extends Model
 {
     protected $connection = 'default';

+ 14 - 0
src/Models/Node.php

@@ -4,6 +4,20 @@ namespace App\Models;
 
 /**
  * Node Model
+ *
+ * @property-read   int     $id         id
+ * @property        string  $name       Display name
+ * @property        int     $type       If node display @todo Correct column name and type
+ * @property        string  $server     Domain
+ * @property        string  $method     Crypt method @deprecated
+ * @property        string  $info       Infomation
+ * @property        string  $status     Status description
+ * @property        int     $sort       Node type @todo Correct column name to `type`
+ * @property        int     $custom_method  Customs node crypt @deprecated
+ * @property        float   $traffic_rate   Node traffic rate
+ * @todo More property
+ * @property        bool    $online     If node is online
+ * @property        bool    $gfw_block  If node is blocked by GFW
  */
 
 use App\Utils\Tools;

+ 9 - 4
src/Models/User.php

@@ -2,10 +2,6 @@
 
 namespace App\Models;
 
-/**
- * User Model
- */
-
 use App\Utils\Tools;
 use App\Utils\Hash;
 use App\Services\Config;
@@ -14,6 +10,15 @@ use App\Utils\QQWry;
 use App\Utils\Radius;
 use Ramsey\Uuid\Uuid;
 
+/**
+ * User Model
+ *
+ * @property-read   int     $id         ID
+ * @todo More property
+ * @property        bool    $expired    If account expired
+ * @property        bool    $traffic_notified   If user is noticed for low traffic
+ */
+
 class User extends Model
 {
     protected $connection = 'default';

+ 173 - 0
utils/clean_job.php

@@ -0,0 +1,173 @@
+#!/usr/bin/env php
+<?php
+
+declare(strict_types=1);
+
+use App\Services\Boot;
+
+require __DIR__ . '/../app/predefine.php';
+require __DIR__ . '/../vendor/autoload.php';
+require __DIR__ . '/../config/.config.php';
+
+Boot::setTime();
+Boot::bootDb();
+
+$processed = [];
+$renew = [];
+$renew_c = function ($ids) use ($processed) {
+    echo 'Renew Process START.';
+    \App\Models\Bought::where('renew', '=', 0)->update(array('is_renewed' => true));
+    foreach ($ids as $id) {
+        $bought = \App\Models\Bought::find($id);
+        if ($bought == null) {
+            echo 'Bought not found:' . $id . PHP_EOL;
+            unlink(__DIR__ . '/../storage/' . $id . 'renew');
+            $processed['renew'] = $id;
+        } else {
+            $bought->is_renewed = true;
+            if ($bought->save() == true) {
+                unlink(__DIR__ . '/../storage/' . $id . 'renew');
+                echo 'Renew Process successed for bought' . $id . PHP_EOL;
+                $processed['renew'] = $id;
+            }
+        }
+    }
+    echo 'Renew Process END.' . PHP_EOL . PHP_EOL;
+};
+
+$offline = [];
+$offline_c = function ($ids) use ($processed) {
+    echo 'Offline Process START.';
+    foreach ($ids as $id) {
+        $node = \App\Models\Node::find($id);
+        if ($node == null) {
+            echo 'Node not found:' . $id . PHP_EOL;
+            unlink(__DIR__ . '/../storage/' . $id . 'offline');
+            $processed['offline'] = $id;
+        } else {
+            $node->offline = true;
+            if ($node->save() == true) {
+                unlink(__DIR__ . '/../storage/' . $id . 'offline');
+                echo 'Offline Process successed for node' . $id . PHP_EOL;
+                $processed['offline'] = $id;
+            }
+        }
+    }
+    echo 'Offline Process END.' . PHP_EOL . PHP_EOL;
+};
+
+$expire = [];
+$expire_c = function ($ids) use ($processed) {
+    echo 'Expire Process START.';
+    foreach ($ids as $id) {
+        $user = \App\Models\User::find($id);
+        if ($user == null) {
+            echo 'User not found:' . $id . PHP_EOL;
+            unlink(__DIR__ . '/../storage/' . $id . 'expire_in');
+            $processed['expire'] = $id;
+        } else {
+            $user->online = false;
+            if ($user->save() == true) {
+                unlink(__DIR__ . '/../storage/' . $id . 'expire_in');
+                echo 'Expire Process successed for user' . $id . PHP_EOL;
+                $processed['expire'] = $id;
+            }
+        }
+    }
+    echo 'Expire Process END.' . PHP_EOL . PHP_EOL;
+};
+
+$gfw = [];
+$gfw_c = function ($ids) use ($processed) {
+    echo 'GFW Process START.';
+    foreach ($ids as $id) {
+        $node = \App\Models\Node::find($id);
+        if ($node == null) {
+            echo 'Node not found:' . $id . PHP_EOL;
+            unlink(__DIR__ . '/../storage/' . $id . 'gfw');
+            $processed['gfw'] = $id;
+        } else {
+            $node->gfw_block = true;
+            if ($node->save() == true) {
+                unlink(__DIR__ . '/../storage/' . $id . 'gfw');
+                echo 'GFW Process successed for node' . $id . PHP_EOL;
+                $processed['gfw'] = $id;
+            }
+        }
+    }
+    echo 'GFW Process END.' . PHP_EOL . PHP_EOL;
+};
+
+$files = scandir(__DIR__ . '/../storage');
+foreach ($files as $origin_file) {
+    $file = explode('.', $origin_file);
+    if (count($file) == 2 && is_numeric($file[0])) {
+        switch ($file[1]) {
+            case 'renew':
+                $renew[] = $file[0];
+                break;
+            case 'offline':
+                $offline[] = $file[0];
+                break;
+            case 'expire_in':
+                $expire[] = $file[0];
+                break;
+            case 'gfw':
+                $gfw[] = $file[0];
+                break;
+            default:
+                echo 'Unrecognized file: ' . $origin_file . PHP_EOL;
+        }
+    } else {
+        continue;
+    }
+}
+
+$renew_c($renew);
+$offline_c($offline);
+$expire_c($expire);
+$gfw_c($gfw);
+
+if (file_exists(__DIR__ . '/../storage/traffic_notified') == true) {
+    $files = scandir(__DIR__ . '/../storage/traffic_notified');
+    if ($files != false) {
+        foreach ($files as $origin_file) {
+            $file = explode('.', $origin_file);
+            if (count($file) == 2 && is_numeric($file[0] && $file[1] == 'userid')) {
+                        $notified[] = $file[0];
+            } else {
+                echo 'Unrecognized file: ' . $origin_file . PHP_EOL;
+            }
+        }
+        file_put_contents(__DIR__ . '/notified.json', json_encode($file));
+    }
+    $notified_c = function ($ids) use ($processed) {
+        echo 'Notified Process START.';
+        foreach ($ids as $id) {
+            $user = \App\Models\User::find($id);
+            if ($user == null) {
+                echo 'User not found:' . $id . PHP_EOL;
+                unlink(__DIR__ . '/../storage/traffic_notified/' . $id . 'gfw');
+                $processed['notified'] = $id;
+            } else {
+                $user->traffic_notified = true;
+                if ($user->save() == true) {
+                    unlink(__DIR__ . '/../storage/traffic_notified/' . $id . 'gfw');
+                    echo 'Notified Process successed for node' . $id . PHP_EOL;
+                    $processed['notified'] = $id;
+                }
+            }
+        }
+        echo 'Notified Process END.' . PHP_EOL . PHP_EOL;
+    };
+} else {
+    echo 'Notified Process Nothing to do.' . PHP_EOL . PHP_EOL;
+}
+file_put_contents(__DIR__ . '/processed.json', json_encode($processed));
+file_put_contents(__DIR__ . '/raw.json', json_encode([
+    'renew' => $renew,
+    'offline' => $offline,
+    'expire' => $expire,
+    'gfw' => $gfw,
+]));
+exit(0);