Add project files.
This commit is contained in:
19
ProcessInfo.cs
Normal file
19
ProcessInfo.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace marketally.processmonitor
|
||||
{
|
||||
class ProcessInfo
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Path { get; set; }
|
||||
public Int32 Count { get; set; }
|
||||
public string Time { get; set; }
|
||||
public int? Interval { get; set; }
|
||||
public bool Enable { get; set; }
|
||||
public DateTime? LastRun { get; set; }
|
||||
}
|
||||
}
|
||||
136
Program.cs
Normal file
136
Program.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using marketally.processmonitor;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Diagnostics;
|
||||
|
||||
Console.WriteLine("Do you want to start processes in a new window? (yes/no)");
|
||||
string userInput = Console.ReadLine().ToLower();
|
||||
bool openInNewWindow = userInput == "yes";
|
||||
|
||||
double checkOnce = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
string jsonFilePath = "processlist.json"; // Relative path to the JSON file
|
||||
string json = File.ReadAllText(jsonFilePath);
|
||||
var jObject = JObject.Parse(json);
|
||||
var processInfos = jObject["processes"].ToObject<List<ProcessInfo>>();
|
||||
|
||||
foreach (var processInfo in processInfos.Where(x => x.Enable))
|
||||
{
|
||||
if (checkOnce == 0 && processInfo.Interval.HasValue)
|
||||
{
|
||||
// Schedule repeated execution
|
||||
ScheduleRepeatedExecution(processInfo);
|
||||
}
|
||||
else if (string.IsNullOrEmpty(processInfo.Time))
|
||||
{
|
||||
// Run continuously
|
||||
EnsureProcessRunning(processInfo, processInfo.Count);
|
||||
}
|
||||
else if (checkOnce == 0)
|
||||
{
|
||||
// Schedule for a specific time
|
||||
ScheduleProcessStart(processInfo);
|
||||
}
|
||||
}
|
||||
if (checkOnce == 0)
|
||||
checkOnce = 1;
|
||||
Thread.Sleep(10000); // Wait for 10 seconds before checking again
|
||||
}
|
||||
|
||||
void EnsureProcessRunning(ProcessInfo processInfo, int desiredCount)
|
||||
{
|
||||
var runningProcesses = Process.GetProcessesByName(processInfo.Name);
|
||||
int countToStart = desiredCount - runningProcesses.Length;
|
||||
|
||||
for (int i = 0; i < countToStart; i++)
|
||||
{
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = processInfo.Path,
|
||||
CreateNoWindow = !openInNewWindow, // Controlled by the user's choice
|
||||
UseShellExecute = true, // Use the system shell to start the process
|
||||
};
|
||||
|
||||
Process.Start(startInfo);
|
||||
Console.WriteLine($"Started {processInfo.Name} at {DateTime.Now}");
|
||||
AppendToLogFile(processInfo, "launched");
|
||||
}
|
||||
}
|
||||
|
||||
void ScheduleProcessStart(ProcessInfo processInfo)
|
||||
{
|
||||
DateTime scheduledTime = DateTime.Today.Add(TimeSpan.Parse(processInfo.Time));
|
||||
TimeSpan delay = scheduledTime - DateTime.Now;
|
||||
|
||||
if (delay < TimeSpan.Zero)
|
||||
{
|
||||
// Scheduled time is in the past. Schedule for the next day or handle as needed.
|
||||
delay = delay.Add(TimeSpan.FromDays(1));
|
||||
}
|
||||
|
||||
var timer = new System.Threading.Timer(_ =>
|
||||
{
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = processInfo.Path,
|
||||
CreateNoWindow = !openInNewWindow, // Controlled by the user's choice
|
||||
UseShellExecute = true, // Use the system shell to start the process
|
||||
};
|
||||
Process.Start(startInfo);
|
||||
AppendToLogFile(processInfo, "run");
|
||||
}, null, delay, Timeout.InfiniteTimeSpan); // Run only once
|
||||
|
||||
Console.WriteLine($"Scheduled {processInfo.Name} to start at {scheduledTime}");
|
||||
}
|
||||
|
||||
void ScheduleRepeatedExecution(ProcessInfo processInfo)
|
||||
{
|
||||
var timer = new System.Threading.Timer(_ =>
|
||||
{
|
||||
var runningProcesses = Process.GetProcessesByName(processInfo.Name);
|
||||
if (runningProcesses.Length == 0)
|
||||
{
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = processInfo.Path,
|
||||
CreateNoWindow = !openInNewWindow, // Controlled by the user's choice
|
||||
UseShellExecute = true, // Use the system shell to start the process
|
||||
};
|
||||
Process.Start(startInfo);
|
||||
Console.WriteLine($"Started {processInfo.Name} at {DateTime.Now}");
|
||||
AppendToLogFile(processInfo, "run");
|
||||
} else
|
||||
{
|
||||
Console.WriteLine($"Process still running {processInfo.Name} at {DateTime.Now}, skipping for next interval");
|
||||
AppendToLogFile(processInfo, "skipped");
|
||||
}
|
||||
}, null, TimeSpan.Zero, TimeSpan.FromMinutes(processInfo.Interval.Value));
|
||||
|
||||
Console.WriteLine($"Scheduled {processInfo.Name} to run every {processInfo.Interval.Value} minutes");
|
||||
}
|
||||
|
||||
void AppendToLogFile(ProcessInfo processInfo, string state)
|
||||
{
|
||||
string logFilePath = Path.Combine("Logs", GetLogFileName()); // Store logs in a "Logs" subfolder
|
||||
string logEntry = $"{DateTime.Now}: {processInfo.Name} was {state}.";
|
||||
|
||||
if (!Directory.Exists("Logs"))
|
||||
{
|
||||
Directory.CreateDirectory("Logs");
|
||||
}
|
||||
|
||||
File.AppendAllText(logFilePath, logEntry + Environment.NewLine);
|
||||
}
|
||||
|
||||
string GetLogFileName()
|
||||
{
|
||||
var cultureInfo = System.Globalization.CultureInfo.CurrentCulture;
|
||||
int weekNo = cultureInfo.Calendar.GetWeekOfYear(
|
||||
DateTime.Now,
|
||||
cultureInfo.DateTimeFormat.CalendarWeekRule,
|
||||
cultureInfo.DateTimeFormat.FirstDayOfWeek);
|
||||
|
||||
return $"processLog_{DateTime.Now.Year}_Week{weekNo}.log";
|
||||
}
|
||||
24
marketally.processmonitor.csproj
Normal file
24
marketally.processmonitor.csproj
Normal file
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="processlist.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="processlist.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
25
marketally.processmonitor.sln
Normal file
25
marketally.processmonitor.sln
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.9.34310.174
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "marketally.processmonitor", "marketally.processmonitor.csproj", "{0DC18C8D-FF6C-4A8A-9215-3D99C3B19F98}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{0DC18C8D-FF6C-4A8A-9215-3D99C3B19F98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0DC18C8D-FF6C-4A8A-9215-3D99C3B19F98}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0DC18C8D-FF6C-4A8A-9215-3D99C3B19F98}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0DC18C8D-FF6C-4A8A-9215-3D99C3B19F98}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {819C0AC2-428B-4FF6-99E9-12418DF3ABB8}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
23
processlist.json
Normal file
23
processlist.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"processes": [
|
||||
{
|
||||
"name": "Chann3l.WebJob",
|
||||
"count": 4,
|
||||
"enable": false,
|
||||
"path": "C:\\Solutions\\chann3ls\\Chann3l.MessageJob\\bin\\Debug\\Chann3l.WebJob.exe"
|
||||
},
|
||||
{
|
||||
"name": "Chann3l.SymbolJob",
|
||||
"count": 6,
|
||||
"enable": false,
|
||||
"path": "C:\\Solutions\\chann3ls\\Chann3l.SymbolJob\\bin\\Debug\\Chann3l.SymbolJob.exe"
|
||||
},
|
||||
{
|
||||
"name": "Chann3l.StockJob",
|
||||
"count": 1,
|
||||
"enable": true,
|
||||
"interval": 5,
|
||||
"path": "C:\\Solutions\\chann3ls\\Chann3l.StockJob\\bin\\Debug\\Chann3l.StockJob.exe"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user