Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced start and shutdown lockinkg mechanism #26

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions mqtt_framework/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
from datetime import datetime, timedelta
import time
from threading import Lock

from flask import Flask as Flask
from flask import jsonify
Expand All @@ -25,7 +26,7 @@
from mqtt_framework.config import Config as Config

# current MQTT-Framework version
__version__ = "1.1.1"
__version__ = "1.2.0"


class ReadOnlyDict(dict):
Expand Down Expand Up @@ -75,7 +76,9 @@ def __init__(self):
"How many exceptions caused by do_update",
registry=self._metrics_registry,
)
self._started = False
self._closed = False
self.lock = Lock()
self._limiter = Limiter(
get_remote_address,
app=self._flask,
Expand Down Expand Up @@ -293,6 +296,14 @@ def run(self, app: App, config: Config) -> int:
return self.start(app, config, blocked=True)

def start(self, app: App, config: Config, blocked=False) -> int:
with self.lock:
self._start(app, config, blocked)

def _start(self, app: App, config: Config, blocked=False) -> int:
if self._started:
self._flask.logger.debug("Application already started")
return 1

self._load_config(config)

if blocked:
Expand Down Expand Up @@ -352,6 +363,8 @@ def subscribe_to_mqtt_topic(self, topic: str) -> None:
+ timedelta(seconds=self._flask.config["DELAY_BEFORE_FIRST_TRY"])
)
self._scheduler.start()
self._started = True
self._closed = False
if blocked:
self._do_wait()
return 0
Expand All @@ -363,16 +376,18 @@ def _shutdown(self) -> None:
self._mqtt.unsubscribe_all()
self._publish_value_to_mqtt_topic("status", "offline", True)
self._mqtt._disconnect()
self._closed = True
self._started = False

def shutdown(self) -> None:
if not self._closed:
self._closed = True
self._flask.config["EXIT"] = True
self._flask.logger.info("Closing...")
try:
self._shutdown()
except Exception as e:
self._flask.logger.exception(f"Error occured: {e}")
self._flask.logger.critical("Application stopped")
else:
self._flask.logger.trace("Application already stopped")
with self.lock:
if not self._closed:
self._flask.config["EXIT"] = True
self._flask.logger.info("Closing...")
try:
self._shutdown()
except Exception as e:
self._flask.logger.exception(f"Error occured: {e}")
self._flask.logger.critical("Application stopped")
else:
self._flask.logger.debug("Application already stopped")
Loading