def test_find_daemon_pid_no_pid(mock_process_iter, mock_process): process_name = 'junk' dp = DaemonProcess(process_name) #set up mock values mock_process.ppid.return_value = 1 mock_process.pid = 10 mock_process.name.return_value = 'myprocess' mock_process_iter.return_value = [mock_process] assert dp.find_daemon_pid() == None
def start(self): if not self.proc is None: self.proc.kill() if len(self.watched_ids) > 0: self.proc = DaemonProcess([ exe, "-home", os.path.join(get_config_dir(), "syncthing"), "-folders", ",".join(self.watched_ids) ]) self.proc.connect("exit", self._on_exit) self.proc.connect("failed", self._on_failed) log.info("Starting syncthing-inotify for %s" % (",".join(self.watched_ids))) self.proc.start()
def test_find_worker_pid_no_daemon(mock_find_daemon_pid, mock_process_iter, mock_process): process_name = 'myprocess' dp = DaemonProcess(process_name) #set up mock values mock_find_daemon_pid.return_value = None mock_process.ppid.return_value = 88 mock_process.pid = 100 mock_process_iter.return_value = [mock_process] #find the worker PIDs result = dp.find_worker_pid() #check that find_worker_pid called find_daemon_pid with no arguements mock_find_daemon_pid.assert_called_with() #check list is None assert result == None
def test_find_worker_pid(mock_find_daemon_pid, mock_process_iter, mock_process): process_name = 'myprocess' dp = DaemonProcess(process_name) #set up mock values mock_find_daemon_pid.return_value = 11 mock_process.ppid.return_value = 11 mock_process.pid = 100 mock_process_iter.return_value = [mock_process] #find the worker PIDs result = dp.find_worker_pid() #check that find_worker_pid called find_daemon_pid with no arguements mock_find_daemon_pid.assert_called_with() #check worker PID 100 is in list assert (100 in result)
def start(self): if not self.proc is None: self.proc.kill() if len(self.watched_ids) > 0: self.proc = DaemonProcess( [exe, "-home", os.path.join(get_config_dir(), "syncthing"), "-folders", ",".join(self.watched_ids)] ) self.proc.connect("exit", self._on_exit) self.proc.connect("failed", self._on_failed) log.info("Starting syncthing-inotify for %s" % (",".join(self.watched_ids))) self.proc.start()
class _WinWatcher: """ Filesystem watcher implementation for Windows. Passes watched directories to syncthing-notify executable ran on background. Available only if executable is found in same folder as syncthing-gtk.exe is. """ def __init__(self, app, daemon): self.watched_ids = [] self.app = app self.proc = None def watch(self, id, path): self.watched_ids += [id] def kill(self): """ Cancels & deallocates everything """ self.watched_ids = [] if not self.proc is None: self.proc.kill() self.proc = None def start(self): if not self.proc is None: self.proc.kill() if len(self.watched_ids) > 0: self.proc = DaemonProcess([ exe, "-home", os.path.join(get_config_dir(), "syncthing"), "-folders", ",".join(self.watched_ids) ]) self.proc.connect("exit", self._on_exit) self.proc.connect("failed", self._on_failed) log.info("Starting syncthing-inotify for %s" % (",".join(self.watched_ids))) self.proc.start() def _on_exit(self, proc, code): log.warning("syncthing-inotify exited with code %s" % (code, )) def _on_failed(self, proc, error): log.error("Failed to start syncthing-inotify: %s" % (error, )) self.proc = None
class _WinWatcher: """ Filesystem watcher implementation for Windows. Passes watched directories to syncthing-notify executable ran on background. Available only if executable is found in same folder as syncthing-gtk.exe is. """ def __init__(self, app, daemon): self.watched_ids = [] self.app = app self.proc = None def watch(self, id, path): self.watched_ids += [id] def kill(self): """ Cancels & deallocates everything """ self.watched_ids = [] if not self.proc is None: self.proc.kill() self.proc = None def start(self): if not self.proc is None: self.proc.kill() if len(self.watched_ids) > 0: self.proc = DaemonProcess([ exe, "-home", os.path.join(get_config_dir(), "syncthing"), "-folders", ",".join(self.watched_ids) ]) self.proc.connect("exit", self._on_exit) self.proc.connect("failed", self._on_failed) log.info("Starting syncthing-inotify for %s" % (",".join(self.watched_ids))) self.proc.start() def _on_exit(self, proc, code): log.warning("syncthing-inotify exited with code %s" % (code,)) def _on_failed(self, proc, error): log.error("Failed to start syncthing-inotify: %s" % (error,)) self.proc = None
import sys import time import datetime import argparse from daemonprocess import DaemonProcess import memory_stats import cpu_stats parser = argparse.ArgumentParser() parser.add_argument("process", help="name of worker processes to monitor") parser.add_argument("frequency", type=int, help="sample frequency in seconds") args = parser.parse_args() process_name = args.process frequency_sec = args.frequency while True: current_time = str(datetime.datetime.now()) dp = DaemonProcess(process_name) list_pids = dp.find_worker_pid() if list_pids is None: break mem = memory_stats.memory_average(list_pids) cpu = cpu_stats.cpu_usage(list_pids) print current_time, (process_name, mem, cpu) time.sleep(frequency_sec)