[wing] Added timeout parameter to wing_service_manager_stop_service



commit d5e9d030a80167ee7d5318f80fac9136f8bfe3ef
Author: Silvio Lazzeretti <silviola amazon com>
Date:   Mon Oct 30 12:18:51 2017 +0100

    Added timeout parameter to wing_service_manager_stop_service

 wing/wingservice.c        |    9 ++++++++-
 wing/wingservicemanager.c |   14 ++++++++++----
 wing/wingservicemanager.h |    1 +
 3 files changed, 19 insertions(+), 5 deletions(-)
---
diff --git a/wing/wingservice.c b/wing/wingservice.c
index 0047730..c09b017 100644
--- a/wing/wingservice.c
+++ b/wing/wingservice.c
@@ -77,6 +77,7 @@ static gboolean uninstall_service;
 static gchar *service_start_type;
 static gboolean start_service;
 static gboolean stop_service;
+static gint service_stop_timeout = 5;
 static gboolean exec_service_as_application;
 static const GOptionEntry entries[] =
 {
@@ -89,6 +90,8 @@ static const GOptionEntry entries[] =
     "auto|demand|disabled" },
   { "start", '\0', 0, G_OPTION_ARG_NONE, &start_service,
     "Starts the service using the Windows service manager" },
+  { "stop-timeout", '\0', 0, G_OPTION_ARG_INT, &service_stop_timeout,
+    "Time in seconds to wait for the service to be stopped" },
   { "stop", '\0', 0, G_OPTION_ARG_NONE, &stop_service,
     "Stops the service using the Windows service manager" },
   { "exec", '\0', 0, G_OPTION_ARG_NONE, &exec_service_as_application,
@@ -692,6 +695,7 @@ on_handle_local_options (GApplication *application,
   WingServicePrivate *priv;
   WingServiceManager *manager;
   WingServiceManagerStartType start_type = WING_SERVICE_MANAGER_START_AUTO;
+  guint stop_timeout = 5;
   gint ret = -1;
 
   manager = wing_service_manager_new ();
@@ -703,12 +707,15 @@ on_handle_local_options (GApplication *application,
   else if (g_strcmp0 (service_start_type, "disabled") == 0)
     start_type = WING_SERVICE_MANAGER_START_DISABLED;
 
+  if (service_stop_timeout >= 0)
+    stop_timeout = service_stop_timeout;
+
   if (install_service)
     ret = wing_service_manager_install_service (manager, service, start_type, NULL) ? 0 : 1;
   else if (uninstall_service)
     ret = wing_service_manager_uninstall_service (manager, service, NULL) ? 0 : 1;
   else if (stop_service)
-    ret =  wing_service_manager_stop_service (manager, service, NULL) ? 0 : 1;
+    ret =  wing_service_manager_stop_service (manager, service, stop_timeout, NULL) ? 0 : 1;
   else if (exec_service_as_application)
     /* do nothing so the application continues to run */
     ret = -1;
diff --git a/wing/wingservicemanager.c b/wing/wingservicemanager.c
index 7b02dc0..e2afc16 100644
--- a/wing/wingservicemanager.c
+++ b/wing/wingservicemanager.c
@@ -21,6 +21,8 @@
 #include <gio/gio.h>
 #include <Windows.h>
 
+#define STOP_SERVICE_NUMBERS_OF_CHECKS_PER_SEC 10
+
 struct _WingServiceManager
 {
   GObject parent_instance;
@@ -338,6 +340,7 @@ wing_service_manager_start_service (WingServiceManager  *manager,
 gboolean
 wing_service_manager_stop_service (WingServiceManager  *manager,
                                    WingService         *service,
+                                   guint                timeout_in_sec,
                                    GError             **error)
 {
   SC_HANDLE sc;
@@ -362,12 +365,13 @@ wing_service_manager_stop_service (WingServiceManager  *manager,
       if (ControlService (service_handle, SERVICE_CONTROL_STOP, &status))
         {
           gboolean stopped = status.dwCurrentState == SERVICE_STOPPED;
-          gint i = 0;
+          guint timeout_value = timeout_in_sec * STOP_SERVICE_NUMBERS_OF_CHECKS_PER_SEC;
+          guint i = 0;
 
           /* It may take some time to get a response that the service was stopped */
-          while (!stopped && i < 10)
+          while (!stopped && i < timeout_value)
             {
-              g_usleep (200);
+              g_usleep (G_USEC_PER_SEC / STOP_SERVICE_NUMBERS_OF_CHECKS_PER_SEC);
 
               if (!QueryServiceStatus (service_handle, &status))
                 {
@@ -390,7 +394,9 @@ wing_service_manager_stop_service (WingServiceManager  *manager,
             {
               g_set_error (error, G_IO_ERROR,
                            WING_SERVICE_ERROR_SERVICE_STOP_TIMEOUT,
-                           "Stopping the server took more than 2 seconds");
+                           "Stopping the service took more than %d %s",
+                           timeout,
+                           timeout == 1 ? "second" : "seconds");
             }
 
           result = stopped;
diff --git a/wing/wingservicemanager.h b/wing/wingservicemanager.h
index e2c3467..77884bc 100644
--- a/wing/wingservicemanager.h
+++ b/wing/wingservicemanager.h
@@ -73,6 +73,7 @@ gboolean                wing_service_manager_start_service            (WingServi
 WING_AVAILABLE_IN_ALL
 gboolean                wing_service_manager_stop_service             (WingServiceManager           *manager,
                                                                        WingService                  *service,
+                                                                       guint                         
timeout_in_sec,
                                                                        GError                      **error);
 
 G_END_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]