-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ce6cbd
commit 1dd0b07
Showing
273 changed files
with
1,333 additions
and
1,325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from multiprocessing.forkserver import set_forkserver_preload | ||
|
||
from abstra_internals.controllers import execution_process | ||
from abstra_internals.controllers.execution_consumer import ExecutionConsumer | ||
from abstra_internals.controllers.main import MainController | ||
from abstra_internals.environment import RABBITMQ_CONNECTION_URI | ||
from abstra_internals.logger import AbstraLogger | ||
from abstra_internals.repositories.consumer import RabbitConsumer | ||
from abstra_internals.repositories.factory import get_prodution_repositories | ||
from abstra_internals.settings import SettingsController | ||
from abstra_internals.signals import SignalHandlers | ||
|
||
|
||
def run(): | ||
SignalHandlers.init() | ||
AbstraLogger.init("cloud") | ||
SettingsController.set_root_path(".") | ||
|
||
if not RABBITMQ_CONNECTION_URI: | ||
raise Exception("RABBITMQ_CONNECTION_URI not found") | ||
|
||
set_forkserver_preload([execution_process.__name__]) | ||
controller = MainController(repositories=get_prodution_repositories()) | ||
with RabbitConsumer(RABBITMQ_CONNECTION_URI) as consumer: | ||
SignalHandlers.register_sigterm_callback(consumer.stop) | ||
ExecutionConsumer(consumer, controller) | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from concurrent.futures import ThreadPoolExecutor | ||
from uuid import uuid4 | ||
|
||
from abstra_internals.controllers.execution_process import ExecutionProcess | ||
from abstra_internals.controllers.main import MainController | ||
from abstra_internals.environment import QUEUE_CONCURRENCY | ||
from abstra_internals.logger import AbstraLogger | ||
from abstra_internals.repositories.consumer import Consumer, QueueMessage | ||
from abstra_internals.settings import Settings | ||
|
||
|
||
class StageNotFound(Exception): | ||
pass | ||
|
||
|
||
class NonCleanExit(Exception): | ||
pass | ||
|
||
|
||
def PreExecController( | ||
*, | ||
controller: MainController, | ||
consumer: Consumer, | ||
msg: QueueMessage, | ||
arbiter_uuid: str, | ||
): | ||
worker_uuid = str(uuid4()) | ||
head_id = worker_uuid.split("-")[0] | ||
|
||
try: | ||
mp_context = controller.repositories.mp_context.get_context() | ||
stage = controller.get_action(msg.preexecution.stage_id) | ||
if stage is None: | ||
raise StageNotFound(msg.preexecution.stage_id) | ||
|
||
p = mp_context.Process( | ||
target=ExecutionProcess, | ||
kwargs=dict( | ||
stage=stage, | ||
controller=controller, | ||
worker_uuid=worker_uuid, | ||
arbiter_uuid=arbiter_uuid, | ||
root_path=Settings.root_path, | ||
request=msg.preexecution.request, | ||
environment=AbstraLogger.environment, | ||
target_stage_run_id=msg.preexecution.target_stage_run_id, | ||
), | ||
name=f"Worker-{head_id}", | ||
) | ||
|
||
p.start() | ||
p.join() | ||
|
||
if p.exitcode != 0: | ||
err_msg = f"Worker exited with status ({p.exitcode})" | ||
if p.exitcode == -9: | ||
err_msg += ": Server reached its memory limit" | ||
|
||
raise NonCleanExit(err_msg) | ||
|
||
consumer.done_callback(msg) | ||
except Exception as e: | ||
AbstraLogger.error(f"[{head_id}] PreExecController ERROR: aborting consumer") | ||
AbstraLogger.capture_exception(e) | ||
controller.child_exit( | ||
app_id=arbiter_uuid, worker_id=worker_uuid, err_msg=f"[ABORTED] {e.args}" | ||
) | ||
consumer.stop() | ||
|
||
|
||
class Arbiter: | ||
def __init__(self, controller: MainController): | ||
self.controller = controller | ||
self.uuid = str(uuid4()) | ||
|
||
@property | ||
def head_id(self) -> str: | ||
return self.uuid.split("-")[0] | ||
|
||
def __enter__(self): | ||
AbstraLogger.debug(f"[{self.head_id}] ARBITER INIT") | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
AbstraLogger.debug(f"[{self.head_id}] ARBITER EXIT") | ||
self.controller.self_exit(app_id=self.uuid, err_msg="[ABORTED] Worker exited") | ||
|
||
|
||
def ExecutionConsumer(consumer: Consumer, controller: MainController): | ||
with Arbiter(controller) as arbiter: | ||
with ThreadPoolExecutor(max_workers=QUEUE_CONCURRENCY) as executor: | ||
for msg in consumer.iter(): | ||
executor.submit( | ||
PreExecController, | ||
arbiter_uuid=arbiter.uuid, | ||
controller=controller, | ||
consumer=consumer, | ||
msg=msg, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import Optional | ||
|
||
from abstra_internals.controllers.execution import ExecutionController | ||
from abstra_internals.controllers.main import MainController | ||
from abstra_internals.entities.execution import RequestContext | ||
from abstra_internals.environment import set_SERVER_UUID, set_WORKER_UUID | ||
from abstra_internals.logger import AbstraLogger, Environment | ||
from abstra_internals.repositories.project.project import ActionStage | ||
from abstra_internals.settings import Settings | ||
from abstra_internals.stdio_patcher import StdioPatcher | ||
|
||
|
||
# runs in subprocess - all arguments must be picklable/multiprocessable | ||
def ExecutionProcess( | ||
*, | ||
root_path: str, | ||
worker_uuid: str, | ||
arbiter_uuid: str, | ||
stage: ActionStage, | ||
controller: MainController, | ||
environment: Optional[Environment], | ||
request: Optional[RequestContext] = None, | ||
target_stage_run_id: Optional[str] = None, | ||
): | ||
Settings.set_root_path(root_path) | ||
AbstraLogger.init(environment) | ||
|
||
set_WORKER_UUID(worker_uuid) | ||
set_SERVER_UUID(arbiter_uuid) | ||
StdioPatcher.apply(controller) | ||
|
||
head_id = worker_uuid.split("-")[0] | ||
|
||
AbstraLogger.debug(f"[{head_id}] WORKER INIT") | ||
|
||
try: | ||
ExecutionController( | ||
repositories=controller.repositories, | ||
workflow_engine=controller.workflow_engine, | ||
).run( | ||
stage=stage, | ||
request=request, | ||
target_stage_run_id=target_stage_run_id, | ||
) | ||
except Exception as e: | ||
AbstraLogger.error(f"[{head_id}] WORKER ERROR: {e}") | ||
AbstraLogger.capture_exception(e) | ||
finally: | ||
AbstraLogger.debug(f"[{head_id}] WORKER EXIT") |
Oops, something went wrong.