Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions matrix/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ async def morning_update() -> None:
def start(self) -> None:
"""Start the scheduler."""
self.scheduler.start()

def unschedule(self, name: str) -> None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be more logical to have this function after the schedule function rather than at the end of the file. This will ensure that whomever is reading the file can read it like a story.

"""Remove a registered job by its name."""
self.jobs = [job for job in self.jobs if job.name != name]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As you can see in line 17 of this file, the function jobs(...) has the @property decorator. This decorator turns a function into a getter with a read-only attribute. Please see this link to learn more about the decorator.

So, because of the jobs method being read-only, doing self.jobs = ... will not work and will result in an error (as you can see in the build CI). This could have been caught if you had written a unit test for it, which you should in the following commit.

You should also always run your changes locally to ensure your implementation works as expected. Running this PR locally would have also shown that this isn’t valid, as the build would have failed.

Lastly, I don’t think the list comprehension is actually doing what it is supposed to do.
Our Scheduler is a wrapper on top of the APScheduler's AsyncIOScheduler, which means we can use everything from it. Looking at the scheduler api documentation, you can see that there is the .remove_job(...) that you can use (please see APScheduler docs). Your current implementation may remove the event from the list of jobs, but it does not ensure that the job is stopped and properly removed, which, by using the handler from APSchedule, will guarantee that it does.

Loading