final
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import hashlib
|
||||
import json
|
||||
import resource
|
||||
import sys
|
||||
from PIL import UnidentifiedImageError, Image
|
||||
from .config import Settings
|
||||
from .repository import Repository
|
||||
from .storage import Storage
|
||||
from .images import ImagePipeline
|
||||
from .faults import inject
|
||||
|
||||
|
||||
def write_effects(storage, job, data, settings):
|
||||
prefix = f"results/{job['attachment_id']}/v{job['pipeline_version']}"
|
||||
original = prefix + "/original-" + job["checksum"]
|
||||
storage.write_result(original, data, job["content_type"])
|
||||
variants = []
|
||||
for variant in ImagePipeline(settings, job["pipeline_version"]).render(data):
|
||||
body = variant.pop("data")
|
||||
key = prefix + "/" + variant["name"] + "-" + hashlib.sha256(body).hexdigest()
|
||||
item = storage.write_result(key, body, variant["content_type"])
|
||||
variants.append(
|
||||
dict(item, name=variant["name"], width=variant["width"], height=variant["height"])
|
||||
)
|
||||
return original, variants
|
||||
|
||||
|
||||
def process(repository, settings, job):
|
||||
storage = Storage(settings)
|
||||
with repository.connection() as fence:
|
||||
fence.execute("SELECT pg_advisory_xact_lock_shared(741204)")
|
||||
inject(repository, job, "before_effects")
|
||||
data = storage.read_original(job)
|
||||
original, variants = write_effects(storage, job, data, settings)
|
||||
inject(repository, job, "after_effects")
|
||||
published = repository.publish_result(job, original, variants)
|
||||
inject(repository, job, "after_commit")
|
||||
if published:
|
||||
storage.remove_staging(job, repository)
|
||||
print(
|
||||
json.dumps(
|
||||
{"event": "result", "version": job["pipeline_version"], "published": published}
|
||||
),
|
||||
flush=True,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
resource.setrlimit(resource.RLIMIT_AS, (768 * 1024 * 1024, 768 * 1024 * 1024))
|
||||
settings = Settings()
|
||||
repository = Repository(settings)
|
||||
message = json.loads(sys.argv[1])
|
||||
job = repository.claim(message)
|
||||
if not job:
|
||||
if not repository.delivery_done(message):
|
||||
sys.exit(75)
|
||||
print(json.dumps({"event": "duplicate_or_stale"}), flush=True)
|
||||
return
|
||||
try:
|
||||
process(repository, settings, job)
|
||||
except (
|
||||
ValueError,
|
||||
UnidentifiedImageError,
|
||||
Image.DecompressionBombError,
|
||||
Image.DecompressionBombWarning,
|
||||
MemoryError,
|
||||
):
|
||||
repository.fail(job, permanent=True, code="invalid_image")
|
||||
except Exception as error:
|
||||
repository.fail(job)
|
||||
print(json.dumps({"event": "retry", "error_type": type(error).__name__}), flush=True)
|
||||
if not repository.delivery_done(message):
|
||||
sys.exit(75)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user