|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Requests, Exception; |
|
8
|
|
|
|
|
9
|
|
|
class JudgerModel extends Model |
|
10
|
|
|
{ |
|
11
|
|
|
protected $tableName='judger'; |
|
12
|
|
|
|
|
13
|
|
|
public function list($oid=2) |
|
14
|
|
|
{ |
|
15
|
|
|
$judger_list=DB::table($this->tableName)->where(["oid"=>$oid, "available"=>1, 'using'=>0])->get(); |
|
16
|
|
|
return $judger_list; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function updateStatus($jid, $status) |
|
20
|
|
|
{ |
|
21
|
|
|
$judger_list=DB::table($this->tableName)->where(['jid'=>$jid])->update(['using'=>$status]); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function server($oid=1) |
|
25
|
|
|
{ |
|
26
|
|
|
$serverList=DB::table("judge_server")->where(["oid"=>$oid, "available"=>1])->get()->all(); |
|
27
|
|
|
// return $serverList[0]; |
|
28
|
|
|
$bestServer=[ |
|
29
|
|
|
"load"=> 99999, |
|
30
|
|
|
"server" => null |
|
31
|
|
|
]; |
|
32
|
|
|
foreach ($serverList as $server) { |
|
33
|
|
|
$serverURL="http://".$server["host"].":".$server["port"]; |
|
34
|
|
|
try { |
|
35
|
|
|
$pong=$this->ping($serverURL.'/ping', $server["port"], hash('sha256', $server["token"])); |
|
36
|
|
|
} catch (Exception $exception) { |
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (empty($pong)) { |
|
41
|
|
|
continue; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($pong["status_code"]==200) { |
|
45
|
|
|
$pong=$pong["body"]; |
|
46
|
|
|
$load=4 * $pong->data->cpu+0.6 * $pong->data->memory; |
|
47
|
|
|
if ($load<$bestServer['load']) { |
|
48
|
|
|
$bestServer=[ |
|
49
|
|
|
'server' => $server, |
|
50
|
|
|
'load' => $load |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
return $bestServer["server"]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function ping($url, $port, $token) |
|
59
|
|
|
{ |
|
60
|
|
|
$curl=curl_init(); |
|
61
|
|
|
|
|
62
|
|
|
curl_setopt_array($curl, array( |
|
63
|
|
|
CURLOPT_PORT => $port, |
|
64
|
|
|
CURLOPT_URL => $url, |
|
65
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
66
|
|
|
CURLOPT_ENCODING => "", |
|
67
|
|
|
CURLOPT_MAXREDIRS => 10, |
|
68
|
|
|
CURLOPT_TIMEOUT => 30, |
|
69
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
70
|
|
|
CURLOPT_CUSTOMREQUEST => "POST", |
|
71
|
|
|
CURLOPT_POSTFIELDS => "", |
|
72
|
|
|
CURLOPT_HTTPHEADER => array( |
|
73
|
|
|
"Content-Type: application/json", |
|
74
|
|
|
"X-Judge-Server-Token: ".$token, |
|
75
|
|
|
"cache-control: no-cache" |
|
76
|
|
|
), |
|
77
|
|
|
)); |
|
78
|
|
|
|
|
79
|
|
|
$response=curl_exec($curl); |
|
80
|
|
|
$err=curl_error($curl); |
|
81
|
|
|
$httpCode=curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
82
|
|
|
|
|
83
|
|
|
curl_close($curl); |
|
84
|
|
|
|
|
85
|
|
|
if ($err) { |
|
86
|
|
|
return []; |
|
87
|
|
|
} else { |
|
88
|
|
|
return [ |
|
89
|
|
|
"status_code"=>$httpCode, |
|
90
|
|
|
"body"=>json_decode($response) |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|