Ejemplo n.º 1
0
        public void HandleTaskRequest(PacketWorkerTaskRequest packet)
        {
            var isAccepting = _network.GetWorker().IsStandbye();

            if (!isAccepting)
            {
                Logger.Info($"Server give a new task {packet.GetTask()}, decline, we are busy");
                _network.SendPacket(new PacketWorkerTaskResponse(packet.GetTask(), false));
                return;
            }

            Logger.Info($"Server give a new task {packet.GetTask()}, accepting");
            _network.SendPacket(new PacketWorkerTaskResponse(packet.GetTask(), true));
            var task = packet.GetTask();
            var job  = task.ParentJob;

            foreach (var jobFile in job.JobFiles)
            {
                if (jobFile.Direction == EGridJobFileDirection.WorkerOutput)
                {
                    continue;
                }

                if (!GridIo.IsJobTaskFileExistsAndValid(task, jobFile))
                {
                    Logger.Debug($"Request file '{jobFile}' from server for task '{task}' (not exists locally)");
                    _network.SendPacket(new PacketWorkerFileRequest(task.TaskId, job.Name, jobFile));
                }
            }

            if (!_network.GetWorker().RunNewTask(packet.GetTask()))
            {
                Logger.Info("Worker is already running a task (or receive packet twice), refuse");
                _network.SendPacket(new PacketWorkerTaskCancel(packet.GetTask()));
            }
        }
Ejemplo n.º 2
0
        public void Tick()
        {
            if (!_isInitialized)
            {
                return;
            }

            while (!IsConnected())
            {
                try {
                    _remoteAddress = Dns.GetHostAddresses(_gridWorker.Settings.ServerAddress).FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);
                    if (_remoteAddress == null)
                    {
                        throw new Exception($"Unable to lookup ip for remote '{_gridWorker.Settings.ServerAddress}'");
                    }

                    _tcpClient = new TcpClient(new IPEndPoint(IPAddress.Any, 0))
                    {
                        ReceiveTimeout = _gridWorker.Settings.ReceiveTimeout,
                        NoDelay        = true
                    };

                    _tcpClient.Connect(new IPEndPoint(_remoteAddress, (int)_gridWorker.Settings.ServerPort));
                    _tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                    _workerNetwork = new WorkerNetwork(this, _tcpClient, _gridWorker);
                    Logger.Debug("Server is connected to remote grid, wait for logon");

                    _workerNetwork.SendPacket(new PacketWorkerLoginRequest(_gridWorker.Settings.WorkerName));
                    _repeatRefuseMsg = true;
                } catch (SocketException se) {
                    if (_repeatRefuseMsg)
                    {
                        if (se.SocketErrorCode == SocketError.ConnectionRefused)
                        {
                            Logger.Warn("Remote server refused connection, maybe not running (trying to connect in background)");
                        }
                        else
                        {
                            Logger.Warn("Unable to connect to main server (trying to connect in background)", se);
                        }

                        _repeatRefuseMsg = false;
                    }

                    Thread.Sleep(_gridWorker.Settings.ConnectRepeatInterval);
                } catch (Exception e) {
                    Logger.Warn("Unable to connect to main server", e);
                    Thread.Sleep(_gridWorker.Settings.ConnectRepeatInterval);
                }
            }

            if (_workerNetwork == null)
            {
                _tcpClient.Close();
                return;
            }

            try {
                _workerNetwork.ChannelRead();
                _workerNetwork.CheckTimedOut();

                if (!IsConnected())
                {
                    Logger.Info("Disconnected from server: Read timeout");
                }
            } catch (IOException e) {
                if (_workerNetwork.IsLoggedOnServer())
                {
                    ForceDisconnect();
                    return;
                }

                Logger.Error("IO Error during worker network channel read", e);
            }
        }
Ejemplo n.º 3
0
 public void SendPacket(IPacket packet)
 {
     _workerNetwork?.SendPacket(packet);
 }