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

Selecting jobs based on job's name #934

Closed
wants to merge 4 commits into from
Closed

Selecting jobs based on job's name #934

wants to merge 4 commits into from

Conversation

paradox70
Copy link
Contributor

This new function select_job(name=) returns a tuple of jobs with the given name that are currently in the JobQueue

By selecting jobs based on their names users can manage jobs in job_queue.
This new function return a tuple of jobs with the same name given as parameter.
@codecov
Copy link

codecov bot commented Dec 15, 2017

Codecov Report

Merging #934 into master will decrease coverage by 0.02%.
The diff coverage is 50%.

@@            Coverage Diff             @@
##           master     #934      +/-   ##
==========================================
- Coverage    91.8%   91.78%   -0.03%     
==========================================
  Files         103      103              
  Lines        4040     4042       +2     
  Branches      638      639       +1     
==========================================
+ Hits         3709     3710       +1     
- Misses        193      194       +1     
  Partials      138      138
Impacted Files Coverage Δ
telegram/ext/jobqueue.py 91.44% <50%> (-0.45%) ⬇️

@Eldinnie
Copy link
Member

Hi,

Thanks for your contribution. I would like to see some tests for the new method (selecting none, one and multiple). This will also hit the coverage target.
After that looks good to me.

Copy link
Member

@tsnoam tsnoam left a comment

Choose a reason for hiding this comment

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

See comment on the code, but basically this code is not thread safe and a different approach is required.

def select_job(self, name):
"""Returns a tuple of jobs with the given name that are currently in the ``JobQueue``"""

return tuple(job[1] for job in self.queue.queue if job and job[1].name==name)
Copy link
Member

Choose a reason for hiding this comment

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

  1. Accessing self.queue.queue is not thread safe. We need a different approach here.
  2. code style: you're missing spaces around ==...

@paradox70
Copy link
Contributor Author

@Eldinnie I'm not sure, what do you mean about selecting none, one and multiple. So I run the following codes to test:

Codes:

import time
from telegram.ext import Updater

TOKEN = "412548814:AAHhkKLF5rStuI1A_T5k9hb0-1qyiUQ-TY0" #fake token

def print_job():
	pass

def main():
	updater = Updater(TOKEN)
	j = updater.job_queue

	j.run_once(print_job, 10, name='job1')
	j.run_once(print_job, 10, name='job2')
	j.run_once(print_job, 10, name='job3')

	try:
		print('text11:', j.select_job())
	except Exception as e:
		print('test12: ',e)

	try:
		print('text21:', j.select_job(name=None))
	except Exception as e:
		print('test22: ',e)

	try:
		print('test31:', j.select_job('job1'))
	except Exception as e:
		print('test32:', e)

	try:
		print('test41', j.select_job('job1', 'job2'))
	except Exception as e:
		print('test42', e)

	try:
		print('test51:', job_queue.select_job('job4'))
	except Exception as e:
		print('test52:', e)

	print('job_list = ', j.jobs())

	time.sleep(10)

if __name__ == '__main__':
	main()

outputs:

test12:  select_job() missing 1 required positional argument: 'name'
text21: ()
test31: (<telegram.ext.jobqueue.Job object at 0x7fd0ae7b3828>,)
test42 select_job() takes 2 positional arguments but 3 were given
test51: ()
job_list =  (<telegram.ext.jobqueue.Job object at 0x7fd0ae7b3828>, 
             <telegram.ext.jobqueue.Job object at 0x7fd0ae7b3940>,
             <telegram.ext.jobqueue.Job object at 0x7fd0ae7b3a58>)

@tsnoam At sleep duration I run the same script, multiple time by my machine and it seems thread safe. If this method is not thread safe so the method jobs() is not thread safe too, and I prefer to ignore this contribution.

@Eldinnie
Copy link
Member

@paradox70 Look in the test folder for test_jobqueue. The script you pasted is basicaly what I would like to see in there. Except for prints use assert and for try: ... except:... use pytest.raises.

Regarding the threadsafety, we will have to look into that a bit more. I also alerted @tsnoam it's practically identical to the jobs attribute.

@tsnoam
Copy link
Member

tsnoam commented Jan 9, 2018

@paradox70
you are correct, JobQueue.jobs() is using the same mechanism as well and it isn't thread safe either and must be fixed.
we need to consider this issue, but for the moment we cannot accept your PR (we'll leave it open though).

@tsnoam
Copy link
Member

tsnoam commented Jan 20, 2018

@paradox70
We've fixed the blocking issue of thread safety.
As I see it, we'll be ready to merge your PR given the following changes:

  1. Rebase on top of master and implement the same locking mechanism used with JobQueue.jobs.
  2. Add unitests.
  3. I prefer the name get_jobs_by_name rather than select_jobs.

@tsnoam
Copy link
Member

tsnoam commented Feb 18, 2018

Due to lack of response from PR creator, closing the PR.
We'll continue the work on a different PR.

@tsnoam tsnoam closed this Feb 18, 2018
@github-actions github-actions bot locked and limited conversation to collaborators Aug 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants