Skip to content

Commit

Permalink
Merge pull request #51 from Koneko349/main
Browse files Browse the repository at this point in the history
Adding wait section and API tests for SD
  • Loading branch information
rbbrdckybk authored Dec 19, 2023
2 parents 16449a6 + 68de666 commit 45ea8d0
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions scripts/sdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,31 @@ def __init__(self, gpu_id, port, path_to_sd, control_ref, worker_name):
self.command = 'webui-user.sh'
self.target_command = 'df-start-gpu-' + str(gpu_id) + '.sh'

#waits for SD APIs to be ready and returning expected information
def wait_for_server(self, url, api_endpoint, timeout=300):
start_time = time.time()

while True:
try:
response = requests.get(url + api_endpoint)
response.raise_for_status() # Raises stored HTTPError, if one occurred.

data = response.json()
if 'detail' in data and data['detail'] == 'Not Found':
# If we get 'detail': 'Not Found', the API is not ready.
pass
else:
# If we don't get 'detail': 'Not Found', the API is ready.
break

except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError, KeyError):
pass

if time.time() - start_time > timeout:
raise TimeoutError(f'Server at {url} not responding after {timeout} seconds')

time.sleep(5) # Wait before trying again

# starts up a new SD instance
def initialize(self):
self.init = True
Expand All @@ -358,6 +382,9 @@ def initialize(self):
self.monitor = Monitor(self, self.monitor_done_callback)
self.monitor.start()

# Wait for server to be ready
self.wait_for_server(self.url, "/sdapi/v1/samplers")


# creates a suitable startup .bat/.sh for this gpu
def create_startup_batch_file(self):
Expand Down

0 comments on commit 45ea8d0

Please sign in to comment.