1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
use Requests; |
8
|
|
|
use Exception; |
9
|
|
|
|
10
|
|
|
class JudgerModel extends Model |
11
|
|
|
{ |
12
|
|
|
protected $tableName='judger'; |
13
|
|
|
public static $status=[ |
14
|
|
|
"-2"=>[ |
15
|
|
|
"text"=>"Unavailable", |
16
|
|
|
"icon"=>"close-circle", |
17
|
|
|
"color"=>"wemd-pink-text", |
18
|
|
|
], |
19
|
|
|
"-1"=>[ |
20
|
|
|
"text"=>"Unknown", |
21
|
|
|
"icon"=>"help-circle", |
22
|
|
|
"color"=>"wemd-grey-text", |
23
|
|
|
], |
24
|
|
|
"0"=>[ |
25
|
|
|
"text"=>"Operational", |
26
|
|
|
"icon"=>"check-circle", |
27
|
|
|
"color"=>"wemd-teal-text", |
28
|
|
|
], |
29
|
|
|
"1"=>[ |
30
|
|
|
"text"=>"Critical", |
31
|
|
|
"icon"=>"alert-circle", |
32
|
|
|
"color"=>"wemd-amber-text", |
33
|
|
|
], |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public function list($oid=2) |
37
|
|
|
{ |
38
|
|
|
$judger_list=DB::table($this->tableName)->where(["oid"=>$oid, "available"=>1])->get()->all(); |
39
|
|
|
return $judger_list; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function detail($jid) |
43
|
|
|
{ |
44
|
|
|
$judger_list=DB::table($this->tableName)->where(["jid"=>$jid])->get()->first(); |
45
|
|
|
return $judger_list; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function contestJudger($vcid) { |
49
|
|
|
return DB::table("contest_judger")->where(["vcid"=>$vcid, "available"=>1])->get()->all(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function contestJudgerDetail($jid) { |
53
|
|
|
return DB::table("contest_judger")->where("jid", $jid)->get()->first(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function server($oid=1) |
57
|
|
|
{ |
58
|
|
|
$serverList=DB::table("judge_server")->where(["oid"=>$oid, "available"=>1])->get()->all(); |
59
|
|
|
// return $serverList[0]; |
60
|
|
|
$bestServer=[ |
61
|
|
|
"load"=> 99999, |
62
|
|
|
"server" => null |
63
|
|
|
]; |
64
|
|
|
foreach ($serverList as $server) { |
65
|
|
|
$serverURL="http://".$server["host"].":".$server["port"]; |
66
|
|
|
try { |
67
|
|
|
$pong=$this->ping($serverURL.'/ping', $server["port"], hash('sha256', $server["token"])); |
68
|
|
|
} catch (Exception $exception) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (empty($pong)) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($pong["status_code"]==200) { |
77
|
|
|
$pong=$pong["body"]; |
78
|
|
|
$load=4 * $pong->data->cpu+0.6 * $pong->data->memory; |
79
|
|
|
if ($load<$bestServer['load']) { |
80
|
|
|
$bestServer=[ |
81
|
|
|
'server' => $server, |
82
|
|
|
'load' => $load |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
return $bestServer["server"]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function fetchServer($oid=1) |
91
|
|
|
{ |
92
|
|
|
$serverList=DB::table("judge_server")->where(["oid"=>$oid])->get()->all(); |
93
|
|
|
foreach ($serverList as &$server) { |
94
|
|
|
$server["status_parsed"]=is_null($server["status"])?self::$status["-1"]:self::$status[$server["status"]]; |
95
|
|
|
} |
96
|
|
|
return $serverList; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function updateServerStatus($oid=1) |
100
|
|
|
{ |
101
|
|
|
$serverList=DB::table("judge_server")->where(["oid"=>$oid])->get()->all(); |
102
|
|
|
foreach ($serverList as $server) { |
103
|
|
|
if($server["available"]==0){ |
104
|
|
|
DB::table("judge_server")->where(["jsid"=>$server["jsid"]])->update([ |
105
|
|
|
"status"=>-2, |
106
|
|
|
"status_update_at"=>date("Y-m-d H:i:s") |
107
|
|
|
]); |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$serverURL="http://".$server["host"].":".$server["port"]; |
112
|
|
|
try { |
113
|
|
|
$pong=$this->ping($serverURL.'/ping', $server["port"], hash('sha256', $server["token"])); |
114
|
|
|
} catch (Exception $exception) { |
115
|
|
|
DB::table("judge_server")->where(["jsid"=>$server["jsid"]])->update([ |
116
|
|
|
"status"=>1, |
117
|
|
|
"status_update_at"=>date("Y-m-d H:i:s") |
118
|
|
|
]); |
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (empty($pong)) { |
123
|
|
|
DB::table("judge_server")->where(["jsid"=>$server["jsid"]])->update([ |
124
|
|
|
"status"=>1, |
125
|
|
|
"status_update_at"=>date("Y-m-d H:i:s") |
126
|
|
|
]); |
127
|
|
|
continue; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($pong["status_code"]==200) { |
131
|
|
|
DB::table("judge_server")->where(["jsid"=>$server["jsid"]])->update([ |
132
|
|
|
"status"=>0, |
133
|
|
|
"status_update_at"=>date("Y-m-d H:i:s") |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function ping($url, $port, $token) |
140
|
|
|
{ |
141
|
|
|
$curl=curl_init(); |
142
|
|
|
|
143
|
|
|
if($curl===false) return []; |
144
|
|
|
|
145
|
|
|
curl_setopt_array($curl, array( |
146
|
|
|
CURLOPT_PORT => $port, |
147
|
|
|
CURLOPT_URL => $url, |
148
|
|
|
CURLOPT_RETURNTRANSFER => true, |
149
|
|
|
CURLOPT_ENCODING => "", |
150
|
|
|
CURLOPT_MAXREDIRS => 10, |
151
|
|
|
CURLOPT_TIMEOUT => 30, |
152
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
153
|
|
|
CURLOPT_CUSTOMREQUEST => "POST", |
154
|
|
|
CURLOPT_POSTFIELDS => "", |
155
|
|
|
CURLOPT_HTTPHEADER => array( |
156
|
|
|
"Content-Type: application/json", |
157
|
|
|
"X-Judge-Server-Token: ".$token, |
158
|
|
|
"cache-control: no-cache" |
159
|
|
|
), |
160
|
|
|
)); |
161
|
|
|
|
162
|
|
|
$response=curl_exec($curl); |
163
|
|
|
$err=curl_error($curl); |
164
|
|
|
$httpCode=curl_getinfo($curl, CURLINFO_HTTP_CODE); |
165
|
|
|
|
166
|
|
|
curl_close($curl); |
167
|
|
|
|
168
|
|
|
if ($err) { |
169
|
|
|
return []; |
170
|
|
|
} else { |
171
|
|
|
return [ |
172
|
|
|
"status_code"=>$httpCode, |
173
|
|
|
"body"=>json_decode($response) |
174
|
|
|
]; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|