public IActionResult Heartbeat([FromBody] NodeHeartbeatInput input)
        {
            CheckAuth();

            //在此插入代理需要执行的命令消息,如爬网命令
            return(Success(_heartbeatAppService.Create(input), null));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 更新心跳
        /// </summary>
        /// <param name="heartbeat">节点心跳</param>
        /// <returns>A System.Threading.Tasks.Task that represents the asynchronous invoke.</returns>
        public virtual async Task Heartbeat(NodeHeartbeatInput heartbeat)
        {
            using (var conn = CreateDbConnection())
            {
                await AddOrUpdate(new Node { CpuCount = heartbeat.CpuCount, Group = heartbeat.Group, Ip = heartbeat.Ip, NodeId = heartbeat.NodeId, Os = heartbeat.Os, TotalMemory = heartbeat.TotalMemory });

                await conn.ExecuteAsync($"INSERT INTO nodeheartbeat (nodeid,cpu,freememory,processcount) VALUES (@NodeId,@Cpu,@FreeMemory,{(heartbeat.Runnings == null ? 0 : heartbeat.Runnings.Length)});", heartbeat);
            }
        }
Ejemplo n.º 3
0
        public async Task <BlockOutput> Pop(NodeHeartbeatInput heartbeat)
        {
            using (var conn = CreateDbConnection())
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                var         transaction = conn.BeginTransaction();
                BlockOutput output      = new BlockOutput {
                    Command = Command.Download
                };
                try
                {
                    var running = await _runningService.Pop(conn, transaction, heartbeat.Runnings);

                    output.Identity = running.Identity;

                    var block = await conn.QueryFirstOrDefaultAsync <Block>($@"SELECT TOP 1 * FROM block WHERE
{LeftEscapeSql}identity{RightEscapeSql}=@Identity WHERE status = 1 or status = 5", new { running.Identity }, transaction);

                    if (block != null)
                    {
                        await conn.ExecuteAsync($"UPDATE block SET status =@Status, lastmodificationtime={GetDateSql} WHERE blockid=@BlockId",
                                                new { Status = BlockStatus.Using, block.BlockId }, transaction);

                        var requests = await _requestQueueService.GetByBlockId(block.BlockId);

                        output.Requests = requests.Select(r =>
                        {
                            var request = JsonConvert.DeserializeObject <Request>(r.Request);
                            return(new RequestOutput {
                                Content = request.Content, CycleTriedTimes = request.CycleTriedTimes, Depth = request.Depth, Method = request.Method, Origin = request.Origin, Referer = request.Referer, Url = request.Url
                            });
                        }).ToList();
                        output.BlockId   = block.BlockId;
                        output.Site      = JsonConvert.DeserializeObject <Site>(running.Site);
                        output.ThreadNum = running.ThreadNum;
                    }
                    transaction.Commit();
                }
                catch
                {
                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.ToString());
                    }
                    throw;
                }
                return(output);
            }
        }
        public IActionResult Heartbeat([FromBody] NodeHeartbeatInput input)
        {
            CheckAuth();

            if (ModelState.IsValid)
            {
                return(Success(_heartbeatAppService.Create(input)));
            }
            else
            {
                return(BadRequest());
            }
        }
        public IEnumerable <NodeHeartbeatOutput> Create(NodeHeartbeatInput input)
        {
            if (input == null)
            {
                Logger.Error($"{nameof(input)} should not be null.");
                return(Enumerable.Empty <NodeHeartbeatOutput>());
            }
            CreateHeartbeat(input);
            RefreshOnlineStatus(input);
            DbContext.SaveChanges();
            var messages = _messageAppService.Consume(input.NodeId);

            return(Mapper.Map <IEnumerable <NodeHeartbeatOutput> >(messages));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Heartbeat([FromBody] NodeHeartbeatInput heartbeat)
        {
            IActionResult result = BadRequest();

            if (ModelState.IsValid)
            {
                await _nodeService.Heartbeat(heartbeat);

                var output = await _blockService.Pop(heartbeat);

                result = new JsonResult(output);
            }

            return(result);
        }
Ejemplo n.º 7
0
        public async Task <NodeHeartbeatInput> Heartbeat()
        {
            var service     = Services.GetRequiredService <INodeService>();
            var id          = Guid.NewGuid().ToString("N");
            var cpuCount    = 1000;
            var cpu         = 1000;
            var freeMemory  = 1000;
            var group       = "TESTGROUP";
            var ip          = "IPADDR1";
            var os          = "MYOS";
            var totalMemory = 1000;
            var heartbeat   = new NodeHeartbeatInput
            {
                Cpu         = cpu,
                CpuCount    = cpuCount,
                FreeMemory  = freeMemory,
                Group       = group,
                Ip          = ip,
                NodeId      = id,
                Os          = os,
                TotalMemory = totalMemory,
                Runnings    = new string[] { "p1", "p2" }
            };
            await service.Heartbeat(heartbeat);

            var node = await service.Get(id);

            var lastHeartbeat = await service.GetLastHeartbeat(id);

            Assert.Equal(id, node.NodeId);
            Assert.Equal(cpuCount, node.CpuCount);
            Assert.Equal(group, node.Group);
            Assert.Equal(ip, node.Ip);
            Assert.True(node.IsEnable);
            Assert.Equal(os, node.Os);
            Assert.Equal(totalMemory, node.TotalMemory);

            Assert.Equal(cpu, heartbeat.Cpu);
            Assert.Equal(freeMemory, heartbeat.FreeMemory);
            Assert.Equal(2, lastHeartbeat.ProcessCount);
            return(heartbeat);
        }
Ejemplo n.º 8
0
        private Response SendHeartbeartRequest()
        {
            var heartbeat = new NodeHeartbeatInput
            {
                Cpu         = (int)EnvironmentUtil.GetCpuLoad(),
                CpuCount    = Environment.ProcessorCount,
                FreeMemory  = EnvironmentUtil.GetFreeMemory(),
                Group       = _options.Group,
                Ip          = EnvironmentUtil.IpAddress,
                NodeId      = NodeId.Id,
                Os          = EnvironmentUtil.OSDescription,
                Runnings    = _runnings.ToArray(),
                TotalMemory = EnvironmentUtil.TotalMemory
            };

            Log.Logger.Information(heartbeat.ToString());
            var response = _downloader.Download(new Request(_heartbeatUrl)
            {
                Method = HttpMethod.Post, Site = _brokerSite, Content = JsonConvert.SerializeObject(heartbeat)
            });

            return(response);
        }
        private void RefreshOnlineStatus(NodeHeartbeatInput input)
        {
            var node = DbContext.Node.FirstOrDefault(n => n.NodeId == input.NodeId);

            if (node != null)
            {
                node.IsOnline             = true;
                node.Type                 = input.Type;
                node.Os                   = input.Os;
                node.LastModificationTime = DateTime.Now;
            }
            else
            {
                node                      = new Core.Entities.Node();
                node.NodeId               = input.NodeId;
                node.IsEnable             = true;
                node.IsOnline             = true;
                node.CreationTime         = DateTime.Now;
                node.Type                 = input.Type;
                node.Os                   = input.Os;
                node.LastModificationTime = node.CreationTime;
                DbContext.Node.Add(node);
            }
        }
        private void CreateHeartbeat(NodeHeartbeatInput input)
        {
            var heartbeat = Mapper.Map <Core.Entities.NodeHeartbeat>(input);

            DbContext.NodeHeartbeat.Add(heartbeat);
        }
        public IActionResult Heartbeat([FromBody] NodeHeartbeatInput input)
        {
            CheckAuth();

            return(Success(_heartbeatAppService.Create(input)));
        }