/// <summary> /// 写单行数据 /// (如果文件存在,则追加) /// </summary> /// <param name="line">数据</param> /// <param name="fileName">文件名</param> /// <param name="globalFile">是否是全局文件</param> public void WriteText(string line, string fileName, bool globalFile) { //1. 检查根目录是否存在 string rootFolder = GetRootFolder(); if (!Directory.Exists(rootFolder)) { Directory.CreateDirectory(rootFolder); } //2. 写文件数据 string filePath = GetFilePath(fileName, globalFile); File.AppendAllText(filePath, line, Encoding.UTF8); FileInfo fileInfo = new FileInfo(filePath); //3. 写到状态树 if (globalFile) { StateHelper.Put("ISE://File/" + fileName + ".global", fileInfo.Length / 1024); } else { List <string> workers = new List <string>(StateHelper.GetChildren("ISE://system/state/worker")); int count = 1; if (workers.Count > 0) { count = workers.Count; } StateHelper.Put("ISE://File/" + fileName + ".part", fileInfo.Length / 1024 * count); } }
private void Execute(object obj) { try { Status.Description = "Running"; StateHelper.Put(_runningPath, "Start runing..."); var references = new List <string> { "Iveely.CloudComputing.Client.exe", "Iveely.Framework.dll", "System.Xml.dll", "System.Xml.Linq.dll", "NDatabase3.dll" }; CodeCompiler.Execode(obj.ToString(), Status.Packet.ClassName, references, new object[] { Status.Packet.ReturnIp, Status.Packet.Port, _machineName, _servicePort, Status.Packet.TimeStamp, Status.Packet.AppName }); StateHelper.Put(_runningPath, "Finished with success!"); Program.SetStatus(Status.Packet.AppName, "Success"); } catch (Exception exception) { Logger.Error(exception); StateHelper.Put(_runningPath, "Finished with " + exception); Program.SetStatus(Status.Packet.AppName, "Fisnihed with " + exception); } }
public static void Main(string[] args) { //1. 确定worker运行端口号 int port = 8001; if (args.Length > 0) { port = int.Parse(args[0]); } _machineName = Dns.GetHostName(); _servicePort = port; _statusCenter = new Hashtable(); _runner = new Hashtable(); string processFolder = _servicePort.ToString(CultureInfo.InvariantCulture); if (!Directory.Exists(processFolder)) { Directory.CreateDirectory(processFolder); CopyFile("Init", processFolder); CopyDirectory("Init", processFolder + "\\"); } CheckCrash(); //2. 向State Center发送上线消息 StateHelper.Put("ISE://system/state/worker/" + _machineName + "," + _servicePort, _machineName + ":" + _servicePort + " is ready online!"); //3. 启动心跳线程 Thread thread = new Thread(SendHeartbeat); thread.Start(); //3. 启动任务接收监听 if (_taskSuperviser == null) { Logger.Info("Starting listen the worker's task..."); _taskSuperviser = new Server(_machineName, _servicePort, ProcessTask); Logger.Info("worker's task supervisor instance build success..."); _taskSuperviser.Listen(); } }
public override void ProcessCmd(string[] args) { if (args.Length < 3 || args.Length == 4) { UnknowCommand(); return; } //2.1 将文件切分成块 List <string> workers = new List <string>(StateHelper.GetChildren("ISE://system/state/worker")); string filePath = args[1]; if (File.Exists(filePath)) { //普通切分模式 if (args.Length == 3) { FileBlock.Split(filePath, workers.Count); } //用户自定义切分 else { string splitStr = args[3]; List <int> keys = new List <int>(); for (int i = 4; i < args.Length; i++) { int index; if (!int.TryParse(args[i], out index)) { Logger.Error("index should be an int."); break; } keys.Add(index); } FileBlock.Split(filePath, workers.Count, splitStr, keys.ToArray()); } } else { Logger.Error(filePath + " is not found."); return; } //2.2 告诉worker即将发送文件块 string remotePath = args[2]; string[] childFiles = Directory.GetFiles(filePath + ".part"); for (int i = 0; i < workers.Count; i++) { string[] ip = workers[i].Substring(workers[i].LastIndexOf('/') + 1, workers[i].Length - workers[i].LastIndexOf('/') - 1) .Split(','); Framework.Network.Synchronous.Client transfer = new Framework.Network.Synchronous.Client(ip[0], int.Parse(ip[1])); ExcutePacket codePacket = new ExcutePacket(Encoding.UTF8.GetBytes(remotePath), string.Empty, string.Empty, string.Empty, ExcutePacket.Type.FileFragment); codePacket.SetReturnAddress(Dns.GetHostName(), 8800); //1207 codePacket.WaiteCallBack = false; transfer.Send <bool>(codePacket); Logger.Info(ip[0] + "," + ip[1] + " has been noticed to receive the file."); int maxRetryCount = 5; while (maxRetryCount > 0) { try { FileTransfer fileTransfer = new FileTransfer(); fileTransfer.Send(childFiles[i], ip[0], 7001); maxRetryCount = -1; } catch (Exception exception) { Logger.Error(exception.Message); maxRetryCount--; if (maxRetryCount > 0) { Console.WriteLine("Now retry..."); Thread.Sleep(1000); } } } if (maxRetryCount == 0) { Logger.Info(ip[0] + "," + ip[1] + " do not get the file."); } } //2.4 删掉切分的文件 Directory.Delete(filePath + ".part", true); StateHelper.Put("ISE://File/" + remotePath, new FileInfo(filePath).Length / 1024); }
public override void ProcessCmd(string[] args) { if (args.Length == 5) { _showMsgFromRemote = args[4] == "true"; } if (args.Length != 4 && !_showMsgFromRemote) { UnknowCommand(); return; } //1.1 编译应用程序 Logger.Info("Start Compile your code..."); string appName = args[3]; string className = args[2]; string filePath = args[1]; string timeStamp = DateTime.Now.ToFileTimeUtc().ToString(CultureInfo.InvariantCulture); string compileResult = CompileCode(filePath); if (compileResult != string.Empty) { Logger.Error(compileResult); return; } //1.2 读取编译后的文件 Logger.Info("Preparing for send your application to platform..."); string sourceCode = File.ReadAllText(filePath); byte[] bytes = Encoding.UTF8.GetBytes(sourceCode); //1.3 上传程序至各个节点 Thread thread = new Thread(StartListen); thread.Start(); StateHelper.Put("ISE://history/" + timeStamp + "/" + appName, Dns.GetHostName()); IEnumerable <string> ipPathes = StateHelper.GetChildren("ISE://system/state/worker"); var ipPaths = ipPathes as string[] ?? ipPathes.ToArray(); foreach (var ipPath in ipPaths) { Logger.Info("Current worker ip path:" + ipPath); string[] ip = ipPath.Substring(ipPath.LastIndexOf('/') + 1, ipPath.Length - ipPath.LastIndexOf('/') - 1) .Split(','); Framework.Network.Synchronous.Client transfer = new Framework.Network.Synchronous.Client(ip[0], int.Parse(ip[1])); ExcutePacket codePacket = new ExcutePacket(bytes, className, appName, timeStamp, ExcutePacket.Type.Code); codePacket.SetReturnAddress(Dns.GetHostName(), 8800); //1207 codePacket.WaiteCallBack = false; transfer.Send <object>(codePacket); } //1.4 结点运行程序,直至结束 DateTime submitTime = DateTime.UtcNow; while (!IsDelay(submitTime, 60)) { if (CheckApplicationExit(timeStamp, appName, ipPaths.Count())) { Console.WriteLine("Application has submitted, you can user [task] command to see the status."); return; } Thread.Sleep(1000); } Console.WriteLine("Application failured as run too much time."); }