Fixing jobstore.py #73
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem: pause and rescheduler jobs (serialized and deserialized obviously)
Structure
A TGJob
tg_jobis a telegram job and is based on a constructor with 5 args and a APScheduler Jobjobfieldtg_job = TGJob(callback, data, name, chat_id, user_id): the actual callback, data, ecc we settg_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.runfunction when the callback will be executed.Serialization
APScheduler so will try to serialize the
jobinstance butapplicationis a arg so it would have been serialized. Here we have our implementation ofSQLAlchemyJobStorein jobstore.py.Adapter of SQLAlchemyJobStore
When a job is going to be serialized we:
tg_jobinstantiating theselfof the job callback (tg_job.run remember?) if the tg_job is in memoryjobargs 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:
tg_job.runfunction literal (reference to the function not the instance obiouvsly)[callback, data, name, chat_id, user_id]Deserialization
When we want to reschedule or pause an existing job, we get the job:
jobdeserializedtg_job = TGGob(job.args)(5 args)jobargs to be 2 (not only application now).tg_jobandapplication. The first is going to be theselfargument, the second is the standardapplicationargument that we started with before serializationSerialization of a deserialized job
When we come to the serialization step this time we do not have a
tg_jobinstance when trying to get theselffrom thejob.callbackfunction literal.__self__ does not exists.Solution
So get the
tg_jobform the first argument we provided during deserialization that is the instance we need :)