From 21bd3560c6a30a66be70afe1ac7b908005e9a28d Mon Sep 17 00:00:00 2001
From: Samuel Gaist <samuel.gaist@idiap.ch>
Date: Thu, 28 Feb 2019 16:45:52 +0100
Subject: [PATCH] [utils][commands] Add full_scheduling command

This command will start a full scheduler/broker/worker setup on the
local machine. This allows to have a working system closer to what
the platform uses.
---
 .../management/commands/full_scheduling.py    | 122 ++++++++++++++++++
 1 file changed, 122 insertions(+)
 create mode 100644 beat/web/utils/management/commands/full_scheduling.py

diff --git a/beat/web/utils/management/commands/full_scheduling.py b/beat/web/utils/management/commands/full_scheduling.py
new file mode 100644
index 000000000..96bc69283
--- /dev/null
+++ b/beat/web/utils/management/commands/full_scheduling.py
@@ -0,0 +1,122 @@
+#!/usr/bin/env python
+# vim: set fileencoding=utf-8 :
+# encoding: utf-8
+
+###############################################################################
+#                                                                             #
+# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/           #
+# Contact: beat.support@idiap.ch                                              #
+#                                                                             #
+# This file is part of the beat.web module of the BEAT platform.              #
+#                                                                             #
+# Commercial License Usage                                                    #
+# Licensees holding valid commercial BEAT licenses may use this file in       #
+# accordance with the terms contained in a written agreement between you      #
+# and Idiap. For further information contact tto@idiap.ch                     #
+#                                                                             #
+# Alternatively, this file may be used under the terms of the GNU Affero      #
+# Public License version 3 as published by the Free Software and appearing    #
+# in the file LICENSE.AGPL included in the packaging of this file.            #
+# The BEAT platform is distributed in the hope that it will be useful, but    #
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  #
+# or FITNESS FOR A PARTICULAR PURPOSE.                                        #
+#                                                                             #
+# You should have received a copy of the GNU Affero Public License along      #
+# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
+#                                                                             #
+###############################################################################
+
+import logging
+import multiprocessing
+import signal
+
+from django.core.management.base import BaseCommand
+from django.core.management import call_command
+from django.conf import settings
+from django import db
+
+from beat.core.utils import find_free_port
+
+logger = logging.getLogger(__name__)
+
+
+def start_broker(port, verbosity=0):
+    db.connections.close_all()
+    call_command("broker", port=port, verbosity=verbosity)
+
+
+def start_scheduler(broker_address, verbosity=0):
+    db.connections.close_all()
+    call_command("scheduler", broker_address=broker_address, verbosity=verbosity)
+
+
+def start_worker(broker_address, prefix, cache, use_docker, verbosity=0):
+    call_command(
+        "worker",
+        broker_address=broker_address,
+        prefix=prefix,
+        cache=cache,
+        use_docker=use_docker,
+        verbosity=verbosity,
+    )
+
+
+class Command(BaseCommand):
+
+    help = "Run a complete local scheduler/broker/worker setup"
+
+    def __init__(self):
+        super(Command, self).__init__()
+
+        self.broker = None
+        self.worker = None
+        self.scheduler = None
+
+    def __signal_handler(self, signum, frame):
+        self.scheduler.terminate()
+        self.worker.terminate()
+        self.broker.terminate()
+
+    def add_arguments(self, parser):
+        parser.add_argument(
+            "--docker",
+            "-d",
+            action="store_true",
+            dest="use_docker",
+            default=False,
+            help="Use docker",
+        )
+
+    def handle(self, *args, **options):
+        signal.signal(signal.SIGTERM, self.__signal_handler)
+        signal.signal(signal.SIGINT, self.__signal_handler)
+
+        verbosity = options["verbosity"]
+
+        port = find_free_port()
+        broker_address = "tcp://localhost:{}".format(port)
+
+        self.broker = multiprocessing.Process(
+            target=start_broker, args=(port, verbosity)
+        )
+        self.worker = multiprocessing.Process(
+            target=start_worker,
+            args=(
+                broker_address,
+                settings.PREFIX,
+                settings.CACHE_ROOT,
+                options["use_docker"],
+                verbosity,
+            ),
+        )
+        self.scheduler = multiprocessing.Process(
+            target=start_scheduler, args=(broker_address, verbosity)
+        )
+
+        self.broker.start()
+        self.worker.start()
+        self.scheduler.start()
+
+        self.broker.join()
+        self.scheduler.join()
+        self.worker.join()
-- 
GitLab