簡體   English   中英

.Net Core 后台服務和長時間運行的任務

[英].Net Core Background Services and Long Running Tasks

我正在使用.Net Core 在我的 ASP.Net Core 中運行長時間運行的后台服務和長時間運行的任務,以獲取由 Linux Ubuntu 18.04 中的外部運行進程生成的文件。 使用 FileSystemWatcher。

24 小時或更長時間后,情況會變得更糟。 FileSystemWatch 工作正常,但長時間運行的線程和后台服務工作不正常,有時它們會停止而沒有錯誤。

任何人都對 .Net Core 3.1 中的后台服務或長時間運行的任務問題有所了解。

我應該使用 Hangfire 來避免這些問題嗎?

示例:此線程可以不間斷地工作長達 72 小時。

   public class DataByteCollector{

    public readonly BytesMemoryCache DataBytesMemoryCache;

    public DataByteCollector(BytesMemoryCache DataBytesMemoryCache)
    {
        DataBytesMemoryCache = DataBytesMemoryCache;
    }

    public BufferBlock<ByteData> ByteStream { get; set; }

    public async Task SubscribeToStream(string bytesId, CancellationToken cancellationToken)
    {
        // Avoid Capturing members in anonymous methods

        var DataBytesMemoryCache = DataBytesMemoryCache;
        ByteStream = new BufferBlock<ByteData>(
            new DataflowBlockOptions
            {
                BoundedCapacity = 8,
                EnsureOrdered = true,
                CancellationToken = cancellationToken
            });
        var byteStream = ByteStream;

        var streamDataCache = DataBytesMemoryCache.GetBytesCache(bytesId);
        if (streamDataCache == null)
        {
            ByteStream.Complete();
            return;
        }

        var bytesData = streamDataCache.Streams;

        async Task DataGathring()
        {
            try
            {
                DateTime? lastDataRead = null;

                var hasStarted = true;
                while (!cancellationToken.IsCancellationRequested && DataBytesMemoryCache.DataCacheExists(bytesId))
                {
                    IEnumerable<ByteData> stat;
                    if (hasStarted)
                    {
                        stat = bytesData.ToArray();
                        hasStarted = false;
                    }
                    else
                    {
                        stat = bytesData.TakeLast(1).Where(x => x.DateTime > lastDataRead).ToArray();
                    }

                    if (stat.Any())
                    {
                        foreach (var farge in stat)
                        {
                            lastDataRead = DateTime.Now;
                            await Channel.SendAsync(farge, cancellationToken);
                        }
                    }
                    else
                    {
                        await byteStream.SendAsync(new ByteData(new byte[100], "dummy"), cancellationToken);
                    }

                 
                    if (lastDataRead == null)
                    {
                        break;
                    }
                    if (lastDataRead < DateTime.Now.AddSeconds(-30))
                    {
                        break;
                    }

                    await Task.Delay(5000, cancellationToken);
                  
                }
                cancellationToken.ThrowIfCancellationRequested();
                byteStream.Complete();
                await byteStream.Completion;
            }
            catch (Exception e)
            {

                byteStream.Complete();
                await byteStream.Completion;
            }
        }

        await Task.Factory.StartNew(DataGathring, cancellationToken);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM