Exemplo n.º 1
0
        /// <summary>
        /// 本地代理不需要留存心跳
        /// </summary>
        /// <param name="heartbeat"></param>
        /// <returns></returns>
        public Task HeartbeatAsync(DownloadAgentHeartbeat heartbeat)
        {
            if (_agents.TryGetValue(heartbeat.AgentId, out var agent))
            {
                agent.LastModificationTime = DateTime.Now;
            }

            return(Task.CompletedTask);
        }
        public async Task HeartbeatAsync(DownloadAgentHeartbeat agent)
        {
            using (var conn = new MySqlConnection(_options.ConnectionString))
            {
                var obj = await conn.QueryFirstOrDefaultAsync <dynamic>(
                    $"SELECT id FROM SopSpider.downloader_agent WHERE id = @Id && is_deleted = false LIMIT 1;",
                    new
                {
                    agent.Id
                });

                if (obj != null)
                {
                    await conn.ExecuteAsync(
                        $"INSERT IGNORE INTO SopSpider.downloader_agent_heartbeat (agent_id, agent_name, free_memory, downloader_count, creation_time) VALUES (@AgentId, @AgentName, @FreeMemory, @DownloaderCount, @CreationTime);",
                        agent);

                    await conn.ExecuteAsync(
                        $"UPDATE SopSpider.downloader_agent SET last_modification_time = @LastModificationTime WHERE id = @AgentId",
                        new { agent.AgentId, LastModificationTime = agent.CreationTime });
                }
            }
        }