Page 1 of 1

Webhook Issue

PostPosted: Mon Dec 21, 2020 7:05 pm
by SBarnes
We have a client with webhooks set up for a debtor update the business logic is firing and creating an entry in SY_WebhookMessage but the response only shows up in SY_WebhookMessageResponse and in the receiver of the webhook when the service is restarted, how can we diagnose what is going wrong?

Re: Webhook Issue

PostPosted: Tue Dec 22, 2020 1:40 pm
by Mike.Sheen
SBarnes wrote:We have a client with webhooks set up for a debtor update the business logic is firing and creating an entry in SY_WebhookMessage but the response only shows up in SY_WebhookMessageResponse and in the receiver of the webhook when the service is restarted, how can we diagnose what is going wrong?


That'll happen if the API couldn't be reached by Jiwa clients. Either the WebhooksHostURL system setting hasn't been set correctly, or the Jiwa clients can't POST to the WebhooksHostURL.

When a user does something in Jiwa, a business logic handler for the SaveEnd will invoke a method to POST the DTO to the WebhooksHostURL. If that fails for any reason, the exception handler will insert into SY_WebhookMessage - it's assuming the API is down, but rather than fail it inserts into SY_WebhookMessage so when the service comes back up, it can processes those queued messages.

You can see where this all happens in line 427 of the standard REST API plugin:

Code: Select all
static async public System.Threading.Tasks.Task Webhook(JiwaFinancials.Jiwa.JiwaApplication.Manager Manager, string body, string eventName)
{      
   string url = Manager.Database.ReadSysData("REST API", "WebhooksHostURL", "").ToString();
   string clientKey = Manager.Database.ReadSysData("REST API", "WebhooksClientKey", "").ToString();
   
   if (url.Length > 0)
   {
      try
      {            
         ServiceStack.JsonServiceClient client = new ServiceStack.JsonServiceClient(url);                        
         client.Headers.Add("ClientKey", clientKey);            
         await client.PostAsync<IReturnVoid>(new JiwaFinancials.Jiwa.JiwaServiceModel.WebhooksEventsPOSTRequest() { EventName = eventName, Body = body });            
      }
      catch (System.Exception ex)
      {            
         // Don't throw if the above fails - we don't want to stop the user continuing on if there was a problem with the REST API, but we do want to try to insert into the message queue table so when the service comes up it knows about these items it missed
         string sql = @"INSERT INTO SY_WebhookMessage(RecID, SY_WebhookSubscription_RecID, Body, ItemNo, Status, LastSavedDateTime, AddedDateTime, Retries)
               SELECT NewID(), SY_WebhookSubscription.RecID, @Body,
               COALESCE(QueueTable.MaximumItemNo, 0) + ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),
               0, SYSUTCDATETIME(), SYSUTCDATETIME(), 0
               FROM SY_WebhookSubscription
               CROSS JOIN (SELECT MAX(ItemNo) AS MaximumItemNo FROM SY_WebhookMessage) QueueTable
               WHERE EventName = @EventName
               ORDER BY SY_WebhookSubscription.ItemNo";
         
         var dbFactory = new OrmLiteConnectionFactory(Manager.Database.ConnectionString, ServiceStack.OrmLite.SqlServer2012Dialect.Provider);
         using (var db = dbFactory.Open())
         {            
            db.ExecuteSql(sql, new {Body = body, EventName = eventName} );
         }
         
      }
   }
}


Check the WebhooksHostURL system setting to make sure it's valid - use Postman or something to send a test POST to the URL and make sure you're getting a 204 or 200 response.

Re: Webhook Issue

PostPosted: Tue Dec 22, 2020 1:49 pm
by SBarnes
Thanks Mike, but I am laughing as great minds think alike :lol: I am kind of remoting this one given I am on leave and letting Danny handle although I had suggested putting a debug call at the top of the of the Webhook function and try debugging it that way but I'll suggest the use of Postman first