List queued tasks with ActiveJob AsyncAdapter
Asked Answered
B

1

8

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

Baptism answered 13/10, 2016 at 22:49 Comment(2)
hey, let me know whether below suggestion worked for you if you already tried it - I'm kind'a curious but not yet had a moment to test it :) (may be during weekend)Harker
Same here :) Couldn't test it today. Let me see if I can get to that tomorrow morning ;)Baptism
H
7

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

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

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

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

  • queue_length - The number of tasks in the queue awaiting execution.
  • scheduled_task_count - The number of tasks that have been scheduled for execution on the pool since construction.

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)

Above does the following:

  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.

Harker answered 13/10, 2016 at 23:51 Comment(4)
Ok, I just tried. It doesn't seem to work. It returns 0 always.Baptism
@Baptism think of it, it returns an integer, thus I'm pretty sure it is working in a sense, that it calls the correct method. Did you try another methods? Also, are you 1000% sure, it shouldn't return 0?Harker
I tried this solution. I tried queue_length, scheduled_task_count, completed_task_count. All of them always return 0Hautemarne
Getting the async_executor of the adapter is returning a Concurrent::ThreadPoolExecutor instance. The problem is that Concurrent::ThreadPoolExecutor counts aren't updated for some reason.Havildar

© 2022 - 2024 — McMap. All rights reserved.