File tree Expand file tree Collapse file tree 2 files changed +26
-5
lines changed Expand file tree Collapse file tree 2 files changed +26
-5
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,23 @@ public function isRunning()
69
69
return false ;
70
70
}
71
71
72
+ /**
73
+ * Stops the process.
74
+ *
75
+ * @return boolean `true` if the processes was stopped, `false` otherwise.
76
+ */
77
+ public function stop ()
78
+ {
79
+ try {
80
+ $ result = shell_exec (sprintf ('kill %d 2>&1 ' , $ this ->pid ));
81
+ if (!preg_match ('/No such process/ ' , $ result )) {
82
+ return true ;
83
+ }
84
+ } catch (Exception $ e ) {}
85
+
86
+ return false ;
87
+ }
88
+
72
89
/**
73
90
* Returns the ID of the process.
74
91
*
Original file line number Diff line number Diff line change 10
10
/**
11
11
* BackgroundProcessTest
12
12
*
13
- * @category Test
13
+ * @category test
14
14
* @package cocur/background-process
15
15
* @author Florian Eckerstorfer <[email protected] >
16
16
* @copyright 2013-2104 Florian Eckerstorfer
@@ -26,13 +26,17 @@ class BackgroundProcessTest extends \PHPUnit_Framework_TestCase
26
26
* @covers Cocur\BackgroundProcess\BackgroundProcess::run()
27
27
* @covers Cocur\BackgroundProcess\BackgroundProcess::isRunning()
28
28
* @covers Cocur\BackgroundProcess\BackgroundProcess::getPid()
29
+ * @covers Cocur\BackgroundProcess\BackgroundProcess::stop()
29
30
*/
30
31
public function testRun ()
31
32
{
32
- $ process = new BackgroundProcess ('sleep 1 ' );
33
- $ this ->assertFalse ($ process ->isRunning ());
33
+ $ process = new BackgroundProcess ('sleep 5 ' );
34
+ $ this ->assertFalse ($ process ->isRunning (), ' process should not run ' );
34
35
$ process ->run ();
35
- $ this ->assertNotNull ($ process ->getPid ());
36
- $ this ->assertTrue ($ process ->isRunning ());
36
+ $ this ->assertNotNull ($ process ->getPid (), 'process should have a pid ' );
37
+ $ this ->assertTrue ($ process ->isRunning (), 'process should run ' );
38
+ $ this ->assertTrue ($ process ->stop (), 'stop process ' );
39
+ $ this ->assertFalse ($ process ->isRunning (), 'processes should not run anymore ' );
40
+ $ this ->assertFalse ($ process ->stop (), 'cannot stop process that is not running ' );
37
41
}
38
42
}
You can’t perform that action at this time.
0 commit comments