55import sys
66import time
77import uuid
8- from io import BytesIO
8+ from importlib . metadata import version as get_version
99from pathlib import Path
1010from typing import TypedDict
1111
@@ -56,6 +56,13 @@ class ImageExifExtractionJob(BaseJob):
5656 """Represent an ImageExifExtractionJob."""
5757
5858
59+ @app .command ()
60+ def version () -> None :
61+ """Return the version of bma-cli and bma-client."""
62+ click .echo (f"bma-cli version { get_version ('bma-cli' )} " )
63+ click .echo (f"bma-client version { get_version ('bma-client' )} " )
64+
65+
5966@app .command ()
6067def fileinfo (file_uuid : uuid .UUID ) -> None :
6168 """Get info for a file."""
@@ -66,12 +73,11 @@ def fileinfo(file_uuid: uuid.UUID) -> None:
6673
6774@app .command ()
6875def jobs () -> None :
69- """Get info on assigned jobs."""
76+ """Get info on unfinished jobs."""
7077 client , config = init ()
71- jobs = client .get_jobs (job_filter = f "?limit=0&finished=false&client_uuid= { client . uuid } " )
78+ jobs = client .get_jobs (job_filter = "?limit=0&finished=false" )
7279 click .echo (json .dumps (jobs ))
73- click .echo (f"Total { len (jobs )} unfinished jobs assigned to this client." )
74-
80+ click .echo (f"Total { len (jobs )} unfinished jobs." , err = True )
7581
7682
7783@app .command ()
@@ -88,22 +94,24 @@ def grind() -> None:
8894 """Get jobs from the server and handle them."""
8995 client , config = init ()
9096
91- # get any unfinished jobs already assigned to this client
92- jobs = client .get_jobs (job_filter = f"?limit=0&finished=false&client_uuid={ client .uuid } " )
93- if not jobs :
94- # no unfinished jobs assigned to this client, ask for new assignment
95- jobs = client .get_job_assignment ()
96-
97- if not jobs :
98- click .echo ("Nothing to do." )
99- return
100-
101- # loop over jobs and handle each
102- for job in jobs :
103- # make sure we have the original file locally
104- fileinfo = client .download (file_uuid = job ["basefile_uuid" ])
105- path = Path (config ["path" ], fileinfo ["filename" ])
106- handle_job (f = path , job = job , client = client , config = config )
97+ while True :
98+ # get any unfinished jobs already assigned to this client
99+ jobs = client .get_jobs (job_filter = f"?limit=0&finished=false&client_uuid={ client .uuid } " )
100+ if not jobs :
101+ # no unfinished jobs assigned to this client, ask for new assignment
102+ jobs = client .get_job_assignment ()
103+
104+ if not jobs :
105+ click .echo ("Nothing left to do." )
106+ return
107+
108+ # loop over jobs and handle each
109+ click .echo (f"Processing { len (jobs )} jobs for file { jobs [0 ]['basefile_uuid' ]} ..." )
110+ for job in jobs :
111+ # make sure we have the original file locally
112+ fileinfo = client .download (file_uuid = job ["basefile_uuid" ])
113+ path = Path (config ["path" ], fileinfo ["filename" ])
114+ handle_job (f = path , job = job , client = client , config = config )
107115 click .echo ("Done!" )
108116
109117
@@ -121,7 +129,7 @@ def upload(files: list[str]) -> None:
121129 if metadata ["jobs_unfinished" ] == 0 :
122130 continue
123131
124- # it seems there is work to do! ask for assignment
132+ # it seems there is work to do for the newly uploaded file!
125133 jobs = client .get_job_assignment (file_uuid = metadata ["uuid" ])
126134 if not jobs :
127135 click .echo ("No unassigned unfinished jobs found for this file." )
@@ -157,29 +165,7 @@ def handle_job(f: Path, job: ImageConversionJob | ImageExifExtractionJob, client
157165 click .echo (f"Handling job { job ['job_type' ]} { job ['job_uuid' ]} ..." )
158166 start = time .time ()
159167 result = client .handle_job (job = job , orig = f )
160- logger .debug (f"Getting result took { time .time () - start } seconds" )
161- if not result :
162- click .echo (f"No result returned for job { job ['job_type' ]} { job ['uuid' ]} - skipping ..." )
163- return
164-
165- # got job result, do whatever is needed depending on job_type
166- if job ["job_type" ] == "ImageConversionJob" :
167- image , exif = result
168- filename = job ["job_uuid" ] + "." + job ["filetype" ].lower ()
169- logger .debug (f"Encoding result as { job ['filetype' ]} ..." )
170- start = time .time ()
171- with BytesIO () as buf :
172- image .save (buf , format = job ["filetype" ], exif = exif , lossless = False , quality = 90 )
173- logger .debug (f"Encoding result took { time .time () - start } seconds" )
174- client .upload_job_result (job_uuid = job ["job_uuid" ], buf = buf , filename = filename )
175- elif job ["job_type" ] == "ImageExifExtractionJob" :
176- logger .debug (f"Got exif data { result } " )
177- with BytesIO () as buf :
178- buf .write (json .dumps (result ).encode ())
179- client .upload_job_result (job_uuid = job ["job_uuid" ], buf = buf , filename = "exif.json" )
180- else :
181- logger .error ("Unsupported job type" )
182- raise typer .Exit (1 )
168+ logger .debug (f"Getting result took { time .time () - start } seconds: { result } " )
183169
184170
185171def load_config () -> dict [str , str ]:
0 commit comments