We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

logger in memory until quit

I'm required to logg each page/action in the application. Now, I've implemented a log to file in my controllerBase, but I'm not fine with that, a file write operation on each request, that's not good about performance. So I would log to memory, until quit , or when the logs display page is called, where I would copy to file. Here is a way to use the memory adapter to commit log?

How about using queue ? Put job to queue, and handle logging and file operations in cli app ?



11.6k

that sound clever..will learn more about cli, thanks..



11.6k

Wojciech, using queue, what the point about using cli? would not a script included into my web app be enough, if it's triggered by the beanstalk daemon?

Someone needs to handle this job from queue, right ? If you want to handle it in web app then it will be still done in request time. You need cli for handling it out of web app like:

  1. User acces website.
  2. Website is creating log message and adding it to queue - this don't cost like any peformance.
  3. Cli is handling your queue and doing writing to file - this cost performance so we are not doing it when returning response.


11.6k

ok..tried that with no success: (cli.php)


        include...

        // Connect to Beanstalk
        $queue = new Phalcon\Queue\Beanstalk(
                array(
                    'host' => '127.0.0.1',
                    'port' => '11300'
                )
        );
        $queue->choose('mainHook');

        while($job = $queue->reserve()) {
                $arguments = array();
                $reception = $job->getBody();
                foreach ($reception as $k => $arg) {
                        if ($k == 1) {
                                $arguments['task'] = $reception['task'];
                        } elseif ($k == 2) {
                                $arguments['action'] = $reception['action'];
                        } elseif ($k >= 3) {
                                $arguments[] = $reception['data'];
                        }
                    }
            try {
                $app = new ConsoleApp();
                $app->setDI($di);
                $app->handle($arguments);
            } catch (Exception $e) {
                echo $e->getMessage();
             }
            $job->delete();
        }

and in the web app:


          // Connect to the queue
                $queue = new Phalcon\Queue\Beanstalk(
                    array(
                        'host' => '127.0.0.1',
                        'port' => '11300'
                    )
                );
                $queue->choose('mainHook');

                $job = array(
                    "task" => "logging",
                    "action" => "start"
                    );

                $queue->put($job);

..and I'm not sure about how to start the cli app first, when an user log into

First check cli documentation here - https://docs.phalcon.io/pl/latest/reference/cli.html You just need cron or something that would be running each few seconds/minustes and that's it.



11.6k

thanks.it's working now, with a rc.local script running at boot and re-triggered each five second, but I'm unsure about the way I do in my previous code example, won't the while loop initialize too many threads/app instance, causing some memory leaks or similar overhead problem? Also, I've read a lot of things about beanstalk, but doesn't understand how the watching process behave..Does it need to be initialized only once time or need recurrent call to be triggered?

edited Aug '16

Just make cli task which will be runned few seconds and working on beanstalkd queue - if there will be no any task it will end instantly, if there will be any it will be running - so you need here using reserve. Nothing fancy here. Add job in your webapp, handle it in cli with cron or whatever. Which exactly you don't understand ? Just check queue tutorial :). Everything is in doceumentation. Which exactly you don't understand ? While will be running until there is any job - if there is no any - it will stop and end php process.



11.6k

if there is no any - it will stop and end php process.

it was the point I was unsure, thank for your patience..