Completed
Push — master ( 481865...ed4291 )
by John
24s queued 10s
created

Kernel::schedule()   C

Complexity

Conditions 10
Paths 4

Size

Total Lines 88
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 1 Features 2
Metric Value
cc 10
eloc 63
c 8
b 1
f 2
nc 4
nop 1
dl 0
loc 88
rs 6.9406

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Console;
4
5
use Illuminate\Console\Scheduling\Schedule;
6
use App\Babel\Babel;
7
use App\Models\Eloquent\JudgeServerModel as EloquentJudgeServerModel;
8
use App\Models\RankModel;
9
use App\Models\SiteMapModel;
10
use App\Models\ContestModel;
11
use App\Models\GroupModel;
12
use App\Models\OJModel;
13
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
14
use Log;
15
use Cache;
16
17
class Kernel extends ConsoleKernel
18
{
19
    /**
20
     * The Artisan commands provided by your application.
21
     *
22
     * @var array
23
     */
24
    protected $commands=[
25
        //
26
    ];
27
28
    /**
29
     * Define the application's command schedule.
30
     *
31
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
32
     * @return void
33
     */
34
    protected function schedule(Schedule $schedule)
35
    {
36
        $schedule->call(function () {
37
            $babel=new Babel();
38
            for ($i=1; $i<=12; $i++) {
39
                $babel->judge();
40
                sleep(5);
41
            }
42
            // file_put_contents(storage_path('app/task-schedule.output'),"Successfully Synced Judger");
43
        })->everyMinute()->description("Sync Judger");
44
45
        $schedule->call(function () {
46
            $rankModel=new RankModel();
47
            $rankModel->rankList();
48
            // file_put_contents(storage_path('app/task-schedule.output'),"Successfully Updated Rank");
49
        })->dailyAt('02:00')->description("Update Rank");
50
51
        $schedule->call(function () {
52
            $siteMapModel=new SiteMapModel();
0 ignored issues
show
Unused Code introduced by
The assignment to $siteMapModel is dead and can be removed.
Loading history...
53
            // file_put_contents(storage_path('app/task-schedule.output'),"Successfully Updated SiteMap");
54
        })->dailyAt('02:00')->description("Update SiteMap");
55
56
        $schedule->call(function () {
57
            $groupModel=new GroupModel();
58
            $groupModel->cacheTrendingGroups();
59
            // file_put_contents(storage_path('app/task-schedule.output'),"Successfully Cached Trending Groups");
60
        })->dailyAt('03:00')->description("Update Trending Groups");
61
62
        $schedule->call(function () {
63
            $contestModel = new ContestModel();
64
            $syncList = $contestModel->runningContest();
65
            foreach ($syncList as $syncContest) {
66
                if (!isset($syncContest['vcid'])) {
67
                    $contestRankRaw=$contestModel->contestRankCache($syncContest['cid']);
68
                    $cid=$syncContest['cid'];
69
                    Cache::tags(['contest', 'rank'])->put($cid, $contestRankRaw);
70
                    Cache::tags(['contest', 'rank'])->put("contestAdmin$cid", $contestRankRaw);
71
                    continue ;
72
                }
73
                $className = "App\\Babel\\Extension\\hdu\\Synchronizer";  // TODO Add OJ judgement.
74
                $all_data = [
75
                    'oj'=>"hdu",
76
                    'vcid'=>$syncContest['vcid'],
77
                    'gid'=>$syncContest['gid'],
78
                    'cid'=>$syncContest['cid'],
79
                ];
80
                $hduSync = new $className($all_data);
81
                $hduSync->crawlRank();
82
                $hduSync->crawlClarification();
83
            }
84
            // file_put_contents(storage_path('app/task-schedule.output'),"Successfully Synced Remote Rank and Clarification");
85
        })->everyMinute()->description("Sync Remote Rank and Clarification");
86
87
        $schedule->call(function () {
88
            $contestModel = new ContestModel();
89
            $syncList = $contestModel->runningContest();
90
            foreach ($syncList as $syncContest) {
91
                if (isset($syncContest['crawled'])) {
92
                    if (!$syncContest['crawled']) {
93
                        $className = "App\\Babel\\Extension\\hdu\\Synchronizer";
94
                        $all_data = [
95
                            'oj'=>"hdu",
96
                            'vcid'=>$syncContest['vcid'],
97
                            'gid'=>$syncContest['gid'],
98
                            'cid'=>$syncContest['cid'],
99
                        ];
100
                        $hduSync = new $className($all_data);
101
                        $hduSync->scheduleCrawl();
102
                        $contestModel->updateCrawlStatus($syncContest['cid']);
103
                    }
104
                }
105
            }
106
        })->everyMinute()->description("Sync Contest Problem");
107
108
        $schedule->call(function () {
109
            $oidList=EloquentJudgeServerModel::column('oid');
110
            $babel=new Babel();
111
            foreach ($oidList as $oid) {
112
                $babel->monitor(["name"=>OJMOdel::ocode($oid)]);
113
            }
114
        })->everyMinute()->description("Update Judge Server Status");
115
116
        if (!env("APP_DEBUG")) {
117
            $schedule->command('backup:run')->weekly()->description("BackUp Site");
118
        }
119
120
        if (!env("APP_DEBUG")) {
121
            $schedule->command('backup:run --only-db')->dailyAt('00:30')->description("BackUp DataBase");
122
        }
123
    }
124
125
    /**
126
     * Register the commands for the application.
127
     *
128
     * @return void
129
     */
130
    protected function commands()
131
    {
132
        $this->load(__DIR__.'/Commands');
133
134
        require base_path('routes/console.php');
135
    }
136
}
137