Skip to content

Commit dbc2cde

Browse files
committed
Fixes #46 Conversation service status
1 parent ef1a68b commit dbc2cde

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

Scripts/Services/Conversation/Conversation.cs

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,84 @@ public string GetServiceID()
199199

200200
public void GetServiceStatus(ServiceStatus callback)
201201
{
202-
if (callback != null && callback.Target != null)
203-
{
204-
callback(SERVICE_ID, false);
205-
}
202+
if (Config.Instance.FindCredentials(SERVICE_ID) != null)
203+
new CheckServiceStatus(this, callback);
204+
else
205+
{
206+
if (callback != null && callback.Target != null)
207+
{
208+
callback(SERVICE_ID, false);
209+
}
210+
}
206211
}
207212

213+
private class CheckServiceStatus
214+
{
215+
private Conversation m_Service = null;
216+
private ServiceStatus m_Callback = null;
217+
private int m_ConversationCount = 0;
218+
219+
public CheckServiceStatus(Conversation service, ServiceStatus callback)
220+
{
221+
m_Service = service;
222+
m_Callback = callback;
223+
224+
string customServiceID = Config.Instance.GetVariableValue(SERVICE_ID + "_ID");
225+
226+
//If custom classifierID is defined then we are using it to check the service health
227+
if (!string.IsNullOrEmpty(customServiceID))
228+
{
229+
230+
if (!m_Service.Message(customServiceID, "Hello", OnMessage))
231+
OnFailure("Failed to invoke Converse().");
232+
else
233+
m_ConversationCount += 1;
234+
}
235+
else
236+
{
237+
if (!m_Service.GetWorkspaces(OnGetWorkspaces))
238+
OnFailure("Failed to invoke GetDialogs().");
239+
}
240+
}
241+
242+
private void OnGetWorkspaces(DataModels.Workspaces workspaces)
243+
{
244+
if (m_Callback != null)
245+
{
246+
foreach (DataModels.Workspace workspace in workspaces.workspaces)
247+
{
248+
if (!m_Service.Message(workspace.workspace_id, "Hello", OnMessage))
249+
OnFailure("Failed to invoke Message().");
250+
else
251+
m_ConversationCount += 1;
252+
}
253+
}
254+
else
255+
OnFailure("GetMessages() failed.");
256+
}
257+
258+
private void OnMessage(DataModels.MessageResponse resp)
259+
{
260+
if (m_ConversationCount > 0)
261+
{
262+
m_ConversationCount -= 1;
263+
if (resp != null)
264+
{
265+
if (m_ConversationCount == 0 && m_Callback != null && m_Callback.Target != null)
266+
m_Callback(SERVICE_ID, true);
267+
}
268+
else
269+
OnFailure("ConverseResponse is null.");
270+
}
271+
}
272+
273+
private void OnFailure(string msg)
274+
{
275+
Log.Error("Dialog", msg);
276+
m_Callback(SERVICE_ID, false);
277+
m_ConversationCount = 0;
278+
}
279+
};
208280
#endregion
209281
}
210282
}

0 commit comments

Comments
 (0)