Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support custom key in queue.enqueue even when runAt is not specified #8

Merged
merged 1 commit into from
Oct 29, 2024

Conversation

noxify
Copy link
Contributor

@noxify noxify commented Oct 29, 2024

Add support to specify a key inside the enqueue method without specifying the runAt.

Previously when you have called:

  const payload = {
    entity: entity,
    entityId: entityId,
  }

  const options = {key: "custom-key"}

  await queue.enqueue(payload, options)

The queue entry was created, but without the specified key.

Based on the code, you had to specify the runAt, which would also trigger some check/delete logic.

With this change, you can now specify the key without running the additional checks.

My use case is/was the following:

I have a queue to named "quality check". Here I have a scheduled job ( daily base ). Via an user interface I can trigger also a "re-check" for an dedicated record.

I wanted to use the the same queue ( and the script logic ).
Inside the script I planned to use the key to decide what I have to do ( run check for all records vs. run it only for one record )


Not sure if this is something what you want to implement - if not, feel free to close this PR.

Alternatives solutions:

  • I set the runAt property
  • I use a dedicated queue for the "one-time jobs"
  • Something else?

I have added a .env.example to make it easier for others if they want to contribute :)
Via pnpm with-env pnpm dev you can run it without getting the error "DATABASE_URL is not defined"

@mgcrea mgcrea merged commit d8b29db into mgcrea:master Oct 29, 2024
4 checks passed
@mgcrea
Copy link
Owner

mgcrea commented Oct 29, 2024

Thanks for the nice detailed PR with updated test!

Regarding the env, I'm not sure why it is not autoloaded on your side (maybe windows?) but I don't need to do anything for vitest/prisma to pickup the DATABASE_URL env var once it is defined in an .env file.

Merging anyway and we can sort it out later.

@noxify
Copy link
Contributor Author

noxify commented Oct 29, 2024

thanks for merging and the kind words.

I'm on OSX and it seems to be only a problem with pnpm dev.
If I use pnpm test, everything seems to work as expected ( = autoloading the .env )

@noxify
Copy link
Contributor Author

noxify commented Oct 29, 2024

For the tests - Do you think https://github.com/morintd/prismock could work, too?

We're using it internally in our unit tests ( e.g. to test the rbac definitions with casl and their prisma extension ).

@mgcrea
Copy link
Owner

mgcrea commented Oct 29, 2024

thanks for merging and the kind words.

I'm on OSX and it seems to be only a problem with pnpm dev. If I use pnpm test, everything seems to work as expected ( = autoloading the .env )

Strange, maybe try to update your pnpm install if it is old?

For the tests - Do you think https://github.com/morintd/prismock could work, too?

We're using it internally in our unit tests ( e.g. to test the rbac definitions with casl and their prisma extension ).

Not sure but I think I'd rather keep testing against a live PostgreSQL vs. a mock since the purpose of this library is tightly coupled with PostgreSQL internals and the SKIP LOCKED mechanics.

I have just released a new version with your changes:

https://github.com/mgcrea/prisma-queue/releases/tag/1.12.0

@noxify
Copy link
Contributor Author

noxify commented Oct 29, 2024

Strange, maybe try to update your pnpm install if it is old?

latest version already installed :) Using only pnpm test works for me :)

Not sure but I think I'd rather keep testing against a live PostgreSQL vs. a mock since the purpose of this library is tightly coupled with PostgreSQL internals and the SKIP LOCKED mechanics.

Understood.

I have just released a new version with your changes:

Already installed 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants