mirror of
https://github.com/pulb/mailnag.git
synced 2026-05-07 05:06:54 +02:00
shutdown existing Mailnag instance via dbus instead of pkill
fixed cleanup function
This commit is contained in:
@@ -26,8 +26,9 @@ import dbus.service
|
||||
|
||||
# DBUS server that exports mailnag signals end methods
|
||||
class DBUSService(dbus.service.Object):
|
||||
def __init__(self):
|
||||
def __init__(self, shutdown_cb):
|
||||
self._mails = []
|
||||
self._shutdown_cb = shutdown_cb
|
||||
bus_name = dbus.service.BusName('mailnag.MailnagService', bus = dbus.SessionBus())
|
||||
dbus.service.Object.__init__(self, bus_name, '/mailnag/MailnagService')
|
||||
|
||||
@@ -64,3 +65,8 @@ class DBUSService(dbus.service.Object):
|
||||
@dbus.service.method(dbus_interface = 'mailnag.MailnagService', out_signature = 'u')
|
||||
def GetMailCount(self):
|
||||
return len(self._mails)
|
||||
|
||||
|
||||
@dbus.service.method(dbus_interface = 'mailnag.MailnagService')
|
||||
def Shutdown(self):
|
||||
self._shutdown_cb()
|
||||
|
||||
@@ -23,8 +23,9 @@
|
||||
# MA 02110-1301, USA.
|
||||
#
|
||||
|
||||
import os
|
||||
import sys
|
||||
from gi.repository import GObject, GLib
|
||||
import dbus
|
||||
from dbus.mainloop.glib import DBusGMainLoop
|
||||
import threading
|
||||
import time
|
||||
@@ -51,19 +52,6 @@ def read_config():
|
||||
return read_cfg()
|
||||
|
||||
|
||||
def write_pid():
|
||||
pid_file = os.path.join(cfg_folder, 'mailnag.pid')
|
||||
f = open(pid_file, 'w')
|
||||
f.write(str(os.getpid()))
|
||||
f.close()
|
||||
|
||||
|
||||
def delete_pid():
|
||||
pid_file = os.path.join(cfg_folder, 'mailnag.pid')
|
||||
if os.path.exists(pid_file):
|
||||
os.remove(pid_file)
|
||||
|
||||
|
||||
def wait_for_inet_connection():
|
||||
if not is_online():
|
||||
print 'Waiting for internet connection...'
|
||||
@@ -71,19 +59,43 @@ def wait_for_inet_connection():
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
def shutdown_existing_instance():
|
||||
BUS_NAME = 'mailnag.MailnagService'
|
||||
OBJ_PATH = '/mailnag/MailnagService'
|
||||
|
||||
bus = dbus.SessionBus()
|
||||
|
||||
if bus.name_has_owner(BUS_NAME):
|
||||
sys.stdout.write('Shutting down existing Mailnag process...')
|
||||
sys.stdout.flush()
|
||||
|
||||
try:
|
||||
proxy = bus.get_object(BUS_NAME, OBJ_PATH)
|
||||
shutdown = proxy.get_dbus_method('Shutdown', BUS_NAME)
|
||||
|
||||
shutdown()
|
||||
|
||||
while bus.name_has_owner(BUS_NAME):
|
||||
time.sleep(2)
|
||||
|
||||
sys.stdout.write('OK\n')
|
||||
except:
|
||||
sys.stdout.write('FAILED\n')
|
||||
|
||||
|
||||
def cleanup():
|
||||
# clean up resources
|
||||
if idlers != None:
|
||||
idlers.dispose()
|
||||
|
||||
if (start_thread != None) and (start_thread.is_alive()):
|
||||
start_thread.join()
|
||||
|
||||
print "Starter thread exited successfully"
|
||||
|
||||
if (poll_thread != None) and (poll_thread.is_alive()):
|
||||
poll_thread_stop.set()
|
||||
poll_thread.join()
|
||||
print "Polling thread exited successfully"
|
||||
|
||||
delete_pid()
|
||||
if idlers != None:
|
||||
idlers.dispose()
|
||||
|
||||
|
||||
def sig_handler(signum, frame):
|
||||
@@ -100,9 +112,11 @@ def main():
|
||||
DBusGMainLoop(set_as_default = True)
|
||||
signal.signal(signal.SIGTERM, sig_handler)
|
||||
|
||||
# shut down an (possibly) already running Mailnag daemon
|
||||
# (must be called before instantiation of the DBUSService).
|
||||
shutdown_existing_instance()
|
||||
|
||||
try:
|
||||
# write Mailnag's process id to file
|
||||
write_pid()
|
||||
cfg = read_config()
|
||||
|
||||
if (cfg == None):
|
||||
@@ -110,8 +124,8 @@ def main():
|
||||
exit(1)
|
||||
|
||||
wait_for_inet_connection()
|
||||
|
||||
dbusservice = DBUSService()
|
||||
|
||||
dbusservice = DBUSService(shutdown_cb = lambda: mainloop.quit())
|
||||
|
||||
# start checking for mails asynchronously
|
||||
start_thread = threading.Thread(target = start, args = (cfg, dbusservice,))
|
||||
@@ -124,6 +138,7 @@ def main():
|
||||
except KeyboardInterrupt:
|
||||
pass # ctrl+c pressed
|
||||
finally:
|
||||
print "Shutting down..."
|
||||
cleanup()
|
||||
|
||||
|
||||
|
||||
12
mailnag
12
mailnag
@@ -4,18 +4,6 @@ LIB_DIR=./Mailnag
|
||||
config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/mailnag"
|
||||
|
||||
mkdir --parents "$config_dir"
|
||||
|
||||
if [ -f "$config_dir/mailnag.pid" ]; then
|
||||
pid=$(cat "$config_dir/mailnag.pid")
|
||||
if [ "`ps -p $pid -o comm=`" == "mailnag" ]; then
|
||||
kill $pid 2> /dev/null
|
||||
# wait until mailnag teminates and removes it's pid file
|
||||
while ps -p $pid > /dev/null; do
|
||||
sleep 1
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
rm --force "$config_dir/mailnag.log"
|
||||
|
||||
python2 $LIB_DIR/mailnag.py >> "$config_dir/mailnag.log" 2>&1 &
|
||||
|
||||
Reference in New Issue
Block a user