Cake PHP while loop to dispatch events

159 Views Asked by At

I'm working on a legacy Cake PHP 2 project that has a form that once I submit dispatches an event in my project. It works when I click my button, but when I put it into a while loop to try and bulk generate 10 events I only ever get 1 generated.

What am I missing?

/**
* Store a new diagnostic action
*/
public function store()
{
    if ($this->request->is('post')) {
        $action = $this->request->data['action'];

        // test event action
        if ($action == 'perform-test-event-dispatch') {

            $count = 0;
            while ($count < 10) {
                CakeEventManager::instance()->dispatch(new CakeEvent(
                    'QueueManagerModule.QueueManagerJob.dispatchJob', $this, array(
                        'job' => 'TypicodeApi',
                        'arguments' => [
                            'endpoint' => 'https://jsonplaceholder.typicode.com/posts',
                            'timeout' => 30
                        ]
                    )
                ));
                $count++;
            }

            $this->Session->setFlash("$action signal successfully requested.");
        }

        return $this->redirect('/queue/settings');
    }
}
0

There are 0 best solutions below