Completed
Pull Request — master (#16)
by Akihito
03:03
created

Worker::hasTask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Ackintosh\Snidel;
3
4
use Ackintosh\Snidel\Result\QueueInterface as ResultQueueInterface;
5
use Ackintosh\Snidel\Result\Result;
6
use Ackintosh\Snidel\Task\QueueInterface as TaskQueueInterface;
7
8
class Worker
9
{
10
    /** @var \Ackintosh\Snidel\Task\Task */
11
    private $task;
12
13
    /** @var \Ackintosh\Snidel\Fork\Process */
14
    private $process;
15
16
    /** @var \Ackintosh\Snidel\Task\QueueInterface */
17
    private $taskQueue;
18
19
    /** @var \Ackintosh\Snidel\Result\QueueInterface */
20
    private $resultQueue;
21
22
    /** @var \Ackintosh\Snidel\Pcntl */
23
    private $pcntl;
24
25
    /** @var bool */
26
    private $done = false;
27
28
    /**
29
     * @param   \Ackintosh\Snidel\Fork\Process $process
30
     */
31
    public function __construct($process)
32
    {
33
        $this->pcntl = new Pcntl();
34
        $this->process = $process;
35
    }
36
37
    /**
38
     * @param   \Ackintosh\Snidel\Task\QueueInterface
39
     * @return  void
40
     */
41
    public function setTaskQueue(TaskQueueInterface $queue)
42
    {
43
        $this->taskQueue = $queue;
44
    }
45
46
    /**
47
     * @param   \Ackintosh\Snidel\Result\QueueInterface
48
     * @return  void
49
     */
50
    public function setResultQueue(ResultQueueInterface $queue)
51
    {
52
        $this->resultQueue = $queue;
53
    }
54
55
    /**
56
     * @return  int
57
     */
58
    public function getPid()
59
    {
60
        return $this->process->getPid();
61
    }
62
63
    /**
64
     * @return  void
65
     * @throws  \RuntimeException
66
     */
67
    public function run()
68
    {
69
        try {
70
            $this->task = $this->taskQueue->dequeue();
71
            $result = $this->task->execute();
72
        } catch (\RuntimeException $e) {
73
            throw $e;
74
        }
75
76
        $result->setProcess($this->process);
77
78
        try {
79
            $this->resultQueue->enqueue($result);
80
            $this->done = true;
81
        } catch (\RuntimeException $e) {
82
            throw $e;
83
        }
84
    }
85
86
    /**
87
     * @return  void
88
     * @throws  \RuntimeException
89
     */
90
    public function error()
91
    {
92
        $result = new Result();
93
        $result->setError(error_get_last());
94
        $result->setTask($this->task);
95
        $result->setProcess($this->process);
96
97
        try {
98
            $this->resultQueue->enqueue($result);
99
        } catch (\RuntimeException $e) {
100
            throw $e;
101
        }
102
    }
103
104
    /**
105
     * @param   int     $sig
106
     * @return  void
107
     */
108
    public function terminate($sig)
109
    {
110
        posix_kill($this->process->getPid(), $sig);
111
        $status = null;
112
        $this->pcntl->waitpid($this->process->getPid(), $status);
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    public function hasTask()
119
    {
120
        return $this->task !== null;
121
    }
122
123
    /**
124
     * @return bool
125
     */
126
    public function done()
127
    {
128
        return $this->done;
129
    }
130
}
131