oldmoe / litestack

MIT License
1.02k stars 56 forks source link

Remove jobs enqueued with perform_later #93

Closed danhealy closed 4 months ago

danhealy commented 7 months ago

I'd like to be able to:

Is there an easy way to implement this in my app code? I think this would need to be a new set of SQL statements?

seocahill commented 6 months ago

I was looking for this myself https://github.com/oldmoe/litestack/blob/master/lib/litestack/litequeue.rb

tl;dr There is an extant ruby api

lq = Litequeue.new
lq.queues_info
lq.clear
...etc
seocahill commented 6 months ago

Here's some generated docs if anyone wants to add to wiki

Litequeue Usage Guide

Litequeue is a simple and efficient queueing system for Ruby applications. This guide covers how to use Litequeue for creating queues, adding, and removing values.

Creating a Litequeue Instance

To start using Litequeue, you first need to create an instance of the Litequeue class. You can optionally pass a hash of options to customize the queue settings such as path, mmap_size, and sync.

queue = Litequeue.new

Adding Items to a Queue

To add an item to a queue, use the push method. You can specify the item's delay time in seconds (default is 0) and a queue name (default is "default"). This method returns a unique job ID.

id = queue.push("somevalue", 2) # The value will be ready to pop in 2 seconds

To add an item back into the queue possibly with a new value or delay, use the repush method with the job ID.

queue.repush(id, "newvalue", 3)

Popping Items from a Queue

To remove and return the next item from a queue, use the pop method. You can specify a queue name and limit the number of items to pop (default is 1).

item = queue.pop # By default, pops from the "default" queue

If you have multiple queues, specify the queue name to pop items from a specific queue.

item = queue.pop("myQueue")

Deleting Items from a Queue

To delete an item from a queue before it is popped, use the delete method with the item's job ID.

queue.delete(id)

Clearing Queues

To delete all items from all queues or from a specific queue, use the clear method. If no queue name is given, all entries in all queues are deleted.

queue.clear # Clears all queues
queue.clear("myQueue") # Clears items from "myQueue" only

Counting Items in a Queue

To get a count of items in all queues or in a specific queue, use the count method.

total = queue.count # Count items in all queues
specific_total = queue.count("myQueue") # Count items in "myQueue"

Selecting a Particular Queue

To interact with a specific queue (e.g., adding, popping, deleting items), simply specify the queue name as an argument in the push, pop, delete, or count methods.


# Adding to a specific queue
queue.push("value", 0, "specificQueue")

# Popping from a specific queue
item = queue.pop("specificQueue")

# Counting items in a specific queue
count = queue.count("specificQueue")
danhealy commented 6 months ago

This guide is great! Unfortunately, it only works on jobs currently enqueued and not on jobs scheduled to be performed later.

oldmoe commented 4 months ago

Hi Dan, I am not sure what is the distinction you have between jobs enqueued and jobs that are going to be performed later? Any job that is scheduled is by definition enqueued and should be found via the queue.count("specific_queue"), also a specific queue can be cleared using queue.clear("specific_queue")

oldmoe commented 4 months ago

Here's some generated docs if anyone wants to add to wiki

Litequeue Usage Guide

Litequeue is a simple and efficient queueing system for Ruby applications. This guide covers how to use Litequeue for creating queues, adding, and removing values.

Creating a Litequeue Instance

To start using Litequeue, you first need to create an instance of the Litequeue class. You can optionally pass a hash of options to customize the queue settings such as path, mmap_size, and sync.

queue = Litequeue.new

Adding Items to a Queue

To add an item to a queue, use the push method. You can specify the item's delay time in seconds (default is 0) and a queue name (default is "default"). This method returns a unique job ID.

id = queue.push("somevalue", 2) # The value will be ready to pop in 2 seconds

To add an item back into the queue possibly with a new value or delay, use the repush method with the job ID.

queue.repush(id, "newvalue", 3)

Popping Items from a Queue

To remove and return the next item from a queue, use the pop method. You can specify a queue name and limit the number of items to pop (default is 1).

item = queue.pop # By default, pops from the "default" queue

If you have multiple queues, specify the queue name to pop items from a specific queue.

item = queue.pop("myQueue")

Deleting Items from a Queue

To delete an item from a queue before it is popped, use the delete method with the item's job ID.

queue.delete(id)

Clearing Queues

To delete all items from all queues or from a specific queue, use the clear method. If no queue name is given, all entries in all queues are deleted.

queue.clear # Clears all queues
queue.clear("myQueue") # Clears items from "myQueue" only

Counting Items in a Queue

To get a count of items in all queues or in a specific queue, use the count method.

total = queue.count # Count items in all queues
specific_total = queue.count("myQueue") # Count items in "myQueue"

Selecting a Particular Queue

To interact with a specific queue (e.g., adding, popping, deleting items), simply specify the queue name as an argument in the push, pop, delete, or count methods.

# Adding to a specific queue
queue.push("value", 0, "specificQueue")

# Popping from a specific queue
item = queue.pop("specificQueue")

# Counting items in a specific queue
count = queue.count("specificQueue")

I added the wiki page, quite late but finally got time to give the repo some love, thanks a lot for this!

danhealy commented 4 months ago

Hi Dan, I am not sure what is the distinction you have between jobs enqueued and jobs that are going to be performed later? Any job that is scheduled is by definition enqueued and should be found via the queue.count("specific_queue"), also a specific queue can be cleared using queue.clear("specific_queue")

I really appreciate the reply! edit: I was definitely a little confused & expecting that they would be stored separately somehow, sorry about that. Hopefully this illustrates my problem.

Imagine you have enqueued a job for later and have not saved the job id: MyJob.set(wait: 1.day).perform_later(important: :not_really)

Then, you have another really important job that must be run, this prevents using clear: MyJob.set(wait: 1.hour).perform_later(important: :very)

How do you find the ID of the first job?

This kind of works: queue.send(:run_sql, "SELECT * FROM queue WHERE iif(?1 IS NOT NULL, name = ?1, TRUE)", "my_queue")

but it's not an ideal solution for obvious reasons!

oldmoe commented 4 months ago

I am adding support for Litequeue#find() and Litejobqueue#find() both expect a hash of search options that can include the following:

opts = { 
  created_at: [from_unix_time, to_unix_time], 
  fire_at: [from_unix_time, to_unix_time], 
  queue: "queue_name", 
  klass: "class_name_or_any_part_of_it",
  params: "a_string_that_appears_in_params" # params are serialized so even numerical values can be found that way
  dir: "desc" # only useful value is "desc", returns results in reverse creation order, newest first, default is oldest first
}

Any supplied field can be null, also any value in the time arrays can be null

danhealy commented 4 months ago

@oldmoe Thank you so much!! I'll give it a try.