From 0c102769e0ca4154662e562d53ad70735f4a191e Mon Sep 17 00:00:00 2001 From: Daniel Milchev Date: Fri, 2 Sep 2022 12:17:00 +0300 Subject: [PATCH 1/2] [#98] Ensure graceful exit of the quickstart Python script and the How-to guide Python scripts Fixed the issue in all scripts Signed-off-by: Daniel Milchev --- quickstart/hono_commands.py | 5 +++++ quickstart/hono_commands_fu.py | 19 +++++++++++++++---- quickstart/hono_commands_su.py | 6 ++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/quickstart/hono_commands.py b/quickstart/hono_commands.py index db6e78c7..6cace573 100644 --- a/quickstart/hono_commands.py +++ b/quickstart/hono_commands.py @@ -56,6 +56,11 @@ def on_message(self, event): print('[ok]', command) else: print('[error]') + event.receiver.close() + event.connection.close() + + def on_connection_closed(self, event): + print('[closing]') os.kill(os.getpid(), signal.SIGINT) diff --git a/quickstart/hono_commands_fu.py b/quickstart/hono_commands_fu.py index b7d2c6e3..d971ab18 100644 --- a/quickstart/hono_commands_fu.py +++ b/quickstart/hono_commands_fu.py @@ -57,7 +57,12 @@ def on_message(self, event): print('[ok]', "fu") else: print('[error]') - os.kill(os.getpid(), signal.SIGINT) + event.receiver.close() + event.connection.close() + + def on_connection_closed(self, event): + print('[closing]') + os.kill(os.getpid(), signal.SIGINT) class CommandsInvoker(MessagingHandler): @@ -88,7 +93,7 @@ def on_sendable(self, event): value=value) print(payload) msg = Message(body=payload, address='{}/{}'.format(self.address, device_id), content_type="application/json", - subject="fu", reply_to=reply_to_address, correlation_id=correlation_id, id=str(uuid.uuid4())) + subject=self.action, reply_to=reply_to_address, correlation_id=correlation_id, id=str(uuid.uuid4())) event.sender.send(msg) event.sender.close() event.connection.close() @@ -121,10 +126,16 @@ def on_message(self, event): print(json.dumps(body, indent=2)) if body["value"]["state"] == "SUCCESS": print('[successful upload]') - os.kill(os.getpid(), signal.SIGINT) + event.receiver.close() + event.connection.close() elif body["value"]["state"] == "FAILED": print('[failed upload]') - os.kill(os.getpid(), signal.SIGINT) + event.receiver.close() + event.connection.close() + + def on_connection_closed(self, event): + print('[closing]') + os.kill(os.getpid(), signal.SIGINT) # Parse command line args diff --git a/quickstart/hono_commands_su.py b/quickstart/hono_commands_su.py index 2d1e480f..c7fd200f 100644 --- a/quickstart/hono_commands_su.py +++ b/quickstart/hono_commands_su.py @@ -76,9 +76,15 @@ def on_message(self, event): print('[ok]', "su") else: print('[error]') + event.receiver.close() + event.connection.close() + + def on_connection_closed(self, event): + print('[closing]') os.kill(os.getpid(), signal.SIGINT) + class CommandsInvoker(MessagingHandler): def __init__(self, server, address): super(CommandsInvoker, self).__init__() From 8997a1d24898ce8d432e419d39bf204369777799 Mon Sep 17 00:00:00 2001 From: Daniel Milchev Date: Fri, 2 Sep 2022 17:45:49 +0300 Subject: [PATCH 2/2] [#98] Ensure graceful exit of the quickstart Python script and the How-to guide Python scripts Formatted the script Signed-off-by: Daniel Milchev --- quickstart/hono_commands.py | 2 +- quickstart/hono_commands_fu.py | 4 ++-- quickstart/hono_commands_su.py | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/quickstart/hono_commands.py b/quickstart/hono_commands.py index 6cace573..5011c230 100644 --- a/quickstart/hono_commands.py +++ b/quickstart/hono_commands.py @@ -60,7 +60,7 @@ def on_message(self, event): event.connection.close() def on_connection_closed(self, event): - print('[closing]') + print('[connection closed]') os.kill(os.getpid(), signal.SIGINT) diff --git a/quickstart/hono_commands_fu.py b/quickstart/hono_commands_fu.py index d971ab18..d10ce8e1 100644 --- a/quickstart/hono_commands_fu.py +++ b/quickstart/hono_commands_fu.py @@ -61,7 +61,7 @@ def on_message(self, event): event.connection.close() def on_connection_closed(self, event): - print('[closing]') + print('[connection closed]') os.kill(os.getpid(), signal.SIGINT) @@ -134,7 +134,7 @@ def on_message(self, event): event.connection.close() def on_connection_closed(self, event): - print('[closing]') + print('[connection closed]') os.kill(os.getpid(), signal.SIGINT) diff --git a/quickstart/hono_commands_su.py b/quickstart/hono_commands_su.py index c7fd200f..780fc415 100644 --- a/quickstart/hono_commands_su.py +++ b/quickstart/hono_commands_su.py @@ -80,11 +80,10 @@ def on_message(self, event): event.connection.close() def on_connection_closed(self, event): - print('[closing]') + print('[connection closed]') os.kill(os.getpid(), signal.SIGINT) - class CommandsInvoker(MessagingHandler): def __init__(self, server, address): super(CommandsInvoker, self).__init__()