Skip to content

Make job queue API similar to the dispatcher, add new functionality #307

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

Merged
merged 22 commits into from
Jun 29, 2016

Conversation

jh0ker
Copy link
Member

@jh0ker jh0ker commented May 25, 2016

The JobQueue API has been changed to look similar to the 4.x Dispatcher API

Instead of simply passing a callback function to the JobQueue.put method, you now have to pass a Job object, which __init__ takes now the arguments callback, interval and repeat (these were passed to put before).

The Job class exposes the boolean property enabled, which you can use to pause execution of the job temporarily, and the method schedule_removal, which marks the job for complete removal from the queue. Note: Because the JobQueue internally uses a PriorityQueue, the job will remain in the queue until it is at the front of the queue, but it will not be executed again.
In addition to the bot argument, callback functions now receive their respective Job object in an argument called job.

The JobQueue class now has a jobs method, which returns a tuple of all jobs currently in the queue.

Under the hood
Instead of continuously checking if any jobs should run, the JobQueue now waits until the next job is due. If a new job is added in the meantime, the waiting time is re-calculated as necessary. As a result, the ticking_interval argument has been removed from both JobQueue and Updater.

@jh0ker jh0ker added enhancement 📋 pending-review work status: pending-review labels May 25, 2016
@jh0ker jh0ker added this to the 5.0 milestone May 25, 2016
@coveralls
Copy link

coveralls commented May 25, 2016

Coverage Status

Changes Unknown when pulling 6b90ac9 on jobqueue-rework into * on master*.

@coveralls
Copy link

coveralls commented May 25, 2016

Coverage Status

Changes Unknown when pulling 8278779 on jobqueue-rework into * on master*.

@leandrotoledo
Copy link
Member

Assigning to @tsnoam

@coveralls
Copy link

coveralls commented May 25, 2016

Coverage Status

Changes Unknown when pulling b3142d2 on jobqueue-rework into * on master*.

@jh0ker jh0ker mentioned this pull request May 26, 2016

def run(self):
"""Executes the callback function"""
self.callback(self.job_queue.bot, self)
Copy link

Choose a reason for hiding this comment

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

will it be better if it's capable to run a job without being added to a job queue?

Copy link
Member Author

Choose a reason for hiding this comment

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

This method is used by the job queue, though of course you can also run it from outside.

Copy link

Choose a reason for hiding this comment

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

IMHO, Job.run method shouldn't depend on a job queue. Rather, I think bot should become a field in Job class. One possible solution is to specify bot as argument in __init__ method. Or it can simply be a public field that's automatically set when added to a job queue.

At least this makes the tests easier to write, since one does no longer need to add a job to a queue in order to check if this job is valid to run.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, you could always write a mock JobQueue, but I see your point. I'll get back to you about that.

Copy link
Member Author

Choose a reason for hiding this comment

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

@shouya I changed it so it takes the used bot arg as parameter and forwards it to the callback

@@ -34,10 +34,15 @@ class CallbackQueryHandler(Handler):
pass_update_queue (optional[bool]): If the handler should be passed the
update queue as a keyword argument called ``update_queue``. It can
be used to insert updates. Default is ``False``
pass_job_queue (optional[bool]): If the callback should be passed the job queue as a
keyword argument called ``job_queue``. It can be used to schedule new jobs.
Default is ``False``
"""
Copy link
Member

@tsnoam tsnoam May 26, 2016

Choose a reason for hiding this comment

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

@jh0ker
I find the whole docstring very confusing. Maybe this need rewrite as well?
example:
When set to true, the callback (what callback? the function just called? soemthing else?) will be passed to the job queue as a keyword argument called ``job_queue``. It can be used to schedule new jobs.

I think similar changes for other arguments are in order

Copy link
Member Author

@jh0ker jh0ker May 27, 2016

Choose a reason for hiding this comment

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

callback is the first argument to __init__, also you added a to there. It's actually the other way: job_queue is passed to the callback function.

Copy link
Member

Choose a reason for hiding this comment

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

@jh0ker
just shows you how much I got confused 😉
another thought:
always pass job_queue and update_queue as keyword args
then no documentation is needed here

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll change the wording ;)
Always passing it means that all callback functions have to at least accept **kwargs, which they currently don't have to (and I'd like to keep it that way)

@@ -48,15 +48,21 @@ class RegexHandler(Handler):
pass_update_queue (optional[bool]): If the handler should be passed the
update queue as a keyword argument called ``update_queue``. It can
be used to insert updates. Default is ``False``
pass_job_queue (optional[bool]): If the callback should be passed the job queue as a
keyword argument called ``job_queue``. It can be used to schedule new jobs.
Default is ``False``
"""
Copy link
Member

Choose a reason for hiding this comment

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

same comment as for callbackqueryhandler.py

jh0ker and others added 13 commits May 28, 2016 13:41
Conflicts:
	README.rst
	telegram/ext/commandhandler.py
	telegram/ext/messagehandler.py
Conflicts:
	tests/test_jobqueue.py
this test was based on timing and assumed that the JobQueue did not have
time to start processing the queue before checking the assert.
what we really should do is make sure JobQueue does not process anything
 - start the jobqueue (by default) during __init__() instead of during
   put()
 - protect self._next_peek and self.__tick with a Lock
 - rename self._start() to self._main_loop()
 - stop() is now blocking until the event loop thread exits
@jh0ker jh0ker removed the 📋 pending-review work status: pending-review label Jun 24, 2016
@jh0ker jh0ker merged commit 9fd298a into master Jun 29, 2016
@leandrotoledo leandrotoledo deleted the jobqueue-rework branch July 14, 2016 20:32
@github-actions github-actions bot locked and limited conversation to collaborators Aug 24, 2020
@Bibo-Joshi Bibo-Joshi added 🔌 enhancement pr description: enhancement and removed enhancement labels Nov 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔌 enhancement pr description: enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy