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

Make JobManager thread-safe #51

Open
vprusso opened this issue Dec 3, 2024 · 0 comments
Open

Make JobManager thread-safe #51

vprusso opened this issue Dec 3, 2024 · 0 comments
Labels
feature New functionality

Comments

@vprusso
Copy link
Contributor

vprusso commented Dec 3, 2024

An instance of the JobManager class is not thread-safe. This can lead to race conditions, deadlocks, etc.

We can do this by making using of the threading module in Python. From there, we would add a lock variable to the JobManager class:

class JobManager:
    def __init__(self, file_path: str = DEFAULT_FILE_PATH):
        self.file_path = file_path
        self.lock = threading.Lock() 
        self._load_jobs()

And any time we save, add, remove, etc. jobs, we want to use the with construct to invoke and use the lock. For instance:

    def remove_job(self, job_id: str):
        """Removes a job by its ID."""
        with self.lock:
            self.jobs = [job for job in self.jobs if job["id"] != job_id]
            self._save_jobs()

etc.

@vprusso vprusso added the feature New functionality label Dec 3, 2024
@vprusso vprusso changed the title Make JobManager threadsafe Make JobManager thread-safe Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality
Projects
None yet
Development

No branches or pull requests

1 participant