Skip to content

Commit c8f4c0a

Browse files
committed
Implement graceful exit on SIGTERM and SIGINT. Also create data directory if it does not exists
1 parent 8aa1553 commit c8f4c0a

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

Diff for: gnssr_raspberry/gnssr.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,17 @@ def __init__(self,configfile=None,simulate=False,noupload=False):
6868
#default of 20 microseconds
6969
self.serialsleep=20e-3
7070

71+
#possibly create the data directory if it doesn't exist yet
72+
if not os.path.exists(self.cfg['data_dir']):
73+
os.mkdir(self.cfg['data_dir'])
7174
#set the filename of the open logfile
7275
self.openLogFile=os.path.join(self.cfg['data_dir'],self.cfg['file_base']+".tmp")
7376
#default (will be updated from GNSS info)
7477
self.logdate=date.today()
7578

7679
self.openSerial()
7780
self.setupWebdav()
78-
81+
7982
def setupWebdav(self):
8083
if "webdav" in self.cfg:
8184
self.webdav = self.cfg['webdav']['url']
@@ -102,11 +105,15 @@ async def rotateNMEAlog(self):
102105
rmcregex=re.compile(b'^\$G[NPL]RMC')
103106
#open logstream
104107
self.openLog()
105-
while True:
108+
while self.isLogging:
109+
106110
#Asynchronously wait for new serial data
107111
nmeamsg=await self.getnmea()
108112
if not nmeamsg.endswith(b"\n"):
109-
#no info -> try again later
113+
#no info -> try again later (or in the case of simulate data rewind the buffer
114+
if self.simulate:
115+
self.serial.seek(0)
116+
continue
110117
print("no data found on the serial port, retrying in one second")
111118
await asyncio.sleep(1)
112119
continue
@@ -142,6 +149,10 @@ def closeLog(self):
142149
if self.logfid:
143150
self.logfid.close()
144151
self.logfid=None
152+
else:
153+
#nothing to do
154+
return
155+
145156
#also move the file to a more suitable name
146157
logfilebase=os.path.join(self.cfg['data_dir'],f"{self.cfg['file_base']}_{self.logdate.isoformat()}")
147158
#make sure not to overwrite existing files
@@ -217,7 +228,8 @@ async def uploadLogs(self):
217228

218229

219230
async def startLoggingDaemon(self):
220-
while True:
231+
self.isLogging=True
232+
while self.isLogging:
221233
synctask=asyncio.create_task(self.uploadLogs())
222234
await self.rotateNMEAlog()
223235
#wait for synctask to finish with timeout as a backup
@@ -227,3 +239,7 @@ async def startLoggingDaemon(self):
227239
except asyncio.TimeoutError:
228240
pass
229241

242+
def stopLoggingDaemon(self,*args):
243+
"""Gracefully stop logging (closes logging file)"""
244+
self.isLogging=False
245+

Diff for: nmealogger.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import asyncio
1010
import argparse
1111
import sys
12+
import signal
1213

1314
def main(argv):
1415
usage="GNSSR nmea logging and upload daemon"
@@ -22,12 +23,15 @@ def main(argv):
2223
parser.add_argument('-n','--noupload',action='store_true',
2324
help="Don't attempt to upload any logs")
2425
args=parser.parse_args(argv[1:])
25-
26+
27+
28+
2629
gnssr=GNSSRconfig(args.config,args.simulate,args.noupload)
27-
try:
28-
asyncio.run(gnssr.startLoggingDaemon())
29-
except KeyboardInterrupt:
30-
gnssr.closeLog()
30+
# register graceful killing of logging routine
31+
signal.signal(signal.SIGINT, gnssr.stopLoggingDaemon)
32+
signal.signal(signal.SIGTERM, gnssr.stopLoggingDaemon)
33+
34+
asyncio.run(gnssr.startLoggingDaemon())
3135

3236

3337
if __name__ == "__main__":

0 commit comments

Comments
 (0)