Skip to content

Commit 4155751

Browse files
committed
init
0 parents  commit 4155751

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea
2+
/vendor
3+
composer.lock

MIT-LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Dima Levischenko
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Installation
2+
3+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
4+
5+
Either run
6+
7+
```bash
8+
php composer.phar require --prefer-dist hex-abyss/duplicate "*"
9+
```
10+
11+
or add
12+
13+
```json
14+
"hex-abyss/duplicate": "*"
15+
```
16+
17+
to the require section of your `composer.json` file.
18+
19+
## Usage
20+
21+
```php
22+
while(true){
23+
//some code
24+
if (\hexAbyss\Duplicate::isStopProcess())
25+
break;
26+
}
27+
```
28+
29+
```php
30+
while(true){
31+
//some code
32+
\hexAbyss\Duplicate::isExit();
33+
}
34+
```
35+
36+
```php
37+
//file: index.php
38+
while(true){
39+
//some code
40+
if (\hexAbyss\Duplicate::isStopProcess())
41+
break;
42+
}
43+
44+
//file: info.php
45+
\hexAbyss\Duplicate::getInstance('keyInfo');
46+
while(true){
47+
//some code
48+
if (\hexAbyss\Duplicate::isStopProcess())
49+
break;
50+
}
51+
```
52+
53+
## License
54+
55+
The source code for the site is licensed under the MIT license, which you can find in
56+
the MIT-LICENSE.md file.

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "hex-abyss/duplicate",
3+
"description": "To check is duplicate process",
4+
"keywords": ["console", "process", "duplicate", "duplicate process", "stop process", "stop fork process", "stop other process"],
5+
"license": "MIT",
6+
"homepage": "https://github.com/hex-abyss/duplicate",
7+
"authors": [
8+
{
9+
"name": "Dima Levischenko",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"support": {
14+
"issues": "https://github.com/hex-abyss/duplicate/issues?state=open",
15+
"source": "https://github.com/hex-abyss/duplicate"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"hexAbyss\\": "src"
20+
}
21+
},
22+
"require": {
23+
"php": ">5.2"
24+
}
25+
}

src/Duplicate.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace hexAbyss;
4+
5+
/**
6+
* To check is duplicate process
7+
*
8+
* @package hexAbyss
9+
*/
10+
class Duplicate
11+
{
12+
private static $pid;
13+
private static $pidFile;
14+
private static $instance;
15+
16+
/**
17+
* Duplicate constructor.
18+
* @param string $processPrefix
19+
*/
20+
private function __construct($processPrefix = '')
21+
{
22+
self::$pid = getmypid();
23+
self::$pidFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $processPrefix . '_process.pid';
24+
25+
if ($this->createPidFile()) {
26+
register_shutdown_function([$this, 'removePidFile']);
27+
}
28+
}
29+
30+
/**
31+
* @param string $processPrefix
32+
* @return self
33+
*/
34+
public static function getInstance($processPrefix = '')
35+
{
36+
if (null === self::$instance) {
37+
self::$instance = new self($processPrefix);
38+
}
39+
40+
return self::$instance;
41+
}
42+
43+
/**
44+
* @param string $message
45+
* @return void
46+
*/
47+
public static function isExit($message = '')
48+
{
49+
if (self::isStopProcess()) {
50+
exit($message);
51+
}
52+
}
53+
54+
/**
55+
* @return bool
56+
*/
57+
public static function isStopProcess()
58+
{
59+
if (null === self::$instance) {
60+
self::getInstance();
61+
}
62+
63+
if (file_exists(self::$pidFile)) {
64+
return ((int)file_get_contents(self::$pidFile)) !== self::$pid;
65+
}
66+
67+
return true;
68+
}
69+
70+
/**
71+
* @return bool
72+
*/
73+
public static function removePidFile()
74+
{
75+
return file_exists(self::$pidFile) && !self::isStopProcess() && unlink(self::$pidFile);
76+
}
77+
78+
/**
79+
* @return false|int
80+
*/
81+
private function createPidFile()
82+
{
83+
return file_put_contents(self::$pidFile, self::$pid, LOCK_EX);
84+
}
85+
}

0 commit comments

Comments
 (0)