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

Fixing jobstore.py #73

Merged
merged 6 commits into from May 31, 2023
Merged

Fixing jobstore.py #73

merged 6 commits into from May 31, 2023

Conversation

Giulianini
Copy link
Contributor

Problem: pause and rescheduler jobs (serialized and deserialized obviously)

Structure

A TGJob tg_job is a telegram job and is based on a constructor with 5 args and a APScheduler Job job field

  • tg_job = TGJob(callback, data, name, chat_id, user_id): the actual callback, data, ecc we set
  • tg_job.job =APJob(callback=tg_job.run, args[application], name=name, **job_kwargs)

So the TGJob wraps the APjob setting the application (became the bot context) as parameter for the tg_job.run function when the callback will be executed.

Serialization

APScheduler so will try to serialize the job instance but application is a arg so it would have been serialized. Here we have our implementation of SQLAlchemyJobStore in jobstore.py.

Adapter of SQLAlchemyJobStore

When a job is going to be serialized we:

  • copy the job
  • get the tg_job instantiating the self of the job callback (tg_job.run remember?) if the tg_job is in memory
  • settings the job args to tg_job parameters [callback, data, name, chat_id, user_id] so replacing the unique [application] arg.

then the job is serialized, remember with these key fields:

  • callback = tg_job.run function literal (reference to the function not the instance obiouvsly)
  • args = [callback, data, name, chat_id, user_id]

Deserialization

When we want to reschedule or pause an existing job, we get the job:

  • job deserialized
  • create a tg_job = TGGob(job.args) (5 args)
  • modify the job args to be 2 (not only application now). tg_job and application. The first is going to be the self argument, the second is the standard application argument that we started with before serialization

Serialization of a deserialized job

When we come to the serialization step this time we do not have a tg_job instance when trying to get the self from the job.callback function literal. __self__ does not exists.

Solution

So get the tg_job form the first argument we provided during deserialization that is the instance we need :)

…lized during modify) we have 2 args (tg_job, application), in this case the first arg is the tg_job.
@Holmes555
Copy link
Contributor

Did the same, before seeing your PR

@Bibo-Joshi
Copy link
Member

Hi. Thanks very much for taking the time for the analysis and the PR! Everything looks very good to me, just the pre-commit tests are failing. Could you mind addressing them?

@Giulianini
Copy link
Contributor Author

Hi. Thanks very much for taking the time for the analysis and the PR! Everything looks very good to me, just the pre-commit tests are failing. Could you mind addressing them?

Sure will fix the formatting to pass the tests

@Bibo-Joshi Bibo-Joshi merged commit 9232cf9 into python-telegram-bot:main May 31, 2023
14 checks passed
@Bibo-Joshi
Copy link
Member

Thank you for the contribution!

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.

None yet

3 participants