Skip to content

Commit

Permalink
Fix get_next_job
Browse files Browse the repository at this point in the history
Used step on regenerate,
Recalculate job on each call
  • Loading branch information
sbrunner committed Jul 14, 2023
1 parent c1dd030 commit dc61b65
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
5 changes: 2 additions & 3 deletions scan_to_paperless/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,7 @@ def main() -> None:
status.write()

while True:
name, job_type = status.get_next_job()
name, job_type, step = status.get_next_job()

if job_type in (JobType.TRANSFORM, JobType.ASSISTED_SPLIT, JobType.FINALIZE):
assert name is not None
Expand All @@ -1815,12 +1815,11 @@ def main() -> None:
config: schema.Configuration = yaml.load(config_file.read())

if "steps" not in config or not config["steps"]:
step: schema.Step = {
step = {
"sources": config["images"],
"name": "transform",
}
config["steps"] = [step]
step = config["steps"][-1]

next_step = None
if job_type == JobType.TRANSFORM:
Expand Down
50 changes: 28 additions & 22 deletions scan_to_paperless/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import natsort
from ruamel.yaml.main import YAML

from scan_to_paperless import process_schema

_WAITING_STATUS_NAME = "Waiting validation"
_WAITING_STATUS_DESCRIPTION = """<div class="sidebar-box"><p>You should validate that the generate images are correct ({generated_images}).<br />
If the result is correct remove the <a href="./{name}/REMOVE_TO_CONTINUE" target="_blank"><code>REMOVE_TO_CONTINUE</code></a> file.</p>
Expand Down Expand Up @@ -63,6 +65,7 @@ class _Folder(NamedTuple):
nb_images: int
status: str
details: str
step: Optional[process_schema.Step]


class JobType(Enum):
Expand All @@ -79,9 +82,6 @@ class JobType(Enum):
class Status:
"""Manage the status file of the progress."""

current_job_type = JobType.NONE
current_job_list: list[str] = []

def __init__(self, no_write: bool = False) -> None:
"""Construct."""

Expand All @@ -93,7 +93,6 @@ def __init__(self, no_write: bool = False) -> None:
self._global_status = "Starting..."
self._global_status_update = datetime.datetime.utcnow().replace(microsecond=0)
self._start_time = datetime.datetime.utcnow().replace(microsecond=0)
self._last_scan = datetime.datetime.utcnow()
self._current_folder: Optional[str] = None
self.scan()

Expand All @@ -118,15 +117,22 @@ def set_current_folder(self, name: Optional[str]) -> None:
if write:
self.write()

def set_status(self, name: str, nb_images: int, status: str, details: str = "") -> None:
def set_status(
self,
name: str,
nb_images: int,
status: str,
details: str = "",
step: Optional[config.ConfigurationStep] = None,
) -> None:
"""Set the status of a folder."""

# Config file name
if name.endswith("/config.yaml"):
name = os.path.basename(os.path.dirname(name))
if nb_images <= 0 and name in self._status:
nb_images = self._status[name].nb_images
self._status[name] = _Folder(nb_images, html.escape(status), details)
self._status[name] = _Folder(nb_images, html.escape(status), details, step)

if self.no_write:
print(f"{name}: {status}")
Expand All @@ -137,8 +143,6 @@ def set_status(self, name: str, nb_images: int, status: str, details: str = "")
def scan(self) -> None:
"""Scan for changes for waiting documents."""

self._last_scan = datetime.datetime.utcnow()

codes_folder = os.environ.get("SCAN_CODES_FOLDER", "/scan-codes")
if codes_folder[-1] != "/":
codes_folder += "/"
Expand Down Expand Up @@ -267,17 +271,24 @@ def _update_status(self, name: str) -> None:
)

else:
status_from_step = False
if len(config.get("steps", [])) >= 1:
self.set_status(name, -1, "Waiting to " + config["steps"][-1]["name"])
else:
for step in reversed(config["steps"]):
all_present = True
for source in step["sources"]:
if not os.path.exists(source):
all_present = False
break
if all_present:
self.set_status(name, -1, "Waiting to " + config["steps"][-1]["name"], step=step)
status_from_step = True
break
if not status_from_step:
self.set_status(name, -1, _WAITING_TO_TRANSFORM_STATUS)

def write(self) -> None:
"""Write the status file."""

if self._last_scan < datetime.datetime.utcnow() - datetime.timedelta(minutes=1):
self.scan()

if self.no_write:
return

Expand All @@ -301,9 +312,10 @@ def write(self) -> None:
)
)

def get_next_job(self) -> tuple[Optional[str], JobType]:
def get_next_job(self) -> tuple[Optional[str], JobType, Optional[process_schema.Step]]:
"""Get the next job to do."""

self.scan()
self.write()
job_types = [
(JobType.TRANSFORM, _WAITING_TO_TRANSFORM_STATUS),
Expand All @@ -319,12 +331,6 @@ def get_next_job(self) -> tuple[Optional[str], JobType]:
for name, folder in self._status.items():
if folder.status == waiting_status:
self.current_job_list.append(name)
return name, job_type, folder.step

if not self.current_job_list:
self.current_job_type = JobType.CODE
self.current_job_list = list(self._codes)

if not self.current_job_list:
self.current_job_type = JobType.NONE

return self.current_job_list.pop(0) if self.current_job_list else None, self.current_job_type
return None, JobType.NONE, None

0 comments on commit dc61b65

Please sign in to comment.