Skip to content

Commit

Permalink
Execute files from Scheduler
Browse files Browse the repository at this point in the history
maca134#4

Also ArgumentOutOfRangeException handler for Start times that are to
soon.
  • Loading branch information
LexTheGreat committed May 5, 2018
1 parent 9089f07 commit 62afe5d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 26 deletions.
37 changes: 15 additions & 22 deletions src/Plugins/ScheduledTasks/Schedule.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,26 @@

start: Either be a time (hh:mm:ss) or a number
- When formatted as a time, the task will run once a day at that time
- When formatted as a number, the task will run x seconds after MBCon starts
- When formatted as a number, the task will run x seconds after MBCon starts (Will get error if start is to soon. Use numbers >= 10)

cmd: A valid BattlEye command to run on the server.
cmd: A valid BattlEye command to run on the server. (Optional)
(You want at one of them tho...)
execute: Full path to .bat or other executable file. (Optional)

interval: Numbers of seconds to wait before running the task again (does not apply on time-based tasks)

loop: Number of times the task will run. Set to -1 to loop forever. (Default: -1)
*/
[
{
"start": "10",
"interval": "10",
"loop": 3,
"cmd": "say -1 10 10 3."
},
{
"start": "5",
"interval": "10",
"cmd": "say -1 5 10."
},
{
"start": "40",
"loop": 1,
"cmd": "say -1 40 1."
},
{
"start": "23:45:00",
"cmd": "say -1 Message will run at 23:45"
}
{
"start": "01:00:00",
"execute": "C:/full/path/to/file/test.bat",
"cmd": "say -1 Open bat file every day at 1am."
},
{
"start": "10",
"loop": 1,
"execute": "C:/full/path/to/file/test.bat",
"cmd": "say -1 Opend test.bat"
}
]
35 changes: 33 additions & 2 deletions src/Plugins/ScheduledTasks/ScheduledTasks.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Humanizer;
Expand Down Expand Up @@ -115,7 +116,18 @@ private void TaskMonitor()
{
t = Task.Next;
}
_waitHandle.WaitOne(t.TimeSpan);

if (t == null)
continue;

try {
_waitHandle.WaitOne(t.TimeSpan);
} catch(ArgumentOutOfRangeException ex) {
throw new ScheduledTasksException(String.Format("t.TimeSpan is falling behind. (Start field is to soon?){0}", ex));
} catch(Exception ex) {
throw new ScheduledTasksException(String.Format("{0}", ex));
}

if (_workerTerminateSignal)
break;
if (_debug)
Expand All @@ -124,7 +136,26 @@ private void TaskMonitor()
}
else
{
_api.SendCommand(t.Command);
if (t.Execute != null) {
if (File.Exists(t.Execute.Trim().Replace("/", @"\"))) {
AppConsole.Log(String.Format("Running Executefile: {0}", ConsoleColor.DarkMagenta));

ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = t.Execute.Trim().Replace("/", @"\");
processInfo.Arguments = "";
processInfo.CreateNoWindow = false;
processInfo.UseShellExecute = true;
processInfo.RedirectStandardError = false;
processInfo.RedirectStandardOutput = false;

Process process = Process.Start(processInfo);
} else {
AppConsole.Log(String.Format("Executefile doesn't exist: {0}", t.Execute.Trim().Replace("/", @"\")), ConsoleColor.DarkMagenta);
}
}

if (t.Command != null)
_api.SendCommand(t.Command);
}
lock (Task.Tasks)
{
Expand Down
26 changes: 24 additions & 2 deletions src/Plugins/ScheduledTasks/Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public static void LoadFromJSON(string jsonPath)
{
i++;
string start;
string execute;
Command cmd;
DateTime nextRun;
int interval;
Expand All @@ -164,13 +165,25 @@ public static void LoadFromJSON(string jsonPath)
throw new ScheduledTasksException(String.Format("Could not parse command: {0}", ex.Message));
}

try {
if (item.execute != null) {
execute = item.execute.ToString();
} else { execute = null; }
} catch (CommandException ex) {
AppConsole.Log(String.Format("Could not parse execute: {0}", ex.Message), ConsoleColor.Red);
execute = "";
}

try
{
cmd = new Command(item.cmd.ToString());
if (item.cmd != null) {
cmd = new Command(item.cmd.ToString());
} else { cmd = null; }
}
catch (CommandException ex)
{
throw new ScheduledTasksException(String.Format("Could not parse command: {0}", ex.Message));
AppConsole.Log(String.Format("Could not parse command: {0}", ex.Message), ConsoleColor.Red);
cmd = null;
}

try
Expand Down Expand Up @@ -225,6 +238,7 @@ public static void LoadFromJSON(string jsonPath)

var task = new Task()
{
_execute = execute,
_command = cmd,
_nextRun = nextRun,
_interval = TimeSpan.FromSeconds(interval),
Expand Down Expand Up @@ -368,6 +382,8 @@ public int Loop
}

private Command _command;
private string _execute;

public Command Command
{
get
Expand All @@ -376,6 +392,12 @@ public Command Command
}
}

public string Execute {
get {
return _execute;
}
}

public void Increment()
{
if (_loop > 0)
Expand Down
3 changes: 3 additions & 0 deletions src/Plugins/ScheduledTasks/test.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
echo it worked
pause

0 comments on commit 62afe5d

Please sign in to comment.