예제 #1
0
        public static void Execute(Messsage command, AgentClient client)
        {
            if (client.HasExited)
            {
                return;
            }
            if (command.NodeId != Env.NodeId)
            {
                Logger.Error($"Command error: {JsonConvert.SerializeObject(command)}.");
                return;
            }

            Logger.Info($"Consume message: {JsonConvert.SerializeObject(command)}.");

            try
            {
                if (Commands.ContainsKey(command.Name))
                {
                    Commands[command.Name].Execute(command, client);
                }
            }
            catch (Exception e)
            {
                Logger.Error($"Execute command {JsonConvert.SerializeObject(command)} failed: {e}");
            }
        }
예제 #2
0
        public override void Execute(Messsage command, AgentClient client)
        {
            Logger.Info("Waiting all exists crawler processes...");

            while (ProcessManager.ProcessCount > 0)
            {
                Thread.Sleep(1000);
            }
            Logger.Info("All exists crawler processes exit success.");
            client.Dispose();
        }
예제 #3
0
        public override void Execute(Messsage command, AgentClient client)
        {
            if (string.IsNullOrWhiteSpace(command.Package))
            {
                Log.Logger.Error($"Package should not be empty.");
                return;
            }
            if (!command.Package.ToLower().Contains(".zip"))
            {
                Log.Logger.Error($"Package must be a zip.");
                return;
            }
            if (ProcessManager.IsTaskExsits(command.TaskId))
            {
                Log.Logger.Error($"Task {command.TaskId} is already running.");
                return;
            }
            Log.Logger.Information($"Start prepare workdirectory...");
            var taskDirectory = Path.Combine(Env.ProjectsDirectory, command.TaskId.ToString());

            if (!Directory.Exists(taskDirectory))
            {
                Directory.CreateDirectory(taskDirectory);
                Log.Logger.Information($"Create task directory {taskDirectory} success.");
            }
            Uri uri;

            if (Uri.TryCreate(command.Package, UriKind.RelativeOrAbsolute, out uri))
            {
                var    packageName      = Path.GetFileNameWithoutExtension(uri.AbsolutePath);
                string workingDirectory = Path.Combine(taskDirectory, packageName);

                if (!Directory.Exists(workingDirectory))
                {
                    var localPackageFilePath = Path.Combine(Env.PackagesDirectory, Path.GetFileName(uri.AbsolutePath));
                    var bytes = Env.HttpClient.GetByteArrayAsync(uri).Result;
                    File.WriteAllBytes(localPackageFilePath, bytes);
                    ZipFile.ExtractToDirectory(localPackageFilePath, workingDirectory);
                }
                ProcessManager.StartProcess(command.TaskId, command.ApplicationName, command.Arguments, workingDirectory);
            }
            else
            {
                Log.Logger.Error("Package is not a correct url.");
            }
        }
예제 #4
0
        public override void Execute(Messsage command, AgentClient client)
        {
            if (!ProcessManager.IsTaskExsits(command.TaskId))
            {
                Logger.Warn($"Task {command.TaskId} is not running.");
                return;
            }

            ProcessInfo processInfo = ProcessManager.GetProcessDetail(command.TaskId);

            if (processInfo != null)
            {
                var process = processInfo.Process;
                try
                {
                    SendExitSignal(command.TaskId.ToString(), processInfo.WorkingDirectory);
                }
                catch
                {
                    //ignore
                }
                process.WaitForExit(30000);

                try
                {
                    process.Kill();
                }
                catch (NotSupportedException nse)
                {
                    Logger.Info($"Kill task {command.TaskId} success: {nse.Message}.");
                }
                catch (Win32Exception we)
                {
                    Logger.Info($"Kill task {command.TaskId} success: {we.Message}.");
                }
                catch (InvalidOperationException ioe)
                {
                    Logger.Info($"Kill task {command.TaskId} success: {ioe.Message}.");
                }
                catch (Exception e)
                {
                    Logger.Error($"Kill task {command.TaskId} failed: {e}.");
                }
            }
        }
예제 #5
0
        public override void Execute(Messsage command, AgentClient client)
        {
            if (ProcessManager.IsTaskExsits(command.TaskId))
            {
                Logger.Error($"Task {command.TaskId} is already running.");
                return;
            }

            if (string.IsNullOrEmpty(command.Version) || string.IsNullOrWhiteSpace(command.Version))
            {
                Logger.Error($"Version should not be empty.");
                return;
            }
            Logger.Info($"Start prepare workdirectory...");
            var taskDirectory = Path.Combine(Env.ProjectsDirectory, command.TaskId.ToString());

            if (!Directory.Exists(taskDirectory))
            {
                Directory.CreateDirectory(taskDirectory);
                Logger.Info($"Create task directory {taskDirectory} success.");
            }

            string workingDirectory = Path.Combine(taskDirectory, command.Version);

            if (!Directory.Exists(workingDirectory))
            {
                var packageUrl = $"{Env.PackageUrl}{command.Version}.zip";
                try
                {
                    var localPackageFilePath = Path.Combine(Env.PackagesDirectory, $"{command.Version}.zip");
                    var bytes = Env.HttpClient.GetByteArrayAsync(packageUrl).Result;
                    File.WriteAllBytes(localPackageFilePath, bytes);
                    ZipFile.ExtractToDirectory(localPackageFilePath, workingDirectory);
                }
                catch (Exception e)
                {
                    Logger.Error($"Download package {packageUrl} failed: {e}.");
                    return;
                }
            }
            ProcessManager.StartProcess(command.TaskId, command.ApplicationName, command.Arguments, workingDirectory);
        }
예제 #6
0
        public static async Task SendMessageAsync(DeviceClient deviceClient)
        {
            while (true)
            {
                var data = new TemperatureModel
                {
                    Temperature = rnd.Next(20, 30),
                    Humidity    = rnd.Next(40, 50)
                };

                var json = JsonConvert.SerializeObject(data);

                var payload = new Messsage(Encoding.UTF8.GetBytes(json));
                await deviceClient.SendEventAsync(payload);


                Console.WriteLine($"Message sent: {json}");

                await Task.Delay(60 * 1000);
            }
        }
예제 #7
0
 public abstract void Execute(Messsage command, AgentClient client);