Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flashMessenger not working when adding Message after a long request

I'm facing a really strange issue with Zend Frameworks flashMessenger and can't find out what's the cause of the problem, nor how to solve it.

When a requested action takes very long, the flashMessenger doesn't work as expected.

Non-working Example:

class AttachmentController extends Zend_Controller_Action {

    public function printAction() {
        // action takes really long and causes flash message to not appear at all
        sleep(11);

        $this->_helper->flashMessenger
            ->setNamespace('success')
            ->addMessage("It's done!");

        $this->_redirect('/attachment/index');
    }
}

With the above code, the controller action behind /attachment/index does not display the flashMessage.

However, if i reduce the runtime of the script, it works:

class AttachmentController extends Zend_Controller_Action {

    public function printAction() {
        // now the action runs faster and the flashMessage appears!
        sleep(1);

        $this->_helper->flashMessenger
            ->setNamespace('success')
            ->addMessage("It's done!");

        $this->_redirect('/attachment/index');
    }
}

Question: What may be the cause for the flashMessenger to not display my message? How may i fix that?

Notes:

  • Yes, i'm sure this is the problem. I replaced the long-running procedure with sleep(11) in production code and it produces the described behaviour. The problem is not caused by the code i replaced for isolating the case. It's really caused by the long runtime of the script.
  • I can't make the script execute faster. Instead of the sleep(11) in the example, in production code something is sent to a printer which takes ~11 seconds to complete.
like image 574
Kaii Avatar asked Mar 28 '14 13:03

Kaii


Video Answer


1 Answers

Both examples works fine. If sleep(11) doesn't work you are having side-effects, maybe by concurrency. FlashMessenger is session based so you have to check your session configurations. The redirector action helper $this->_redirect(); will send a header and create a new HTTP Request to produce a new entire dispatch process. So you have also to check your plugins which are registered to this process (explained here).

To resolve this problem you can try to use the forwarder instead of redirector. Really good explained here (difference-between-redirect-and-forward) and here (use-flashmessenger-without-redirect).

By the way it is bad practice using sleep to mock "printjob is done". You should prefer another message like "Printjob added to queue", which is not a lie and it's user friendly.

like image 120
Mamuz Avatar answered Oct 09 '22 00:10

Mamuz