protected override void OnStart(string[] args) { using (TaskRequestServ.TaskPoolClient tp = new TaskRequestServ.TaskPoolClient()) { HostName = GetType().Assembly.Location.Split('\\').LastOrDefault().Substring(1).Replace(".exe", ""); var WCFBack = tp.IamOnline(HostName, Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).FirstOrDefault().ToString(), getRAM(), Environment.ProcessorCount, Environment.OSVersion.ToString(), "r"); if (WCFBack["Err"] != "") { throw new Exception("Instance already exist."); } MaxTask = Int32.Parse(WCFBack["MaxTask"]); WorkDir = WCFBack["WorkDir"]; Heartbeat = Int32.Parse(WCFBack["Heartbeat"]); tp.Close(); } tr = new System.Threading.Thread(new System.Threading.ThreadStart(() => StartWork())); tr.Start(); }
private void StartWork() { while (!isStop) { while (MaxTask - tasks.Count > 0 && !isStop) { Dictionary <string, string> WCFBack = null; try { using (TaskRequestServ.TaskPoolClient tp = new TaskRequestServ.TaskPoolClient()) { WCFBack = tp.GetTaskRun(HostName, tasks.Count); tp.Close(); } if (WCFBack != null) { if (WCFBack["Usage"] == "Work") { Int32 id = Int32.Parse(WCFBack["Id"]); String runnerpatth = WCFBack["RunnerPath"]; String runnerargs = WCFBack["RunnerArgs"]; Int64 mem = Int64.Parse(WCFBack["Mem"]); Int32 runtimelimit = Int32.Parse(WCFBack["RunTimeLimit"]); String executionformat = WCFBack["ExecutionFormat"]; tasks.Add(id); Task.Run(() => Judge(id, runnerpatth, runnerargs, executionformat, runtimelimit, mem, WCFBack["Input"], WCFBack["Output"])); } else { MaxTask = Int32.Parse(WCFBack["MaxTask"]); WorkDir = WCFBack["WorkDir"]; Heartbeat = Int32.Parse(WCFBack["Heartbeat"]); System.Threading.Thread.Sleep(Heartbeat); } } } catch (Exception ex) { ; } } System.Threading.Thread.Sleep(1000); } }
private void updateTaskRunningResult(int id, String reply, Double runtime, long mem, string status, bool ispass) { while (true) { try { using (TaskRequestServ.TaskPoolClient tp = new TaskRequestServ.TaskPoolClient()) { tp.updateTaskRunningResult(id, reply, runtime, mem, status, ispass, tasks.Count); tp.Close(); } } catch (Exception ex) { System.Threading.Thread.Sleep(3000); continue; } return; } }