Update document state exclusively in convert()

The document's state update is better update in the convert() function.
This is because this function is always called for the conversion
progress regardless of the frontend.
This commit is contained in:
deeplow 2022-10-10 15:43:11 +01:00
parent 65ac0d19c3
commit d71e230173
No known key found for this signature in database
GPG key ID: 577982871529A52A
4 changed files with 21 additions and 4 deletions

View file

@ -173,6 +173,10 @@ def exec(
if p.stdout is not None: if p.stdout is not None:
for line in p.stdout: for line in p.stdout:
(error, text, percentage) = parse_progress(document, line) (error, text, percentage) = parse_progress(document, line)
if error:
document.mark_as_failed()
if percentage == 100.0:
document.mark_as_safe()
if stdout_callback: if stdout_callback:
stdout_callback(error, text, percentage) stdout_callback(error, text, percentage)
@ -223,6 +227,7 @@ def convert(
stdout_callback: Optional[Callable] = None, stdout_callback: Optional[Callable] = None,
) -> bool: ) -> bool:
success = False success = False
document.mark_as_converting()
if ocr_lang: if ocr_lang:
ocr = "1" ocr = "1"

View file

@ -25,6 +25,7 @@ class Document:
# document conversion state # document conversion state
STATE_UNCONVERTED = enum.auto() STATE_UNCONVERTED = enum.auto()
STATE_CONVERTING = enum.auto()
STATE_SAFE = enum.auto() STATE_SAFE = enum.auto()
STATE_FAILED = enum.auto() STATE_FAILED = enum.auto()
@ -111,14 +112,23 @@ class Document:
def is_unconverted(self) -> bool: def is_unconverted(self) -> bool:
return self.state is Document.STATE_UNCONVERTED return self.state is Document.STATE_UNCONVERTED
def is_converting(self) -> bool:
return self.state is Document.STATE_CONVERTING
def is_failed(self) -> bool: def is_failed(self) -> bool:
return self.state is Document.STATE_FAILED return self.state is Document.STATE_FAILED
def is_safe(self) -> bool: def is_safe(self) -> bool:
return self.state is Document.STATE_SAFE return self.state is Document.STATE_SAFE
def mark_as_converting(self) -> None:
log.debug(f"Marking doc {self.id} as 'converting'")
self.state = Document.STATE_CONVERTING
def mark_as_failed(self) -> None: def mark_as_failed(self) -> None:
log.debug(f"Marking doc {self.id} as 'failed'")
self.state = Document.STATE_FAILED self.state = Document.STATE_FAILED
def mark_as_safe(self) -> None: def mark_as_safe(self) -> None:
log.debug(f"Marking doc {self.id} as 'safe'")
self.state = Document.STATE_SAFE self.state = Document.STATE_SAFE

View file

@ -56,10 +56,6 @@ class DangerzoneCore(object):
ocr_lang, ocr_lang,
stdout_callback, stdout_callback,
) )
if success:
document.mark_as_safe()
else:
document.mark_as_failed()
max_jobs = container.get_max_parallel_conversions() max_jobs = container.get_max_parallel_conversions()
with concurrent.futures.ThreadPoolExecutor(max_workers=max_jobs) as executor: with concurrent.futures.ThreadPoolExecutor(max_workers=max_jobs) as executor:

View file

@ -93,6 +93,12 @@ def test_mark_as_safe(sample_doc: str) -> None:
assert not d.is_unconverted() assert not d.is_unconverted()
def test_mark_as_converting(sample_doc: str) -> None:
d = Document(sample_doc)
d.mark_as_converting()
assert d.is_converting()
def test_mark_as_failed(sample_doc: str) -> None: def test_mark_as_failed(sample_doc: str) -> None:
d = Document(sample_doc) d = Document(sample_doc)
d.mark_as_failed() d.mark_as_failed()