diff --git a/comfy/cli_args.py b/comfy/cli_args.py index 353bb51e7..066335b82 100644 --- a/comfy/cli_args.py +++ b/comfy/cli_args.py @@ -1,5 +1,7 @@ import argparse +import base64 import enum +import json import comfy.options class EnumAction(argparse.Action): @@ -107,8 +109,12 @@ parser.add_argument("--disable-smart-memory", action="store_true", help="Force C parser.add_argument("--deterministic", action="store_true", help="Make pytorch use slower deterministic algorithms when it can. Note that this might not make images deterministic in all cases.") parser.add_argument("--dont-print-server", action="store_true", help="Don't print server output.") -parser.add_argument("--quick-test-for-ci", action="store_true", help="Quick test for CI.") parser.add_argument("--windows-standalone-build", action="store_true", help="Windows standalone build: Enable convenient things that most people using the standalone windows build will probably enjoy (like auto opening the page on startup).") +ci_group = parser.add_mutually_exclusive_group() +ci_group.add_argument("--quick-test-for-ci", action="store_true", help="Quick test for CI.") +ci_group.add_argument("--test-ci-with-prompts", action="store_true", help="Test CI with prompts and Comfy will stop running after prompts are complete.") +parser.add_argument("--prompts-for-ci-test", nargs="+", default=[], type=lambda x: json.loads(base64.b64decode(x).decode("utf-8")), help="List of base64 encoded JSON strings for testing CI with prompts. Prompts to use for CI test when --test-ci-with-prompts is set.") + parser.add_argument("--disable-metadata", action="store_true", help="Disable saving prompt metadata in files.") diff --git a/execution.py b/execution.py index 1b8f606a5..45f1633ef 100644 --- a/execution.py +++ b/execution.py @@ -720,6 +720,7 @@ class PromptQueue: self.history = {} self.flags = {} server.prompt_queue = self + self.stop_server_upon_completion = False def put(self, item): with self.mutex: @@ -762,6 +763,8 @@ class PromptQueue: 'status': status_dict, } self.server.queue_updated() + if self.stop_server_upon_completion and len(self.queue) == 0 and len(self.currently_running) == 0: + self.server.stop() def get_current_queue(self): with self.mutex: @@ -818,6 +821,10 @@ class PromptQueue: def delete_history_item(self, id_to_delete): with self.mutex: self.history.pop(id_to_delete, None) + + def set_stop_server_upon_completion(self, value): + with self.mutex: + self.stop_server_upon_completion = value def set_flag(self, name, data): with self.mutex: diff --git a/main.py b/main.py index b3a3ebea8..ec2ad0461 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import uuid import comfy.options comfy.options.enable_args_parsing() @@ -241,6 +242,14 @@ if __name__ == "__main__": if args.quick_test_for_ci: exit(0) + if args.test_ci_with_prompts: + q.set_stop_server_upon_completion(True) + for prompt_json in args.prompts_for_ci_test: + prompt_id = str(uuid.uuid4()) + validation = execution.validate_prompt(prompt_json) + outputs_to_execute = validation[2] + q.put((0, prompt_id, prompt_json, {}, outputs_to_execute)) + call_on_start = None if args.auto_launch: def startup_server(address, port): diff --git a/server.py b/server.py index 5642bd5e2..7ae2634cc 100644 --- a/server.py +++ b/server.py @@ -73,6 +73,8 @@ class PromptServer(): self.loop = loop self.messages = asyncio.Queue() self.number = 0 + self.runner = None + self.site = None middlewares = [cache_control] if args.enable_cors_header: @@ -475,6 +477,7 @@ class PromptServer(): if valid[0]: prompt_id = str(uuid.uuid4()) outputs_to_execute = valid[2] + # TODO: refactor PromptQueue to take typed input self.prompt_queue.put((number, prompt_id, prompt, extra_data, outputs_to_execute)) response = {"prompt_id": prompt_id, "number": number, "node_errors": valid[3]} return web.json_response(response) @@ -618,13 +621,16 @@ class PromptServer(): async def publish_loop(self): while True: msg = await self.messages.get() + # Break the message loop with None message + if msg is None: + break await self.send(*msg) async def start(self, address, port, verbose=True, call_on_start=None): - runner = web.AppRunner(self.app, access_log=None) - await runner.setup() - site = web.TCPSite(runner, address, port) - await site.start() + self.runner = web.AppRunner(self.app, access_log=None) + await self.runner.setup() + self.site = web.TCPSite(self.runner, address, port) + await self.site.start() if verbose: logging.info("Starting server\n") @@ -632,6 +638,39 @@ class PromptServer(): if call_on_start is not None: call_on_start(address, port) + # TODO: accept stop API request + def stop(self): + """ + Stop the server. + + Call `async_stop` coroutine in a thread-safe manner + If the event loop is not running, the stop() is called too early + """ + logging.info("Stopping server") + if self.loop.is_running(): + asyncio.run_coroutine_threadsafe(self.async_stop(), self.loop) + else: + logging.error("Cannot stop server, loop is not running") + + + async def async_stop(self): + """ + Asynchronously stops the server. + This coroutine should be called to gracefully stop the server. + """ + # Put breaking message for message queue + await self.messages.put(None) + # TODO: check if queue is not yet complete and conditional shutdown + # server + if not self.site or self.site._closed: # Check if the site is already closed + logging.warning("Server is already stopped or was never started.") + return + if self.site: + await self.site.stop() + if self.runner: + await self.runner.cleanup() + logging.info("Stopped site upon request") + def add_on_prompt_handler(self, handler): self.on_prompt_handlers.append(handler)