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

Raise ResultNotFound when Job.result() finds no job and no result #364

Merged
merged 6 commits into from
Dec 2, 2022

Conversation

iamlikeme
Copy link
Contributor

Job.result() assumed that a result will eventually show up in Redis. This assumption does not hold for jobs which are not in the queue and jobs which do not keep result. In such cases Job.result() would hang forever (or until the optional timeout is reached).

This PR changes Job.result() so that it raises a new exception ResultNotFound, rather than waiting forever, if result is not found and the job is not in the queue.

Job.result() assumed that a result will eventually show up in Redis.
This assumption does not hold for jobs which are not in the queue
and jobs which do not keep result. In such cases Job.result() would
hang forever (or until the optional timeout is reached).

This PR changes Job.result() so that it raises a new exception
ResultNotFound, rather than waiting forever, if result is not found
and the job is not in the queue.
@iamlikeme iamlikeme marked this pull request as ready for review November 20, 2022 21:09
@codecov
Copy link

codecov bot commented Nov 20, 2022

Codecov Report

Merging #364 (96b094d) into main (1495be6) will increase coverage by 0.01%.
The diff coverage is 100.00%.

❗ Current head 96b094d differs from pull request most recent head 019450a. Consider uploading reports for the commit 019450a to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #364      +/-   ##
==========================================
+ Coverage   98.80%   98.81%   +0.01%     
==========================================
  Files          11        9       -2     
  Lines        1007      932      -75     
  Branches      185      172      -13     
==========================================
- Hits          995      921      -74     
  Misses          6        6              
+ Partials        6        5       -1     
Impacted Files Coverage Δ
arq/jobs.py 98.14% <100.00%> (+0.14%) ⬆️
arq/__init__.py
arq/cron.py
arq/worker.py 99.56% <0.00%> (+0.22%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1495be6...019450a. Read the comment docs.

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good idea to me, just a few things to change to change.

Also can you add some info about this to result docstring.

arq/jobs.py Outdated Show resolved Hide resolved
arq/jobs.py Outdated Show resolved Hide resolved
@samuelcolvin
Copy link
Member

I'll try to make a new release tomorrow, would be great if we could include this.

@iamlikeme
Copy link
Contributor Author

I updated docstring and error message. If you want to change wording feel free to push directly to this branch.

@iamlikeme
Copy link
Contributor Author

The new ResultNotFound exception may now occur in Job.abort(). When it does, we don't know whether the job was cancelled or not (e.g. the job might have been cancelled, but if it does not keep result we wouldn't know). There are several options for what we can do with it:

  • not handle it, i.e. let Job.abort() raise ResultNotFound
  • catch and return False (i.e. job wasn't cancelled)
  • catch and return True (i.e. job was cancelled)
  • catch and return something else, e.g. None - this would be a breaking change

The first two options seem most sensible to me and I chose to catch and return False. Let me know if you'd rather have it changed.

arq/jobs.py Outdated Show resolved Hide resolved
@samuelcolvin samuelcolvin enabled auto-merge (squash) December 2, 2022 11:04
@samuelcolvin
Copy link
Member

Great, thanks so much.

The first two options seem most sensible to me and I chose to catch and return False

Agreed, I think returning False is most correct since it's matches the docstring: True if the job aborted properly, False otherwise

@samuelcolvin samuelcolvin merged commit 12456ee into python-arq:main Dec 2, 2022
@shahakL
Copy link

shahakL commented Dec 11, 2022

Hi @iamlikeme! Quick question about this PR, I noticed this part in Job.result():

async with self._redis.pipeline(transaction=True):
    v = await self._redis.get(result_key_prefix + self.job_id)
    s = await self._redis.zscore(self._queue_name, self.job_id)

A transaction is created but not used for the two queries; is that on purpose? Couldn't it create a possible false ResultNotFound if the job is done and removed from the queue after the first line but before the second?

@iamlikeme iamlikeme deleted the job-result-not-found branch December 11, 2022 19:50
@iamlikeme
Copy link
Contributor Author

Hi @shahakL! You are completely right. What a great catch, thank you! The fix is in #374.

@shahakL
Copy link

shahakL commented Dec 11, 2022

@iamlikeme Thank you for your contributions!

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.

3 participants