本文介绍了使用 ActiveJob AsyncAdapter 列出排队的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以让我看到队列中还有多少(甚至可能检查每个作业?)作业?

Is there a way I can see how many (maybe even inspect each job?) jobs are there remaining in the queue?

推荐答案

深入研究 源代码 这是我发现的:

After some digging into source code here is what I found out:

ActiveJob::QueueAdapters::AsyncAdapter 使用 并发 Ruby 线程池来调度和执行作业.

ActiveJob::QueueAdapters::AsyncAdapter uses a Concurrent Ruby thread pool to schedule and execute jobs.

当您在配置中初始化适配器时,您会传递执行程序选项,这些选项又恰好是 initialize 方法的参数Concurrent::ThreadPoolExecutor 类.

When you initialize the adapter in your configuration, you pass executor options, which in turn happen to be arguments to initialize method ofConcurrent::ThreadPoolExecutor class.

创建了 Concurrent::ThreadPoolExecutor 类有这样的方法,如:

Created instance of Concurrent::ThreadPoolExecutor class has such methods, as:

也就是说,我认为应该按照以下方式为您做:

That said, I think something along these lines should do it for you:

ActiveJob::Base
  .queue_adapter
  .instance_variable_get(:@scheduler)
  .instance_variable_get(:@async_executor)
  .public_send(:queue_length)

以上执行以下操作:

  1. 获取您的适配器
  2. 获取它的 instance_variable norelrefer="noferlow">@scheduler,指向
  3. Concurrent::ThreadPoolExecutor 的实例(Scheduler 类的实例变量 - @async_executor)
  4. 实际上可以调用上面描述的方法(queue_lengthscheduled_task_count其他)
  1. get your adapter
  2. get its instance_variable @scheduler, that points to
  3. instance of Concurrent::ThreadPoolExecutor (instance variable of Scheduler class - @async_executor)
  4. on which you can actually call the methods, described above (queue_length, scheduled_task_count and others)

虽然我没有测试过,所以请务必仔细检查是否有错别字或其他任何内容.

Though I did not test it, so make sure to double check for typos or whatsoever.

这篇关于使用 ActiveJob AsyncAdapter 列出排队的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:12