示例#1
0
def run_command(request):
    command_instance_id = request.POST["commandInstanceId"]
    arguments = json.loads(request.POST.get("arguments", "{}"))
    try:
        command_instance = CommandInstance.objects.get(id=command_instance_id)
    except Exception:
        return BaseconfigError.INVALID_COMMAND_INSTANCE

    def run():
        CommandRun.run(request.user, command_instance, arguments)

    ThreadHandler("commandthread", run)

    return {}
示例#2
0
 def __init__(self, name, streams):
     #TODO: this should take in a name and a redis connection
     self.name = name
     self.own_writer = BasicStreamWriter(os.path.join("/logging/", name),
                                         name)
     self.stream_writers = {}
     # The redis subscription is created in a separate thread since pubsub is not thread safe (and reads don't need to block)
     self.subscription = None
     # self.subscription = RedisStreamSubscriber()
     self.subscription_queue = queue.Queue()
     self.streams = streams
     self.lock = threading.Lock()
     self.background_updating_thread = ThreadHandler(
         "background updating stream " + name, self.background_updating)
示例#3
0
    def init(cls, name, status={}, update_interval=2.0):
        if hasattr(cls, "init_time") and cls.init_time is not None:
            raise Exception(
                "Service status is already started, manually change the settings if you want"
            )

        # TODO: we should have an option if we want to report CPU/memory info
        # TODO: should be support also app version information?

        cls.init_time = time.time()

        cls.service_name = name
        cls.lock = threading.Lock()
        cls.status = status
        cls.update_interval = min(max(update_interval, 1.0), 30.0)

        cls.pid = os.getpid()
        ethernet_info = get_default_network_interface()

        cls.mac_address = ethernet_info["mac"]
        cls.machine_address = ethernet_info.get("ip", "127.0.0.1")

        cls.log_info = {
            "name": cls.service_name,
            "pid": cls.pid,
            "initTime": cls.init_time,
            "machineIP": cls.machine_address,
        }

        cls.machine_id = cls.get_machine_id()

        # TODO: this should be configurable
        cls.status_stream = RedisStreamPublisher("service_status",
                                                 persistence=False)

        cls.background_thread_handler = ThreadHandler("service_status_update",
                                                      cls.background_thread)
示例#4
0
 def __init__(self, *args, **kwargs):
     super().__init__()
     self.record_queue = Queue(maxsize=128 << 10) # Drops anything when > 128k unlogged message records
     self.logging_thread_handler = ThreadHandler(self.__class__.__name__ + " background logging", self.background_thread)
示例#5
0
 def start(self):
     self.keep_working = True
     self.background_thread = ThreadHandler("Command processor " + str(self.__class__.__name__), self.process)
示例#6
0
# The default Django session engine
session_engine = import_module(settings.SESSION_ENGINE)

#TODO: also keep a counter with the number of stream subcriptions

users_pending_connection = queue.Queue(maxsize=2048)


# TODO: this should be a thread pool of ~4-8 threads
def background_connect_users():
    while True:
        user_connection = users_pending_connection.get()
        user_connection.get_user()


backround_user_connection_init_thread = ThreadHandler(
    "background fetch users from DB", background_connect_users)


class UserConnection(object):
    """
    Class used to manage a websocket connection to a user
    """
    def __init__(self, request, session_key, event, websocket):
        self.request = request
        self.session_key = session_key
        self.event = event
        self.websocket = websocket
        self.time_connected = time.time()

        users_pending_connection.put(self)
示例#7
0
 def start(self):
     self.background_thread = ThreadHandler("Command processor " +
                                            str(self.__class__.__name__),
                                            self.process,
                                            daemon=False)