Permalink
Browse files

Implemented feature : SIGTERM and SIGINT calls are now caught and imp…

…ort is smoothly and safely interrupted (PCNTL extension needed)
  • Loading branch information...
1 parent cae8171 commit 9661e65504a0a5a44aa8e8c247fe0701ceacafa8 @lolautruche committed Sep 26, 2010
View
@@ -1,4 +1,4 @@
-build.version=1.0.0
+build.version=1.2.0
build.authors=Jerome Vieilledent
build.dir=build
build.dir.dist=build/_dist
View
@@ -32,6 +32,7 @@
$script->initialize();
$script->setUseDebugAccumulators( true );
+include_once 'extension/sqliimport/modules/sqliimport/sigtermhandler.php';
include_once 'extension/sqliimport/modules/sqliimport/fatalerrorhandler.php';
try
@@ -8,6 +8,7 @@
* @package sqliimport
*/
+include_once 'extension/sqliimport/modules/sqliimport/sigtermhandler.php';
include_once 'extension/sqliimport/modules/sqliimport/fatalerrorhandler.php';
try
View
@@ -1,3 +1,7 @@
+1.2.0
+-----------------
+Implemented feature : SIGTERM and SIGINT calls are now caught and import is smoothly and safely interrupted (PCNTL extension needed)
+
1.1.0
-----------------
Added manual frequency support for scheduled imports (not less than every 5 minutes)
View
@@ -4,9 +4,9 @@
SQLIImport eZ Publish extension
=================================
-*Version 1.0 - 2010 Jerome Vieilledent for SQLi*
+*Version 1.2 - 2010 Jerome Vieilledent for SQLi*
-:Date: 2010/08/26
+:Date: 2010/09/26
.. contents:: Table of contents
@@ -45,7 +45,6 @@ There are two types of imports :
- Scheduled
Each import is stored in the database as pending and awaits for the cronjob to process it.
-Running imports are safely interruptable from the admin interface.
Pending imports can be cancelled while the cronjob has not processed it.
Immediate
@@ -74,6 +73,30 @@ You can only add one option per line with format **optionName=optionValue**.
Options will be passed to the import handler at runtime (in the handler constructor).
+Import Interruption
+===================
+Running imports are safely interruptable from the admin interface or from the CLI.
+
+From admin interface
+~~~~~~~~~~~~~~~~~~~~
+From the import list, you can interrupt a running import by clicking the **Interrupt** link of the import.
+Please note that you will need to have access to the *manageimports* policy function for the current import handler.
+
+From CLI
+~~~~~~~~
+From version 1.2.0, SQLI Import catches *SIGTERM* and *SIGINT* signals.
+This is made possible thanks to `PCNTL extension <http://php.net/pcntl>`_ (won't work on Windows).
+You can thus safely interrupt a running import with **kill** command :
+
+::
+
+ kill -2 <import_script_pid>
+ kill -15 <import_script_pid>
+
+Please note that **kill -9** (*SIGKILL*) signal cannot be caught, so always prefer using SIGTERM (**kill -15**) or SIGINT (**kill -2**).
+You can also ask for import interruption by pressing **Ctrl+C**, which sends a *SIGINT* signal.
+
+
------------
CLI SCRIPT
------------
View
Binary file not shown.
@@ -0,0 +1,42 @@
+<?php
+/**
+ * SIGTERM Handler.
+ * This piece of code allows to catch system signals such as SIGTERM, SIGINT or SIGKILL
+ * Will cleanup the running import by interrupting the process safely
+ * Needs {@link http://php.net/pcntl PCNTL} to work properly (doesn't work on Windows)
+ * @uses PCNTL
+ * @copyright Copyright (C) 2010 - SQLi Agency. All rights reserved
+ * @licence http://www.gnu.org/licenses/gpl-2.0.txt GNU GPLv2
+ * @author Jerome Vieilledent
+ * @version @@@VERSION@@@
+ * @package sqliimport
+ */
+
+$isPcntl = function_exists( 'pcntl_signal' );
+if( $isPcntl )
+{
+ declare( ticks = 1 );
+
+ function sigHandler( $signo )
+ {
+ if( SQLIImportToken::importIsRunning() )
+ {
+ // Note : SIGKILL cannot be caught
+ // So try to always send a SIGINT (kill -2) or SIGTERM (kill -15) to request interruption
+ switch( $signo )
+ {
+ case SIGTERM:
+ case SIGINT:
+ SQLIImportLogger::logNotice( 'Caught SIGTERM while importing. Demanding import interruption (might take a little while)' );
+ $factory = SQLIImportFactory::instance();
+ $currentItem = $factory->getCurrentImportItem();
+ $currentItem->setAttribute( 'status', SQLIImportItem::STATUS_INTERRUPTED );
+ $currentItem->store();
+ break;
+ }
+ }
+ }
+
+ pcntl_signal( SIGTERM, 'sigHandler' );
+ pcntl_signal( SIGINT, 'sigHandler' );
+}

0 comments on commit 9661e65

Please sign in to comment.