Learn.VideoAnalysis/VideoAnalysisCore/Job/DeviceHeartbeatJob.cs

38 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Coravel.Invocable;
using FreeRedis;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
using VideoAnalysisCore.Common;
namespace VideoAnalysisCore.Job
{
/// <summary>
/// 设备心跳上报任务
/// </summary>
public class DeviceHeartbeatJob : IInvocable
{
public Task Invoke()
{
try
{
var redis = AppCommon.Services.GetService<RedisClient>();
if (redis == null) return Task.CompletedTask;
var deviceId = AppCommon.Config.ID.ToString();
// 1. 发送心跳 (设置一个带过期时间的Key)
// 只有当程序正常运行时这个Key才会不断被续期
// 过期时间设为 60秒Job每30秒执行一次
redis.Set(RedisExpandKey.DeviceHeartbeat(deviceId), DateTime.Now.ToString(), 60);
}
catch (Exception ex)
{
Console.WriteLine($"心跳任务异常: {ex.Message}");
}
return Task.CompletedTask;
}
}
}