Test Setup Failed
Push — master ( 894c42...6b221f )
by Php Easy Api
03:38
created

SupervisorJob   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 137
rs 10
c 1
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A stopWorkerForSupervisor() 0 3 1
A getWorkersForSupervisor() 0 3 1
A __construct() 0 7 1
A execute() 0 20 2
A isSupervisorRunning() 0 3 1
A reReadForSupervisor() 0 3 1
A startWorkerForSupervisor() 0 3 1
A updateForSupervisor() 0 3 1
A putConfigurationFile() 0 15 2
1
<?php
2
3
namespace Resta\Worker;
4
5
use Resta\Support\Process;
0 ignored issues
show
Bug introduced by
The type Resta\Support\Process was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Resta\Contracts\JobContracts;
7
use Resta\Contracts\ApplicationContracts;
8
use Resta\Foundation\ApplicationProvider;
9
use Resta\Contracts\WorkerManagerContracts;
10
11
class SupervisorJob extends ApplicationProvider implements JobContracts
12
{
13
    /**
14
     * @var null|object
15
     */
16
    protected $worker;
17
18
    /**
19
     * @var Process
20
     */
21
    protected $process;
22
23
    /**
24
     * DefaultJob constructor.
25
     *
26
     * @param ApplicationContracts $app
27
     * @param WorkerManagerContracts $worker
28
     */
29
    public function __construct(ApplicationContracts $app,WorkerManagerContracts $worker)
30
    {
31
        parent::__construct($app);
32
33
        $this->worker = $worker;
34
35
        $this->process = new Process();
36
    }
37
38
    /**
39
     * execute job
40
     *
41
     * @return mixed|void
42
     */
43
    public function execute()
44
    {
45
        $this->isSupervisorRunning();
46
47
        if($this->app->get('WORKER_STATUS')===true){
48
49
            $this->stopWorkerForSupervisor();
50
        }
51
        else{
52
53
            $this->putConfigurationFile();
54
55
            $this->reReadForSupervisor();
56
57
            $this->updateForSupervisor();
58
59
            $this->startWorkerForSupervisor();
60
        }
61
62
        echo $this->getWorkersForSupervisor();
63
    }
64
65
    /**
66
     * get workers for supervisor
67
     *
68
     * @return mixed
69
     */
70
    public function getWorkersForSupervisor()
71
    {
72
        return $this->process->command(config('supervisor.commands.workers'));
73
    }
74
75
    /**
76
     * check if the supervisor is or not running
77
     *
78
     * @return bool
79
     */
80
    public function isSupervisorRunning()
81
    {
82
        return $this->process->command(config('supervisor.commands.status'));
83
    }
84
85
    /**
86
     * reread for supervisor
87
     *
88
     * @return mixed
89
     */
90
    public function reReadForSupervisor()
91
    {
92
        return $this->process->command(config('supervisor.commands.reread'));
93
    }
94
95
    /**
96
     * start Worker for supervisor
97
     *
98
     * @return mixed
99
     */
100
    public function startWorkerForSupervisor()
101
    {
102
        return $this->process->command(config('supervisor.commands.start').' '.$this->app->get('WORKER').':*');
103
    }
104
105
    /**
106
     * stop worker for supervisor
107
     *
108
     * @return mixed
109
     */
110
    public function stopWorkerForSupervisor()
111
    {
112
        return $this->process->command(config('supervisor.commands.stop').' '.$this->app->get('WORKER').':*');
113
    }
114
115
    /**
116
     * put configutation file for supervisor
117
     *
118
     * @return mixed|void
119
     */
120
    public function putConfigurationFile()
121
    {
122
        $path = config('supervisor.path').'/'.$this->app->get('WORKER').'.conf';
123
124
        if(files()->exists($path)===false){
125
            files()->put($path,'
126
[program:'.$this->app()->get('WORKER').']
127
process_name=%(program_name)s_%(process_num)02d
128
command=php '.root.'/api worker run '.$this->app->get('PROJECT_NAME').' worker:'.$this->worker->getWorker().'
0 ignored issues
show
Bug introduced by
The method getWorker() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
command=php '.root.'/api worker run '.$this->app->get('PROJECT_NAME').' worker:'.$this->worker->/** @scrutinizer ignore-call */ getWorker().'

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
autostart=true
130
autorestart=true
131
user=root
132
numprocs=1
133
redirect_stderr=true
134
stdout_logfile='.root.'/worker.log
135
');
136
        }
137
138
    }
139
140
    /**
141
     * update for supervisor
142
     *
143
     * @return mixed
144
     */
145
    public function updateForSupervisor()
146
    {
147
        return $this->process->command(config('supervisor.commands.update'));
148
    }
149
150
}