File tree Expand file tree Collapse file tree 2 files changed +88
-0
lines changed
Expand file tree Collapse file tree 2 files changed +88
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * This file is part of SWBF2Admin (https://github.com/jweigelt/swbf2admin).
3+ * Copyright(C) 2017, 2018 Jan Weigelt <jan@lekeks.de>
4+ *
5+ * SWBF2Admin is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU General Public License as published by
7+ * the Free Software Foundation, either version 3 of the License, or
8+ * (at your option) any later version.
9+
10+ * SWBF2Admin is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
13+ * GNU General Public License for more details.
14+
15+ * You should have received a copy of the GNU General Public License
16+ * along with SWBF2Admin. If not, see<http://www.gnu.org/licenses/>.
17+ */
18+ using System ;
19+
20+ namespace SWBF2Admin . Gameserver
21+ {
22+ public enum ServerStopReason
23+ {
24+ STOP_EXIT ,
25+ STOP_RESTART
26+ }
27+
28+ class StopEventArgs : EventArgs
29+ {
30+ public ServerStopReason Reason { get ; }
31+ public StopEventArgs ( ServerStopReason reason )
32+ {
33+ Reason = reason ;
34+ }
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ using System ;
2+ using SWBF2Admin . Config ;
3+ using SWBF2Admin . Utility ;
4+
5+ namespace SWBF2Admin . Runtime . Watchdog
6+ {
7+ public class EmptyRestart : ComponentBase
8+ {
9+ private DateTime lastSeenNotEmpty ;
10+ private int restartThreshold ;
11+ private bool isRestarting ;
12+
13+ public EmptyRestart ( AdminCore core ) : base ( core ) { }
14+
15+ public override void Configure ( CoreConfiguration config )
16+ {
17+ UpdateInterval = config . EmptyRestartCheckInterval ;
18+ restartThreshold = config . EmptyRestartThreshold ;
19+ }
20+
21+ public override void OnInit ( )
22+ {
23+ base . OnInit ( ) ;
24+ }
25+
26+ public override void OnServerStart ( EventArgs e )
27+ {
28+ EnableUpdates ( ) ;
29+ isRestarting = false ;
30+ lastSeenNotEmpty = DateTime . Now ;
31+ }
32+
33+ public override void OnServerStop ( )
34+ {
35+ DisableUpdates ( ) ;
36+ }
37+
38+ protected override void OnUpdate ( )
39+ {
40+ if ( Core . Players . PlayerList . Count > 0 )
41+ {
42+ lastSeenNotEmpty = DateTime . Now ;
43+ }
44+ else if ( ! isRestarting && ( DateTime . Now - lastSeenNotEmpty ) . TotalSeconds > restartThreshold )
45+ {
46+ Logger . Log ( LogLevel . Info , "Server has been empty for {0} seconds - restarting it" , restartThreshold . ToString ( ) ) ;
47+ Core . Server . Restart ( ) ;
48+ isRestarting = true ; //make sure we don't try to restart twice
49+ }
50+ }
51+ }
52+ }
You can’t perform that action at this time.
0 commit comments