Improve restart from UI

This commit is contained in:
ElMoribond 2021-07-15 17:26:48 +02:00
parent e06532d7e7
commit 9954e1fb07
3 changed files with 29 additions and 45 deletions

View File

@ -90,7 +90,10 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
client.publish(state_topic, payload, retain=True) client.publish(state_topic, payload, retain=True)
def on_restart_command(client, userdata, message): def on_restart_command(client, userdata, message):
restart_frigate(client, mqtt_config.topic_prefix) payload = message.payload.decode()
if payload == "please_restart":
restart_frigate()
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
threading.current_thread().name = "mqtt" threading.current_thread().name = "mqtt"

View File

@ -519,14 +519,9 @@ def clipped(obj, frame_shape):
return False return False
def restart_frigate(mqtt_client, topic_prefix, from_ui = 1): def restart_frigate():
def on_publish(client,userdata,result):
os.kill(os.getpid(), signal.SIGKILL) os.kill(os.getpid(), signal.SIGKILL)
mqtt_client.on_publish = on_publish
mqtt_client.publish(f"{topic_prefix}/restarted", int(from_ui))
class EventsPerSecond: class EventsPerSecond:
def __init__(self, max_events=1000): def __init__(self, max_events=1000):

View File

@ -4,57 +4,43 @@ import { useCallback, useEffect, useState } from 'preact/hooks';
import { useRestart } from '../api/mqtt'; import { useRestart } from '../api/mqtt';
export default function DialogRestart({ show, setShow }) { export default function DialogRestart({ show, setShow }) {
const { payload: detectRestarted = null, send: sendRestart } = useRestart();
const [dialogTitle, setDialogTitle] = useState('Restart in progress');
useEffect(() => { useEffect(() => {
if (detectRestarted != null && Number.isInteger(detectRestarted)) { if (show === 2) {
if (!detectRestarted) { sendRestart('please_restart');
setDialogTitle('Server-initiated startup'); setTimeout(async () => {
} const delay = (ms) => new Promise(res => setTimeout(res, ms));
setShow(false);
}
}, [detectRestarted]); // eslint-disable-line react-hooks/exhaustive-deps
const waitPlease = async () => { while (show === 2) {
const delay = ms => new Promise(res => setTimeout(res, ms));
await delay(3456);
/* eslint-disable no-constant-condition */
while (true) {
try { try {
const response = await fetch('/api/config', { method: 'GET' }); const response = await fetch('/api/config', { method: 'GET' });
if (await response.status === 200) { if (await response.status === 200) {
window.location.reload(); setShow(0);
continue;
} }
} }
catch (e) {} catch (e) {}
await delay(987); await delay(987);
} }
}; window.location.reload();
const handleClick = useCallback(() => { }, 3456);
sendRestart(); }
setShow(false);
waitPlease();
}, [show]); // eslint-disable-line react-hooks/exhaustive-deps
const handleDismiss = useCallback(() => {
setShow(false);
}, [show]); // eslint-disable-line react-hooks/exhaustive-deps }, [show]); // eslint-disable-line react-hooks/exhaustive-deps
return ( return (
<Fragment> <Fragment>
{show ? ( {show === 2 ? (
<Dialog title="Restart in progress" text="This page should refresh as soon as the server is up and running…" />
) : show === 1 && (
<Dialog <Dialog
onDismiss={handleDismiss} onDismiss={() => setShow(0)}
title="Restart Frigate" title="Restart Frigate"
text="Are you sure?" text="Are you sure?"
actions={[ actions={[
{ text: 'Yes', color: 'red', onClick: handleClick }, { text: 'Yes', color: 'red', onClick: () => setShow(2) },
{ text: 'Cancel', onClick: handleDismiss } ]} { text: 'Cancel', onClick: () => setShow(0) } ]}
/> />
) : detectRestarted != null && (
<Dialog title={dialogTitle} text="This page should refresh as soon as the server is up and running…" />
)} )}
</Fragment> </Fragment>
); );