Skip to content

Commit

Permalink
Minor updates to process mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Dec 1, 2023
1 parent d1f773f commit a1d4a3e
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 28 deletions.
8 changes: 6 additions & 2 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ <h2 id="A">A</h2>
<li><a href="index.html#jarvis.modules.models.classes.Indicators.acknowledgement">acknowledgement (jarvis.modules.models.classes.Indicators attribute)</a>
</li>
<li><a href="index.html#jarvis.main.Activator">Activator (class in jarvis.main)</a>
</li>
<li><a href="index.html#jarvis.executors.process_map.add">add() (in module jarvis.executors.process_map)</a>
</li>
<li><a href="index.html#jarvis.modules.car.controller.Control.add_charging_period">add_charging_period() (jarvis.modules.car.controller.Control method)</a>
</li>
Expand Down Expand Up @@ -820,6 +822,8 @@ <h2 id="G">G</h2>
<li><a href="index.html#jarvis.modules.templates.templates.generic">generic (in module jarvis.modules.templates.templates)</a>
</li>
<li><a href="index.html#jarvis.modules.templates.templates.GenericTemplates">GenericTemplates (class in jarvis.modules.templates.templates)</a>
</li>
<li><a href="index.html#jarvis.executors.process_map.get">get() (in module jarvis.executors.process_map)</a>
</li>
<li><a href="index.html#jarvis.executors.alarm.get_alarm_state">get_alarm_state() (in module jarvis.executors.alarm)</a>
</li>
Expand Down Expand Up @@ -3336,11 +3340,11 @@ <h2 id="U">U</h2>
</li>
<li><a href="index.html#jarvis.modules.exceptions.UnsupportedOS">UnsupportedOS</a>
</li>
<li><a href="index.html#jarvis.modules.lights.smart_lights.MagicHomeApi.update_device">update_device() (jarvis.modules.lights.smart_lights.MagicHomeApi method)</a>
<li><a href="index.html#jarvis.executors.process_map.update">update() (in module jarvis.executors.process_map)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="index.html#jarvis.executors.process_map.update_map">update_map() (in module jarvis.executors.process_map)</a>
<li><a href="index.html#jarvis.modules.lights.smart_lights.MagicHomeApi.update_device">update_device() (jarvis.modules.lights.smart_lights.MagicHomeApi method)</a>
</li>
<li><a href="index.html#jarvis.modules.microphone.graph_mic.update_plot">update_plot() (in module jarvis.modules.microphone.graph_mic)</a>
</li>
Expand Down
37 changes: 31 additions & 6 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4431,8 +4431,8 @@ <h1>———-Executors———-<a class="headerlink" href="#executors" title="
<dt>Handles:</dt><dd><ul class="simple">
<li><p>speech_synthesis_api: Speech Synthesis</p></li>
<li><p>telegram_api: Telegram Bot</p></li>
<li><p>jarvis_api: Offline communicator, Robinhood report gatherer, Jarvis UI, Stock monitor, Surveillance, Telegram</p></li>
<li><p>background_tasks: Home automation, Alarms, Reminders, Meetings and Events sync, Cron jobs and Background tasks</p></li>
<li><p>jarvis_api: Offline communicator, Robinhood portfolio report, Jarvis UI, Stock monitor, Surveillance, Telegram</p></li>
<li><p>background_tasks: Home automation, Alarms, Reminders, Meetings and Events sync, Wi-Fi connector, Cron jobs, Background tasks</p></li>
<li><p>plot_mic: Plot microphone usage in real time</p></li>
</ul>
</dd>
Expand All @@ -4458,10 +4458,10 @@ <h1>———-Executors———-<a class="headerlink" href="#executors" title="
<p class="admonition-title">See also</p>
<ul class="simple">
<li><p>speech_synthesis_api: Initiates docker container for speech synthesis.</p></li>
<li><p>telegram_api: Initiates message polling for Telegram bot to execute offline commands.</p></li>
<li><p>telegram_api: Initiates polling Telegram API to execute offline commands (if no webhook config is available)</p></li>
<li><p>jarvis_api: Initiates uvicorn server to process API requests, stock monitor and robinhood report generation.</p></li>
<li><p>background_tasks: Initiates internal background tasks, cron jobs, alarms, reminders, events and meetings sync.</p></li>
<li><p>plot_mic: Initiates plotting microphone usage using matplotlib.</p></li>
<li><p>plot_mic: Initiates plotting realtime microphone usage using matplotlib.</p></li>
</ul>
</div>
</dd></dl>
Expand All @@ -4481,6 +4481,31 @@ <h1>———-Executors———-<a class="headerlink" href="#executors" title="
</section>
<section id="module-jarvis.executors.process_map">
<span id="process-map"></span><h1>Process Map<a class="headerlink" href="#module-jarvis.executors.process_map" title="Permalink to this heading">¶</a></h1>
<dl class="py function">
<dt class="sig sig-object py" id="jarvis.executors.process_map.get">
<span class="sig-prename descclassname"><span class="pre">jarvis.executors.process_map.</span></span><span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">Dict</span><span class="p"><span class="pre">[</span></span><span class="pre">str</span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="pre">Dict</span><span class="p"><span class="pre">[</span></span><span class="pre">int</span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="pre">List</span><span class="p"><span class="pre">[</span></span><span class="pre">str</span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></span></span><a class="headerlink" href="#jarvis.executors.process_map.get" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the existing process map.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns<span class="colon">:</span></dt>
<dd class="field-odd"><p>Returns the dictionary of process data and the impact information.</p>
</dd>
<dt class="field-even">Return type<span class="colon">:</span></dt>
<dd class="field-even"><p>Dict[str, Dict[int, List[str]]]</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="jarvis.executors.process_map.add">
<span class="sig-prename descclassname"><span class="pre">jarvis.executors.process_map.</span></span><span class="sig-name descname"><span class="pre">add</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">Dict</span><span class="p"><span class="pre">[</span></span><span class="pre">str</span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="pre">Dict</span><span class="p"><span class="pre">[</span></span><span class="pre">int</span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="pre">List</span><span class="p"><span class="pre">[</span></span><span class="pre">str</span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">None</span></span></span><a class="headerlink" href="#jarvis.executors.process_map.add" title="Permalink to this definition">¶</a></dt>
<dd><p>Dumps the process map data into the mapping file.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><p><strong>data</strong> – Dictionary of process data and the impact information.</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="jarvis.executors.process_map.remove">
<span class="sig-prename descclassname"><span class="pre">jarvis.executors.process_map.</span></span><span class="sig-name descname"><span class="pre">remove</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func_name</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">None</span></span></span><a class="headerlink" href="#jarvis.executors.process_map.remove" title="Permalink to this definition">¶</a></dt>
Expand All @@ -4493,8 +4518,8 @@ <h1>———-Executors———-<a class="headerlink" href="#executors" title="
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="jarvis.executors.process_map.update_map">
<span class="sig-prename descclassname"><span class="pre">jarvis.executors.process_map.</span></span><span class="sig-name descname"><span class="pre">update_map</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func_name</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">old_pid</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">int</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">new_pid</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">int</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">None</span></span></span><a class="headerlink" href="#jarvis.executors.process_map.update_map" title="Permalink to this definition">¶</a></dt>
<dt class="sig sig-object py" id="jarvis.executors.process_map.update">
<span class="sig-prename descclassname"><span class="pre">jarvis.executors.process_map.</span></span><span class="sig-name descname"><span class="pre">update</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func_name</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">old_pid</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">int</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">new_pid</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">int</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">None</span></span></span><a class="headerlink" href="#jarvis.executors.process_map.update" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove process map for a function that has stopped running.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion jarvis/executors/process_map.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
from typing import Dict, List

import yaml

from jarvis.modules.logger import logger
from jarvis.modules.models import models


def get() -> Dict[str, Dict[int, List[str]]]:
"""Get the existing process map.
Returns:
Dict[str, Dict[int, List[str]]]:
Returns the dictionary of process data and the impact information.
"""
with open(models.fileio.processes) as file:
return yaml.load(stream=file, Loader=yaml.FullLoader)


def add(data: Dict[str, Dict[int, List[str]]]) -> None:
"""Dumps the process map data into the mapping file.
Args:
data: Dictionary of process data and the impact information.
"""
with open(models.fileio.processes, 'w') as file:
yaml.dump(stream=file, data=data)


def remove(func_name: str) -> None:
"""Remove process map for a function that has stopped running.
Expand All @@ -21,7 +44,7 @@ def remove(func_name: str) -> None:
yaml.dump(data=process_map, stream=file)


def update_map(func_name: str, old_pid: int, new_pid: int) -> None:
def update(func_name: str, old_pid: int, new_pid: int) -> None:
"""Remove process map for a function that has stopped running.
Args:
Expand Down
18 changes: 8 additions & 10 deletions jarvis/executors/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from typing import Dict, List, Union

import psutil
import yaml

from jarvis.api.server import jarvis_api
from jarvis.executors import crontab, offline, telegram
from jarvis.executors import crontab, offline, process_map, telegram
from jarvis.modules.audio import speech_synthesis
from jarvis.modules.database import database
from jarvis.modules.logger import logger
Expand Down Expand Up @@ -47,6 +46,7 @@ def clear_db() -> None:
cursor.execute(f"DELETE FROM {table}")


# noinspection LongLine
def create_process_mapping(processes: Dict[str, Process], func_name: str = None) -> None:
"""Creates or updates the processes mapping file.
Expand All @@ -60,8 +60,8 @@ def create_process_mapping(processes: Dict[str, Process], func_name: str = None)
Handles:
- speech_synthesis_api: Speech Synthesis
- telegram_api: Telegram Bot
- jarvis_api: Offline communicator, Robinhood report gatherer, Jarvis UI, Stock monitor, Surveillance, Telegram
- background_tasks: Home automation, Alarms, Reminders, Meetings and Events sync, Cron jobs and Background tasks
- jarvis_api: Offline communicator, Robinhood portfolio report, Jarvis UI, Stock monitor, Surveillance, Telegram
- background_tasks: Home automation, Alarms, Reminders, Meetings and Events sync, Wi-Fi connector, Cron jobs, Background tasks
- plot_mic: Plot microphone usage in real time
"""
impact_lib = {}
Expand All @@ -77,15 +77,13 @@ def create_process_mapping(processes: Dict[str, Process], func_name: str = None)
if not func_name and sorted(impact_lib.keys()) != sorted(processes.keys()):
warnings.warn(message=f"{list(impact_lib.keys())} does not match {list(processes.keys())}")
if func_name: # Assumes a processes mapping file exists already, since flag passed during process specific restart
with open(models.fileio.processes) as file:
dump = yaml.load(stream=file, Loader=yaml.FullLoader)
dump = process_map.get()
dump[func_name] = {processes[func_name].pid: impact_lib[func_name]}
else:
dump = {k: {v.pid: impact_lib[k]} for k, v in processes.items()}
dump["jarvis"] = {models.settings.pid: ["Main Process"]}
logger.debug("Processes data: %s", dump)
with open(models.fileio.processes, 'w') as file:
yaml.dump(stream=file, data=dump, indent=4)
process_map.add(dump)


def start_processes(func_name: str = None) -> Union[Process, Dict[str, Process]]:
Expand All @@ -100,10 +98,10 @@ def start_processes(func_name: str = None) -> Union[Process, Dict[str, Process]]
See Also:
- speech_synthesis_api: Initiates docker container for speech synthesis.
- telegram_api: Initiates message polling for Telegram bot to execute offline commands.
- telegram_api: Initiates polling Telegram API to execute offline commands (if no webhook config is available)
- jarvis_api: Initiates uvicorn server to process API requests, stock monitor and robinhood report generation.
- background_tasks: Initiates internal background tasks, cron jobs, alarms, reminders, events and meetings sync.
- plot_mic: Initiates plotting microphone usage using matplotlib.
- plot_mic: Initiates plotting realtime microphone usage using matplotlib.
"""
process_dict = {
jarvis_api.__name__: Process(target=jarvis_api), # no process map removal
Expand Down
6 changes: 3 additions & 3 deletions jarvis/executors/weather_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
def monitor() -> None:
"""Weather monitoring system to trigger notifications for high, low weather and severe weather alert."""
multiprocessing_logger(filename=os.path.join('logs', 'background_tasks_%d-%m-%Y.log'))
if weather_resp := weather.weather(monitor=True):
condition, high, low, temp_f, alert = weather_resp
else:
try:
condition, high, low, temp_f, alert = weather.weather(monitor=True)
except TypeError:
logger.error("Failed to get weather alerts")
return
if not any((high >= models.env.weather_alert_max, low <= models.env.weather_alert_min, alert)):
Expand Down
6 changes: 2 additions & 4 deletions jarvis/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import pvporcupine
import pyaudio
import pywifi
import yaml
from playsound import playsound

from jarvis.executors import (commander, controls, internet, listener_controls,
location, processor)
location, process_map, processor)
from jarvis.modules.audio import listener, speaker
from jarvis.modules.exceptions import StopSignal
from jarvis.modules.logger import custom_handler, logger
Expand Down Expand Up @@ -200,8 +199,7 @@ def start() -> None:
if models.settings.limited:
# Write processes mapping file before calling start_processes with func_name flag,
# as passing the flag will look for the file's presence
with open(models.fileio.processes, 'w') as file:
yaml.dump(stream=file, data={"jarvis": {models.settings.pid: ["Main Process"]}})
process_map.add({"jarvis": {models.settings.pid: ["Main Process"]}})
if models.settings.os != models.supported_platforms.macOS:
shared.processes = processor.start_processes(func_name="speech_synthesis_api")
else:
Expand Down
2 changes: 1 addition & 1 deletion jarvis/modules/audio/speech_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def speech_synthesis_api() -> None:
logger.warning("Unable to stream container logs locally")
# Container logs will not be available outside docker, so try to update the process map with docker's PID
if pid := find_pid_by_port(models.env.speech_synthesis_port): # Identified the PID to update in process map
process_map.update_map(speech_synthesis_api.__name__, models.settings.pid, pid)
process_map.update(speech_synthesis_api.__name__, models.settings.pid, pid)
else:
# Failed to get the PID of the listening API, hence removing entry from process map
process_map.remove(speech_synthesis_api.__name__)
Expand Down

0 comments on commit a1d4a3e

Please sign in to comment.