Completed
Push — next ( a537c2...29cbfe )
by Thomas
08:43
created

ProcessSync::enter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 17
rs 9.4285
1
<?php
2
/****************************************************************************
3
 * For license information see doc/license.txt
4
 * Unicode Reminder メモ
5
 * synchronization of processes which must not run concurrently;
6
 ****************************************************************************/
7
8
namespace Oc\Util;
9
10
/**
11
 * Class ProcessSync
12
 */
13
class ProcessSync
14
{
15
    /** @var string $pidFilePath */
16
    private $pidFilePath;
17
18
19
    /**
20
     * ProcessSync constructor.
21
     *
22
     * @param $name
23
     */
24
    public function __construct($name)
25
    {
26
        $this->pidFilePath = __DIR__ . '/../../../cache2/' . $name . '.pid';
27
    }
28
29
30
    /**
31
     * Enter code section which must not run concurrently
32
     *
33
     * @return bool
34
     */
35
    public function enter()
36
    {
37
        if (!$this->checkDaemon()) {
38
            return false;
39
        }
40
41
        if ($pidFile = @fopen($this->pidFilePath, 'w')) {
42
            fwrite($pidFile, posix_getpid());
43
            fclose($pidFile);
44
45
            return true;
46
        } else {
47
            echo "can't create PidFile " . $this->pidFilePath . "\n";
48
49
            return false;
50
        }
51
    }
52
53
    /**
54
     * checks if other instance of process is running
55
     *
56
     * @return bool
57
     */
58
    private function checkDaemon()
59
    {
60
        if (file_exists($this->pidFilePath) && $pidFile = @fopen($this->pidFilePath, 'r')) {
61
            $pidDaemon = fgets($pidFile, 20);
62
            fclose($pidFile);
63
64
            $pidDaemon = (int)$pidDaemon;
65
66
            // bad PID file, e.g. due to system malfunction while creating the file?
67
            if ($pidDaemon <= 0) {
68
                echo 'removing bad PidFile (' . $this->pidFilePath . ")\n";
69
                unlink($this->pidFilePath);
70
71
                return false;
72
            } elseif (posix_kill($pidDaemon, 0)) { // process running?
73
                // yes, good bye
74
                echo 'Error: process for ' . $this->pidFilePath . " is already running with pid=$pidDaemon\n";
75
76
                return false;
77
            } else {
78
                // no, remove pid_file
79
                echo 'process not running, removing old PidFile (' . $this->pidFilePath . ")\n";
80
                unlink($this->pidFilePath);
81
82
                return true;
83
            }
84
        }
85
86
        return true;
87
    }
88
89
90
    /**
91
     * Leave code section which must not run concurrently
92
     *
93
     * @param bool $message
94
     * @return bool
95
     */
96
    public function leave($message = false)
97
    {
98
        if ($message) {
99
            echo $message . "\n";
100
        }
101
102
        if ($pidFile = @fopen($this->pidFilePath, 'r')) {
103
            $pid = fgets($pidFile, 20);
104
            fclose($pidFile);
105
            if ($pid == posix_getpid()) {
106
                unlink($this->pidFilePath);
107
            }
108
109
            return true;
110
        }
111
112
        echo "Error: can't delete own PidFile (" . $this->pidFilePath . ")\n";
113
114
        return false;
115
    }
116
}
117