Skip to content

Commit

Permalink
Rename var. Immediatly end Vorta if running Jobs. Block add_job only …
Browse files Browse the repository at this point in the history
…when creating site.
  • Loading branch information
bastiencyr committed Sep 10, 2021
1 parent b4da415 commit 83c39ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
22 changes: 13 additions & 9 deletions src/vorta/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class _Queue(QRunnable):
def __init__(self, site_id):
super().__init__()
self.__p_queue = queue.Queue() # queues are thread-safe and reentrant in python
self.is_timeout = False
self.worker_is_running = True
self.site_id = site_id
self.timeout = 2
self.current_job = None
Expand All @@ -324,10 +324,14 @@ def cancel_all_jobs(self):
if DEBUG:
print("Cancel job")
# end process_jobs
self.run = False
self.worker_is_running = False
# cancel the current job
if self.current_job is not None:
self.current_job.cancel()
end_job = Job()
end_job.set_status(JobStatus.CANCEL)
# if job is waiting for a Job, send a cancel Job
self.add_job(end_job)

def cancel_job(self, job: Job):
# Dequeue the job
Expand All @@ -341,7 +345,7 @@ def process_jobs(self):
the site waits until a job comes. If no jobs come, a timeout ends the loop.
Since the loop is not launched in a thread, it is up to the calling function to do so.
"""
while not self.is_timeout:
while self.worker_is_running:
if DEBUG:
print("WAIT FOR A JOB")
try:
Expand All @@ -358,7 +362,7 @@ def process_jobs(self):
except queue.Empty:
if DEBUG:
print("Timeout on site: ", self.site_id)
self.is_timeout = True
self.worker_is_running = False

def run(self):
# QRunnable inherited objects has to implement run method
Expand All @@ -377,7 +381,7 @@ def __init__(self):
self.load_from_db()
# use a threadpool -> This could be changed in the future
self.threadpool = QThreadPool()
self.lock_add_job = QtCore.QMutex()
self.lock_add_site = QtCore.QMutex()

def get_value(self, key):
return self.__queues.get(key)
Expand All @@ -388,23 +392,23 @@ def load_from_db(self):

def add_job(self, job: Job):
# This function MUST BE thread safe.
self.lock_add_job.lock()
self.lock_add_site.lock()
if DEBUG:
print("Add Job on site ", job.get_site_id(), type(job.get_site_id()))

if type(job.get_site_id()) is not int:
print("get_site_id must return an integer. A ", type(job.get_site_id()), " has be returned.")
self.lock_add_job.unlock()
self.lock_add_site.unlock()
return 1

if job.get_site_id() not in self.__queues or self.__queues[job.get_site_id()].is_timeout is True:
if job.get_site_id() not in self.__queues or self.__queues[job.get_site_id()].worker_is_running is False:
if DEBUG:
print("Create a site ", job.get_site_id())
self.__queues[job.get_site_id()] = _Queue(job.get_site_id())
# run the loop
self.threadpool.start(self.__queues[job.get_site_id()]) # start call the run method.
self.lock_add_site.unlock()
self.__queues[job.get_site_id()].add_job(job)
self.lock_add_job.unlock()

# Ask to all queues to cancel all jobs. This is what the user expects when he presses the cancel button.
def cancel_all_jobs(self):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def test_repo_unlink(qapp, qtbot):

qtbot.mouseClick(main.createStartBtn, QtCore.Qt.LeftButton)
# -1 is the repo id in this test
qtbot.waitUntil(lambda: qapp.scheduler.vorta_queue.get_value(-1).is_timeout is True, **pytest._wait_defaults)
qtbot.waitUntil(lambda: qapp.scheduler.vorta_queue.get_value(-1).worker_is_running is False
, **pytest._wait_defaults)
assert main.progressText.text() == 'Add a backup repository first.'


Expand Down

0 comments on commit 83c39ba

Please sign in to comment.