by Dominik Jungowski
31. Mai 2009
Comments (2)
Sometimes, when you write a Command Line (CLI) script, you want to catch Ctrl+C. Simple Example: Your script does some stuff on the database and you don't want to leave it in the state it had in the moment the script was killed, Ctrl+C'd or quit by a fatal error.
First off, we have to tell PHP to use ticks, in order for the catches to work (for more information on ticks check the pcntl_signal() documentation):
declare(ticks = 1);
Then, we add shutdown functions to the classes that have to rollback the state:
/**
* Cleanup if process has been killed unexpectedly
*
*/
private function shutdown()
{
$this->getTargetDb()->rollBack();
print('Script quit unexpectedly. Doing rollback' . PHP_EOL);
exit();
}
/**
* Method that is executed, when a Fatal Error occurs
*
*/
public function fatalErrorShutdown()
{
$lastError = error_get_last();
if (!is_null($lastError) && $lastError['type'] === E_ERROR) {
$this->shutdown();
}
}
/**
* Method, that is executed, if script has been killed by
* SIGINT: Ctrl+C
* SIGTERM: kill
*
* @param int $signal
*
*/
public function sigintShutdown($signal)
{
if ($signal === SIGINT || $signal === SIGTERM) {
$this->shutdown();
}
}
You shouldn't forget to include an exit() at some point in the shutdown methods or your script won't quit at all. In the example the shutdown() method (which is called by both shutdown methods, that will be registered afterwards) quits the script as soon as it's done.
As a last step, you only have to register your shutdown methods and you're done:
$sync = new Process_Sync();
// Catch Fatal Error (Rollback)
register_shutdown_function(array($sync, 'fatalErrorShutdown'));
// Catch Ctrl+C, kill and SIGTERM (Rollback)
pcntl_signal(SIGTERM, array($sync, 'sigintShutdown'));
pcntl_signal(SIGINT, array($sync, 'sigintShutdown'));
by Dominik Jungowski
27. Mai 2009
No Comment
Some words on sessions I attended on day 2.
OOP … aber richtig (Stefan Priebsch)
Some known basics about OOP. It was ok, but it really was nothing new.
Organized serendipity: Inside report from the core of PHP development (Lukas Smith)
Finally I know why the namespace separator discussion took so long ;-)
Multi-Master MySQL (Arne Blankerts)
While the session itself was rather disappointing, it got me thinking a lot about how it would be possible to set up a multi master architecture. If I ever have any good idea, I'll let you know ;-)
by Dominik Jungowski
26. Mai 2009
No Comment
Some words on the sessions I attended today, but first off: Since I only got here yesterday, I could not attend the workshop day.
MySQL Performance Tuning & Optimization Capsule (Sonali Minocha)
Some known stuff, a lot of new tweaks, optimizations and dos/don'ts I didn't know yet. For example PROCEDURE ANALYSE() was totally new to me, you would use it like this: select * from mysql.user procedure analyse(); Basically all this does is to check the results the query produces and it then tells you the optimal type you should use for each column, based on the result data. For more information check the documentation linked above. Furthermore the talk was about optimizing query cache, InnoDB specific optimizations, Index optimiazations and many more.
Sichere Applikationen auf Basis des Zend-Frameworks (Stefan Esser)
I was really looking forward to this session. Although most of the shown, like the proper use of Validators, Filters or how to access $_GET/$_POST/etc. variables correctly, there were things that were new to me, like Zend Frameworks build-in possibility against Cross-Site Request Forgery (CSRF). On one hand, I had hoped to learn a bit more, but on the other hand it maybe tells me that I'm on the right course… ;-)
Why Architecture in Web Development matters (Lars Jankowfsky)
Lars showed some really interesting basics for building a good architecture, spiced up with some examples of how they did it at Swoodoo. I already enjoyed Lars' article on architecture in the PHP magazin a while ago and this talk was a good extension to it. Generally it seems that SOA (Service Oriented Archicture) is a good way for abstraction in architecture, even though it makes integration testing harder, since you have to write more mock-up objects.
The Present and Future of PHP (Andrei Zmievski)
Everytime you think about coding in Ruby, Domo-Kun kills a kitten!
DTrace für AMP-Entwickler und Administratoren (Johannes Schlüter)
Johannes showed really nice ways to trace (and debug) processes (and with focus on php) not using strace, but Suns dtrace. It's possibilites really impressed me and there's not really anything I can say about it right now, apart from the fact that you need PHP 5.3 or MySQL 5.4 (there are backports on Solaris up to 5.1) to be able to use it for those two.
By the way: That was fun (in German) ;D
by Dominik Jungowski
6. Mai 2009
One Comment
If you should come across this error message in your JavaScript console and the error is thrown in a JavaScript file that is included via <script src="js/somefile.js" type="text/javascript">: Check that there are no script Tags in your .js file