-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Conversation
Changes Unknown when pulling 6b90ac9 on jobqueue-rework into * on master*. |
…on-telegram-bot into jobqueue-rework
Changes Unknown when pulling 8278779 on jobqueue-rework into * on master*. |
Assigning to @tsnoam |
Changes Unknown when pulling b3142d2 on jobqueue-rework into * on master*. |
|
||
def run(self): | ||
"""Executes the callback function""" | ||
self.callback(self.job_queue.bot, self) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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`` | |||
""" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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`` | |||
""" |
There was a problem hiding this comment.
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
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
The
JobQueue
API has been changed to look similar to the 4.xDispatcher
APIInstead of simply passing a callback function to the
JobQueue.put
method, you now have to pass aJob
object, which__init__
takes now the argumentscallback
,interval
andrepeat
(these were passed toput
before).The
Job
class exposes the boolean propertyenabled
, which you can use to pause execution of the job temporarily, and the methodschedule_removal
, which marks the job for complete removal from the queue. Note: Because theJobQueue
internally uses aPriorityQueue
, 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 respectiveJob
object in an argument calledjob
.The
JobQueue
class now has ajobs
method, which returns atuple
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, theticking_interval
argument has been removed from bothJobQueue
andUpdater
.