Skip to content

Commit

Permalink
Add 'restartBackgroundScripts' action
Browse files Browse the repository at this point in the history
  • Loading branch information
purplesyringa committed Aug 31, 2018
1 parent 6c3f2a2 commit 6bbe390
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
8 changes: 8 additions & 0 deletions SitePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def __init__(self, *args, **kwags):
if "BACKGROUND" in self.settings["permissions"]:
self.spawnBackgroundProcesses()

self.onFileDone.append(self.reloadBackgroundProcess)


def spawnBackgroundProcesses(self):
if self.spawned_background_processes:
Expand All @@ -37,11 +39,17 @@ def spawnBackgroundProcesses(self):
# Spawn background process if needed
def spawnBackgroundProcess(self, file_name):
ext = file_name.replace("0background.", "")
# Kill old thread if it is running
self.spawner.stop(ext)
# Read code
code = self.storage.read(file_name)
# Spawn
self.spawner.spawn(ext, code)

# If a background process is changed, reload it
def reloadBackgroundProcess(self, inner_path):
if inner_path.startswith("0background."):
self.spawnBackgroundProcess(inner_path)


def delete(self):
Expand Down
15 changes: 15 additions & 0 deletions UiWebsocketPlugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from Plugin import PluginManager

@PluginManager.registerTo("UiWebsocket")
class UiWebsocketPlugin(object):
def actionRestartBackgroundScripts(self, to):
if "BACKGROUND" in self.site.settings["permissions"]:
# Stop threads
self.site.spawner.stopAll()
# Start them
self.site.spawned_background_processes = False
self.site.spawnBackgroundProcesses()
# Reply
self.response(to, "ok")
else:
self.response(to, {"error": "No BACKGROUND permission"})
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BackgroundPlugin
import SitePlugin
import UiWebsocketPlugin
import storage

def addModule(name, module):
Expand Down
14 changes: 11 additions & 3 deletions spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def spawn(self, ext, code):
safe_code = sandboxer.toSafe()

self.log.debug("Running 0background.%s" % ext)
self.threads.append(safe_code())
self.threads.append((ext, safe_code()))


def findTranspiler(self, ext):
Expand All @@ -47,6 +47,14 @@ def findTranspiler(self, ext):

# Stop all threads
def stopAll(self):
for thread in self.threads:
for _, thread in self.threads:
thread.kill(block=False)
self.threads = []
self.threads = []

# Stop by extension
def stop(self, ext):
new_threads = []
for ext1, thread in self.threads:
if ext == ext1:
thread.kill(block=False)
self.threads = [thread for thread in self.threads if thread[0] != ext]

0 comments on commit 6bbe390

Please sign in to comment.