Update public source
This commit is contained in:
@@ -16,7 +16,10 @@ from cracklab.server.services.method_definition import (
|
|||||||
MethodDefinitionError,
|
MethodDefinitionError,
|
||||||
validate_method_definition,
|
validate_method_definition,
|
||||||
)
|
)
|
||||||
|
from cracklab.server.services.search_context import (
|
||||||
|
SearchContextError,
|
||||||
|
SearchContextService,
|
||||||
|
)
|
||||||
|
|
||||||
class JobIssuanceError(ValueError):
|
class JobIssuanceError(ValueError):
|
||||||
pass
|
pass
|
||||||
@@ -109,6 +112,7 @@ class JobIssuanceService:
|
|||||||
"unsupported_count": matched_count - valid_hash_count,
|
"unsupported_count": matched_count - valid_hash_count,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def issue_job(
|
def issue_job(
|
||||||
self,
|
self,
|
||||||
method_id: str,
|
method_id: str,
|
||||||
@@ -120,23 +124,10 @@ class JobIssuanceService:
|
|||||||
if limit is not None and limit <= 0:
|
if limit is not None and limit <= 0:
|
||||||
raise ValueError("limit must be greater than zero")
|
raise ValueError("limit must be greater than zero")
|
||||||
|
|
||||||
method_version_row = self._methods.get_method_version(
|
steps = self._prepare_steps(
|
||||||
method_id,
|
method_id,
|
||||||
method_version,
|
method_version,
|
||||||
)
|
)
|
||||||
if method_version_row is None:
|
|
||||||
raise JobIssuanceError(
|
|
||||||
f"Method version not found: {method_id} v{method_version}"
|
|
||||||
)
|
|
||||||
|
|
||||||
definition = self._decode_method_definition(
|
|
||||||
method_version_row["definition_json"],
|
|
||||||
)
|
|
||||||
steps = self._build_steps(
|
|
||||||
method_id,
|
|
||||||
method_version,
|
|
||||||
definition,
|
|
||||||
)
|
|
||||||
|
|
||||||
self._conn.execute("BEGIN IMMEDIATE")
|
self._conn.execute("BEGIN IMMEDIATE")
|
||||||
|
|
||||||
@@ -152,6 +143,115 @@ class JobIssuanceService:
|
|||||||
self._conn.rollback()
|
self._conn.rollback()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
return self._issue_job_with_handshakes(
|
||||||
|
method_id,
|
||||||
|
method_version,
|
||||||
|
client_id,
|
||||||
|
steps,
|
||||||
|
handshake_rows,
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
self._conn.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
def issue_job_from_search_context(
|
||||||
|
self,
|
||||||
|
token: str,
|
||||||
|
method_id: str,
|
||||||
|
method_version: int,
|
||||||
|
client_id: str | None = None,
|
||||||
|
) -> IssuedJob | None:
|
||||||
|
if not isinstance(token, str) or not token:
|
||||||
|
raise JobIssuanceError(
|
||||||
|
"Invalid search context token."
|
||||||
|
)
|
||||||
|
|
||||||
|
steps = self._prepare_steps(
|
||||||
|
method_id,
|
||||||
|
method_version,
|
||||||
|
)
|
||||||
|
|
||||||
|
token_hash = hashlib.sha256(
|
||||||
|
token.encode("utf-8"),
|
||||||
|
).hexdigest()
|
||||||
|
|
||||||
|
self._conn.execute("BEGIN IMMEDIATE")
|
||||||
|
|
||||||
|
try:
|
||||||
|
context = SearchContextService(
|
||||||
|
self._conn,
|
||||||
|
).get_context(token)
|
||||||
|
|
||||||
|
if context is None:
|
||||||
|
raise JobIssuanceError(
|
||||||
|
"Search context not found."
|
||||||
|
)
|
||||||
|
|
||||||
|
if context["expired"]:
|
||||||
|
raise JobIssuanceError(
|
||||||
|
"Search context expired. Return to Search "
|
||||||
|
"and export the results again."
|
||||||
|
)
|
||||||
|
|
||||||
|
handshake_rows = (
|
||||||
|
self._handshakes.list_eligible_for_search_context(
|
||||||
|
token_hash,
|
||||||
|
method_id,
|
||||||
|
method_version,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if not handshake_rows:
|
||||||
|
self._conn.rollback()
|
||||||
|
return None
|
||||||
|
|
||||||
|
return self._issue_job_with_handshakes(
|
||||||
|
method_id,
|
||||||
|
method_version,
|
||||||
|
client_id,
|
||||||
|
steps,
|
||||||
|
handshake_rows,
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
self._conn.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
def _prepare_steps(
|
||||||
|
self,
|
||||||
|
method_id: str,
|
||||||
|
method_version: int,
|
||||||
|
) -> list[dict[str, Any]]:
|
||||||
|
method_version_row = self._methods.get_method_version(
|
||||||
|
method_id,
|
||||||
|
method_version,
|
||||||
|
)
|
||||||
|
|
||||||
|
if method_version_row is None:
|
||||||
|
raise JobIssuanceError(
|
||||||
|
f"Method version not found: {method_id} v{method_version}"
|
||||||
|
)
|
||||||
|
|
||||||
|
definition = self._decode_method_definition(
|
||||||
|
method_version_row["definition_json"],
|
||||||
|
)
|
||||||
|
|
||||||
|
return self._build_steps(
|
||||||
|
method_id,
|
||||||
|
method_version,
|
||||||
|
definition,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _issue_job_with_handshakes(
|
||||||
|
self,
|
||||||
|
method_id: str,
|
||||||
|
method_version: int,
|
||||||
|
client_id: str | None,
|
||||||
|
steps: list[dict[str, Any]],
|
||||||
|
handshake_rows: list[sqlite3.Row],
|
||||||
|
) -> IssuedJob:
|
||||||
job_id = f"JOB-{uuid.uuid4().hex[:12].upper()}"
|
job_id = f"JOB-{uuid.uuid4().hex[:12].upper()}"
|
||||||
hash_file_name = f"{job_id}.22000"
|
hash_file_name = f"{job_id}.22000"
|
||||||
|
|
||||||
@@ -237,10 +337,6 @@ class JobIssuanceService:
|
|||||||
|
|
||||||
self._conn.commit()
|
self._conn.commit()
|
||||||
|
|
||||||
except Exception:
|
|
||||||
self._conn.rollback()
|
|
||||||
raise
|
|
||||||
|
|
||||||
return IssuedJob(
|
return IssuedJob(
|
||||||
job_id=job_id,
|
job_id=job_id,
|
||||||
method_id=method_id,
|
method_id=method_id,
|
||||||
|
|||||||
Reference in New Issue
Block a user