- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在使用 1.6.19 版 atm 和 MongoDB 作为存储的 Hangfire 时遇到问题,我们目前有一个方法,安排如下:
BackgroundJob.Schedule(() => DoAsyncTask(parameters, JobCancellationToken.Null), TimeSpan.FromMinutes(X))
该任务将运行一个多小时,它包含一个循环来验证作业何时完成。在循环内,调用 cancellationToken.ThrowIfCancellationRequested() 以验证是否已请求取消,但此调用在执行后大约 30 分钟后一直被触发,并在完成前终止作业。
我一直在搜索有关此问题的信息,但其中大部分与旧版本或根据 this answer 的 InvisibilityTimeout 的使用有关已被弃用,所以我想知道是否还有其他人遇到过此问题以及任何可能的解决方案。
谢谢
编辑:经过进一步调查,我发现取消问题只是运行 30 分钟后 HangFire 再次调用任务的副作用,因为我在方法中设置了验证以避免重新进入该过程仍在运行(以避免数据重复),该过程将被视为已完成并因此被取消。
所以我面临的真正问题是我无法确定 HangFire 在执行大约 30 分钟后再次调用进程的原因,我按照描述的步骤 here 进行操作将 IIS 上的应用程序设置为始终运行并防止池被回收,但行为仍然存在。
最佳答案
针对我的问题实现的解决方案是使用 this filter在作业上设置分布式锁,直到它正确完成。我对实现进行了一些小的更改,以包含作业 ID 并更新了对该版本 HangFire 上使用的新对象的调用,因此我将其保留在这里:
public class SkipConcurrentExecutionAttribute : JobFilterAttribute, IServerFilter
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private readonly int _timeoutInSeconds;
public SkipConcurrentExecutionAttribute(int timeoutInSeconds)
{
if (timeoutInSeconds < 0) throw new ArgumentException("Timeout argument value should be greater that zero.");
_timeoutInSeconds = timeoutInSeconds;
}
public void OnPerforming(PerformingContext filterContext)
{
var resource = $"{filterContext.BackgroundJob.Job.Type.FullName}.{filterContext.BackgroundJob.Job.Method.Name}.{filterContext.BackgroundJob.Id}";
var timeout = TimeSpan.FromSeconds(_timeoutInSeconds);
try
{
var distributedLock = filterContext.Connection.AcquireDistributedLock(resource, timeout);
filterContext.Items["DistributedLock"] = distributedLock;
}
catch (Exception)
{
filterContext.Canceled = true;
logger.Warn("Cancelling run for {0} job, id: {1} ", resource, filterContext.BackgroundJob.Id);
}
}
public void OnPerformed(PerformedContext filterContext)
{
if (!filterContext.Items.ContainsKey("DistributedLock"))
{
throw new InvalidOperationException("Can not release a distributed lock: it was not acquired.");
}
var distributedLock = (IDisposable)filterContext.Items["DistributedLock"];
distributedLock.Dispose();
}
}
所以现在对后台进程的调用是:
[SkipConcurrentExecution(300)]
public async Task DoAsyncTask(parameters, IJobCancellationToken cancellationToken){
//code execution here
}
我希望这会有所帮助,重新进入的原因仍然未知,因此请随时使用您可能找到的任何信息来扩展此答案。
关于c# - Hangfire 使用 MongoDB 执行长时间运行的后台作业不断重启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51241124/
我有一个独立的 Thread 应用程序。这是一个等待消息的监听器,当消息到达时执行一些操作,其中我必须将消息保存在数据库中。但我遇到了问题,因为如果我运行应用程序并“手动发送消息”,一切都会正常工作,
我有以下php代码: sleep(65); $query = "UPDATE database.table SET XXXXXXX = XXXXXXX - ".$YYYYYY." WHERE
我正在开发一个业余爱好应用程序。它在主布局中使用 webview。单击 webview 内的链接会使用户保持在 webview 内。启动后一切正常,但仍在应用程序内。但是,在手机休眠一段时间后,我重新
我目前运行的应用程序需要最大堆大小为 16GB。 目前我使用以下标志来处理垃圾回收。 -XX\:+UseParNewGC, -XX\:+UseConcMarkSweepGC, -XX:CMSIniti
$ uname -a Darwin Wheelie-Cyberman 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011
在 while 循环仍在休眠时退出它的最简单方法是什么?是否有某种函数可以在 sleep 时检测某个值是否为真? 或者我是否在循环中设置一个小 sleep 并检查如果不再睡一会儿就退出?如果可以,我该
我正在 Ubunu 的 Jetty 6 上运行 Java Web 服务器,用于基于反向 ajax 的 Web。而且我在向浏览器重新发送数据的线程滞后方面遇到了严重的问题。很多时候,一些线程开始 hib
当我运行长时间操作时,我遇到来自 IIS 的请求超时。我的 ASP.NET 应用程序正在后台处理数据,但处理的记录数量很大,因此操作需要很长时间。 但是,我认为 IIS 使 session 超时。这是
我不确定从哪里开始解决这个问题,但如果我有一个 AJAX 网络应用程序向服务器发送请求并在数据库(在我的例子中是 postgresql)上运行长查询,有没有办法停止或如果仍在运行时用户刷新页面或关闭
我是一名优秀的程序员,十分优秀!