Welcome Guest, Not a member yet? Register   Sign In
Process After Webhook Response
#1

Hi,
I'm looking for advice on best practice to processing webhook data after returning 202 success to the webhook?
As there is only a short window for responding to the webhook (5 secs) and potentially a lot of heavy work to do processing the webhook data, I need to call a class or method in the controller AFTER responding to the webhook
Code:
return $this->response->setStatusCode(200);

Should this be done with a "post_system" Event (can I pass the webhook payload data to the event from the controller? can I restrict the event to being called only after a specific Controller has finished running?), or is there an alternative?
I would be grateful if someone could point me in the right direction?
Many Thanks
Reply
#2

what i do in a similar situation is save the data to a pending table and use a cron job to check the table every minute or so
Reply
#3

Thanks badger,
I've considered using CRON, but as I need to process the webhook data immediately, therefore I can't really wait for the CRON to run. Also, when considering CRON this it seemed like running the CRON very minute or so, then writing, reading, updating/removing data from the database was significant waste of resources.
Therefore I was hoping to use the Codeigniter Events functionality, or something similar, to run only after webhook controller was called, as this would seem to make more sense. But, it seems Codeigniter doesn't support this type of processing!
Thanks again :-)
Reply
#4

(This post was last modified: 08-08-2023, 09:02 AM by 68thorby68.)

After considerable web searching using Stack Overflow in conjunction with the CI manual, I was able to to establish I need to apply a filter NOT event.

First I set up my filter. NOTE: as I only want the filter to apply AFTER the controller has run, i.e. after the controller has responded to the 3rd party webhook in a timely manner, I only need to define the after function.

PHP Code:
<?php namespace App\Libraries\Filters;

use 
CodeIgniter\Filters\FilterInterface;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;

class 
Webhook_test implements FilterInterface
{
    public function before(RequestInterface $request$arguments null)
    
         //leave this blank
    }
    
    
public function after(RequestInterface $requestResponseInterface $response$arguments null)
    {
         log_message('notice''[SUCCESS] {file}-{line}, This is called from the controller AFTER has run. THIS WILL BE REPLACED WITH A RETURN ROUTE TO  TO PROCESS THE WEBHOOK DATA');
    }

Then I configured my filter in the Config/Filters, first declaring "use App\Libraries\Filters\Webhook_test;" and then creating an alias "Webhook_test" for the class .
PHP Code:
public $aliases = [
        'csrf'          => CSRF::class,
        'toolbar'      => DebugToolbar::class,
        'honeypot'      => Honeypot::class,
        'invalidchars'  => InvalidChars::class,
        'secureheaders' => SecureHeaders::class,
        'webhooktest' => Webhook_test::class,
    ]; 

Then, as I only want the filter to apply to a single controller and I don't want to list (and keep updating) all excluded controllers.  I applied my alias to a url filter, NOT global. ,
Note: my before and after url pattern is an exact match to the controller I want this filter to be applied to (not sure if this is a proper usage, but it works!!) as I do not want it to be applied anywhere else.
PHP Code:
public $filters = [
         'webhooktest' => ['before'=>['mywebhookcontroller'], 'after' => ['mywebhookcontroller']],
 ]; 

Many thanks, I believe I have found a solution without the resource overhead of CRONs and database queries
Reply




Theme © iAndrew 2016 - Forum software by © MyBB