Laravel 4: Why is my artisan command called 5 times in succession? - php

Today I bumped into something strange regarding an Artisan Command's lifecycle.
I added an artisan command named cronjobs:MyCommand.
Created the necessary files and classes.
Class MyCommand extends BaseCommand.
BaseCommand is something I put up to benchmark command line operations.
It consists of a constructor and destructor. The constructor keeps track of
the starting time of the operation, the destructor logs the current time minus
the starting time in a log table.
To my surprise I found out that the __destruct() function on my BaseCommand is
called 5 times in total every time I issue the cronjobs:MyCommand via artisan.
Is this normal behavior and if so how am I supposed to take care of logging instead?

Further testing with Symfony 2's command class showed that this is a problem native to Symfony 2, not so much with laravel 4.
Using a constructor/destructor in an artisan command's class (or its parent) in any useful way seems impossible at this point in time.
Any input is still highly appreciated!

Issue seems still not fixed.. I had also creaed a basecommand with __destruct method which is gettigng executed multiple times.
I created a shutdown method in my basecommand and calling at the end of mycommand.

Related

How to run phpunit testcases for impacted codes changes?

I have more than 1000s of unit tests for my application and it takes hour to run the test and get the results. how do we configure to run phpunit to run all only those testcases that has dependency / impact of the current change.
E.g if I change a method in the class A and this method is called in one of the method of Class B. I have Unit tests written for class A, B & C. I want to execute only testcases for class A's method that has been changed and class's B method that has called it.
Edit: I'm using Jenkins to automatically test the code ,how will my phpunit know what has been changed so it will trigger the phpunit for impacted code change.

How to execute a PHP script who depends on project classes?

I have to execute a script which is in the project's root directory.
My script is instancing some classes, but unfortunately, it says that my class isn't found when I do a "PHP myscript.php".
I can try with includes, but I have several errors when a class extends another (external abstract class). Is there a way to make this script "part of the project", in order to not include each class?
I read a similar question but it does not answer exactly to my question: How do you execute a method in a class from the command line
Thanks,
If you want a script that is reusable I would recommend creating a command and executing that command when needed.
See: The laravel docs
EDIT
Sorry, I falsely pressumend you were using the Laravel Framework, for Zend see: The Zend/console docs

Is there any way to check if migrations are ended

My question is if there is any way to check if migrations are ended. Maybe some events or hooks or something else.
You may be able to listen for the Illuminate\Console\Events\CommandFinished event to determine when the php artisan migrate command has finished running (by comparing the $command property of the event).
If you want a hook for each migration file, one option is to extend Illuminate\Database\Migrations\Migrator with your own class and override the runUp() method to fire an event afterward. I don't see any obvious built-in hooks for individual migrations, however. A much more simple solution would be to dispatch an event yourself from within each migration's up() method.

Typo3 scheduler: Can i somehow execute Action of controllers of my extension with it? Or how to run my own code with it?

A while ago i was tasked to program a Typo3 extension to write so called .conf files for the icinga2 montoring tool (has nothing to do with Typo3). Still let me explain some parts of it: Basically the backend user needs to create records of records of specific classes and set values for each records properties. Then i need to process the records to create these .conf files with the specific values with a php script.
I was tasked to use the scheduler in Typo3 for this. And here come the problems: How do i use this? I checked the documentation (https://docs.typo3.org/typo3cms/extensions/scheduler/Introduction/Index.html), but i still can't wrap my head around how to use it for my task. I can easily write an Action in a controller of a class to be executed in the frontend and in turn generate the con files... basically manually without the scheduler. But where do i put my php code to be run by the scheduler? I somehow seem not to understand the basical principle of the scheduler. Can i just run an Action of a specific controller of a class of my extension like i would in the Frontend via the scheduler?
I would suggest you use a command controller for this task.
The documentation shows how to create a command controller, which also may accept arguments.
Command controller tasks can directly be executed by TYPO3 scheduler (see screenshot below)
You may even configure task arguments for command controller tasks in TYPO3 scheduler.

How to store when cache was last cleared and rebuilt in Symfony?

I've created a deployment script for my Symfony applications that fetches updates from a git-repo. It downloads dependencies, updates schema, installs assets, then clears and warms up the cache
It works well (as far I know?) but I need a way to get/store the last time the cache was cleared. Preferably I don't want to write to a file or update a record in a database from the shell, I rather detect/fetch the the last time it was cleared and rebuilt inside my Symfony application.
Is there any way to do this? I thought of getting the created time of the prod cache folder..
Here's the script I wrote: https://github.com/StrikeForceZero/DeploySymfony/blob/master/DeploySymfony.sh
One way to do this would be to write to the Symfony logs using Symfony's built in logging service Monolog (http://symfony.com/doc/current/cookbook/logging/monolog.html). You can either create a Symfony console command (http://symfony.com/components/Console) that your script executes to write to the logs, or you can use inheritance to override one of the Symfony methods that you want to log, that simply calls the logger service, updates the log, and calls its parent method.
For example, instead of calling 'app/console cache:clear' you can call your child method that inherits from CacheClearCommand, update the configure method to set a new name for your command, override the execute method to call your log (either before or after the child executes its parent) and that should be about it. In your deployment script call your custom cache:clear command instead of Symfony's.

Categories