即使 svc_start() 成功傳回,基礎應用程式也可能無法啟動。 因此,svc_start() 必須探測應用程式,以驗證該應用程式在傳回成功訊息之前已在執行。 此探測也必須考慮此應用程式可能不會立即可用,因為它要花些時間來啟動。 svc_start() 方法將呼叫 svc_wait() (在 xfnts.c 中定義),以確認此應用程式正在執行,如下所示。
/* Wait for the service to start up fully */ scds_syslog_debug(DBG_LEVEL_HIGH, "Calling svc_wait to verify that service has started."); rc = svc_wait(scds_handle); scds_syslog_debug(DBG_LEVEL_HIGH, "Returned from svc_wait"); if (rc == 0) { scds_syslog(LOG_INFO, "Successfully started the service."); } else { scds_syslog(LOG_ERR, "Failed to start the service."); }
svc_wait() 函式呼叫 scds_get_netaddr_list(3HA),來取得探測此應用程式所需的網路位址資源,如下所示。
/* obtain the network resource to use for probing */ if (scds_get_netaddr_list(scds_handle, &netaddr)) { scds_syslog(LOG_ERR, "No network address resources found in resource group."); return (1); } /* Return an error if there are no network resources */ if (netaddr == NULL || netaddr->num_netaddrs == 0) { scds_syslog(LOG_ERR, "No network address resource in resource group."); return (1); }
然後,svc_wait() 便可取得 start_timeout 與 stop_timeout 值,如下所示。
svc_start_timeout = scds_get_rs_start_timeout(scds_handle) probe_timeout = scds_get_ext_probe_timeout(scds_handle)
若要說明伺服器可能需要的啟動時間,svc_wait() 將呼叫 scds_svc_wait(),並傳送相當於 start_timeout 值 3% 的逾時值。 然後,svc_wait() 將呼叫 svc_probe(),以確認此應用程式已經啟動。 svc_probe() 方法在指定通訊埠上建立與伺服器的簡單套接字連接。 如果無法連接至該通訊埠,svc_probe() 將傳回值 100,指示發生完全故障。 如果連接成功,但與該通訊埠斷開連接失敗,則 svc_probe() 將傳回值 50。
svc_probe() 失敗或部分失敗時,svc_wait() 將以逾時值 5 呼叫 scds_svc_wait()。scds_svc_wait() 方法會將探測頻率限定為每五秒一次。 此方法也將計算嘗試啟動服務的次數。 如果嘗試次數在資源的 Retry_interval 屬性所指定的時間內,超出該資源 Retry_count 屬性的值,則 scds_svc_wait() 函式將傳回故障。 在此情況下,svc_start() 函式也將傳回故障。
#define SVC_CONNECT_TIMEOUT_PCT 95 #define SVC_WAIT_PCT 3 if (scds_svc_wait(scds_handle, (svc_start_timeout * SVC_WAIT_PCT)/100) != SCHA_ERR_NOERR) { scds_syslog(LOG_ERR, "Service failed to start."); return (1); } do { /* * probe the data service on the IP address of the * network resource and the portname */ rc = svc_probe(scds_handle, netaddr->netaddrs[0].hostname, netaddr->netaddrs[0].port_proto.port, probe_timeout); if (rc == SCHA_ERR_NOERR) { /* Success. Free up resources and return */ scds_free_netaddr_list(netaddr); return (0); } /* Call scds_svc_wait() so that if service fails too if (scds_svc_wait(scds_handle, SVC_WAIT_TIME) != SCHA_ERR_NOERR) { scds_syslog(LOG_ERR, "Service failed to start."); return (1); } /* Rely on RGM to timeout and terminate the program */ } while (1);
在結束之前,xfnts_start 方法將呼叫 scds_close() 以回收 scds_initialize() 分配的資源。 請參閱scds_initialize() 函式與 scds_close(3HA) 線上援助頁,以取得詳細資訊。