using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Shumi.DataStats.Tasks;
namespace Shumi.DataStats.TaskCenter
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main(params string[] args)
{
if (args.Length != 0)
{
RunCommands(args);
}
else
{
RunServices();
}
}
static void RunCommands(String[] args)
{
String firstArg = args[0].ToLower();
if (firstArg == "/i")
{
Installer installer = GetInstaller();
Dictionary<Object, Object> stateSaver = new Dictionary<Object, Object>();
installer.Install(stateSaver);
foreach (var kv in stateSaver)
{
Print(stateSaver, 0);
}
}
else if (firstArg == "/u")
{
Installer installer = GetInstaller();
installer.Uninstall(null);
}
else if (firstArg == "/start")
{
try
{
ServiceController sc = new ServiceController("Shumi.DataStats.TaskCenter");
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
TraceUtility.Write("服务启动成功。");
}
else
{
Console.WriteLine("已经启动或无法启动。");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else if (firstArg == "/stop")
{
try
{
ServiceController sc = new ServiceController("Shumi.DataStats.TaskCenter");
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
TraceUtility.Write("服务停止成功。");
}
else
{
Console.WriteLine("已经停止或无法停止。");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else if (firstArg == "/c" || firstArg == "/console")
{
ServiceMain.RunBusiness();
}
else if (firstArg == "/m")
{
TaskStatusMonitor();
}
else
{
Console.WriteLine("参数不正确:" + firstArg);
}
}
static void RunServices()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ServiceMain()
};
ServiceBase.Run(ServicesToRun);
}
static Installer GetInstaller()
{
ProjectInstaller installer = new ProjectInstaller();
installer.BeforeInstall += new InstallEventHandler((obj, state) => { Console.WriteLine("服务正在安装......"); });
installer.AfterInstall += new InstallEventHandler((obj, state) => { Console.WriteLine("服务安装完成!"); });
installer.BeforeUninstall += new InstallEventHandler((obj, state) => { Console.WriteLine("服务正在卸载......"); });
installer.AfterUninstall += new InstallEventHandler((obj, state) => { Console.WriteLine("服务卸载完成!"); });
string logFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "installer.log");
string path = string.Format("/assemblypath={0}", System.Reflection.Assembly.GetExecutingAssembly().Location);
string[] cmd = { path };
InstallContext context = new InstallContext(logFile, cmd);
installer.Context = context;
return installer;
}
static void Print(IDictionary<Object, Object> stateSaver, int tabIndent)
{
foreach (var kv in stateSaver)
{
if (kv.Value is IDictionary<Object, Object> && tabIndent < 5)
{
Print(kv.Value as IDictionary<Object, Object>, tabIndent + 1);
}
else
{
var tabs = "";
while (tabIndent-- > 0)
{
tabs += "\t";
}
Console.WriteLine(tabs + kv.Key + "===" + kv.Value);
}
}
}
static void TaskStatusMonitor()
{
TaskControlClient taskControlClient = new TaskControlClient();
Console.WriteLine("任务状态监视工具!\r\n");
PrintTaskStatus(taskControlClient);
while (true)
{
Console.WriteLine();
String cmd = Console.ReadLine().ToLower();
String[] args = cmd.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
String firstArg = "";
if (args.Length != 0)
{
firstArg = args[0];
}
if (firstArg == "list" || firstArg == "")
{
PrintTaskStatus(taskControlClient);
}
else if (firstArg == "start" && args.Length > 1)
{
String name = args[1];
TaskInfo taskInfo = taskControlClient.GetTaskInfo(name);
if (taskInfo == null)
{
Console.WriteLine("对应的名称的任务不存在。");
}
taskControlClient.StartTask(name);
}
else if (firstArg == "stop" && args.Length > 1)
{
String name = args[1];
TaskInfo taskInfo = taskControlClient.GetTaskInfo(name);
if (taskInfo == null)
{
Console.WriteLine("对应的名称的任务不存在。");
}
taskControlClient.StopTask(name);
}
Console.WriteLine();
}
}
static void PrintTaskStatus(TaskControlClient taskControlClient)
{
var list = taskControlClient.GetAllTaskInfos();
Console.WriteLine("{0}\n\t\t{1,-8}{2,-10}{3,-24}{4,-24}",
"Name",
"Status",
"Interval",
"LastRunStartTime",
"LastRunEndTime");
foreach (var item in list)
{
String format = "{0}\n\t\t{1,-5}{2,-10}{3,-24}{4,-24}";
Console.WriteLine(format,
item.Name,
item.Status,
item.Interval,
item.LastRunStartTime.ToString("yyyy-MM-dd HH:mm:ss"),
item.LastRunEndTime.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
}
}