forked from Alexr03/TCAdminCronsModule
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCAdminCronsService.cs
More file actions
122 lines (103 loc) · 3.5 KB
/
TCAdminCronsService.cs
File metadata and controls
122 lines (103 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using FluentScheduler;
using TCAdmin.GameHosting.SDK.Objects;
using TCAdmin.Interfaces.Logging;
using TCAdmin.Interfaces.Server;
using TCAdmin.SDK;
using TCAdminCrons.Configuration;
using TCAdminCrons.Crons.GameUpdates;
using TCAdminCrons.Models.Objects;
namespace TCAdminCrons
{
public class TcAdminCronsService : IMonitorService
{
public static object CronLock = new object();
public static Registry CronRegistry;
public TcAdminCronsService()
{
this.ConfigurationKey = "TCAdminCrons";
}
public void Initialize(params object[] args)
{
}
public void Start()
{
LogManager.Write("Starting TCAdmin Crons...", LogType.Console);
System.Threading.Tasks.Task.Run(() =>
{
var createdThreadConnection = TCAdmin.SDK.Database.DatabaseManager.CreateDatabaseManagerForThread();
try
{
this.Status = ServiceStatus.Starting;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
LogManager.Write(
"TCAdmin Crons - By Alexr03 | Contributors: M0RG4N01 | Version: " +
Assembly.GetExecutingAssembly().GetName().Version, LogType.Console);
CronRegistry = new Registry();
RegisterCrons();
Status = ServiceStatus.Running;
}
catch
{
if (createdThreadConnection)
{
TCAdmin.SDK.Database.DatabaseManager.DisconnectThreadConnection();
}
Status = ServiceStatus.StartError;
}
});
LogManager.Write("TCAdmin Crons has started.", LogType.Console);
}
public void Stop()
{
this.Status = ServiceStatus.Stopping;
JobManager.RemoveAllJobs();
JobManager.Stop();
this.Status = ServiceStatus.Stopped;
}
public void Pause()
{
this.Stop();
}
public void Resume()
{
this.Start();
}
public void Restart()
{
this.Stop();
this.Start();
}
public static void RegisterCrons()
{
var cronJobs = CronJob.GetCronJobs();
LogManager.Write("Initializing Cron Registry", LogType.Console);
CronRegistry.NonReentrantAsDefault();
foreach (var cronJob in cronJobs)
{
try
{
CronRegistry.Schedule(cronJob.Create<TcAdminCronJob>()).ToRunNow().AndEvery(cronJob.ExecuteEverySeconds).Seconds();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
JobManager.Initialize(CronRegistry);
foreach (var schedule in JobManager.AllSchedules)
{
LogManager.Write($"{schedule.Name} has been scheduled to run at {schedule.NextRun:F}",
LogType.Console);
}
}
public string ConfigurationKey { get; }
public ServiceStatus Status { get; set; }
}
}