ExtenderDaemon   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRunPath() 0 2 1
A __construct() 0 31 6
A installWorkers() 0 22 1
A setup() 0 14 1
1
<?php namespace Comodojo\Extender;
2
3
use \Comodojo\Foundation\Base\ConfigurationTrait;
4
use \Comodojo\Foundation\Events\Manager as EventsManager;
5
use \Comodojo\Foundation\Base\Configuration;
6
use \Comodojo\Foundation\Logging\Manager as LogManager;
7
use \Comodojo\Foundation\Utils\ArrayOps;
8
use \Comodojo\Daemon\Daemon as AbstractDaemon;
9
use \Comodojo\Extender\Workers\ScheduleWorker;
10
use \Comodojo\Extender\Workers\QueueWorker;
11
use \Comodojo\Extender\Task\Table as TasksTable;
12
use \Comodojo\Extender\Components\Database;
13
use \Comodojo\Extender\Traits\TasksTableTrait;
14
use \Comodojo\Extender\Socket\SocketInjector;
15
use \Comodojo\Extender\Traits\CacheTrait;
16
use \Comodojo\SimpleCache\Manager as SimpleCacheManager;
17
use \Psr\Log\LoggerInterface;
18
use \Exception;
19
20
/**
21
 * @package     Comodojo Extender
22
 * @author      Marco Giovinazzi <[email protected]>
23
 * @license     MIT
24
 *
25
 * LICENSE:
26
 *
27
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
 * THE SOFTWARE.
34
 */
35
36
class ExtenderDaemon extends AbstractDaemon {
37
38
    use ConfigurationTrait;
39
    use CacheTrait;
40
    use TasksTableTrait;
41
42
    protected static $default_properties = array(
43
        'pidfile' => '',
44
        'sockethandler' => '',
45
        'socketbuffer' => 1024,
46
        'sockettimeout' => 2,
47
        'socketmaxconnections' => 100,
48
        'niceness' => 0,
49
        'arguments' => '\\Comodojo\\Daemon\\Console\\DaemonArguments',
50
        'description' => 'Extender Daemon'
51
    );
52
53
    public function __construct(
54
        array $configuration,
55
        array $tasks,
56
        EventsManager $events = null,
57
        SimpleCacheManager $cache = null,
58
        LoggerInterface $logger = null
59
    ) {
60
61
        $this->configuration = new Configuration(self::$default_properties);
62
        $this->configuration->merge($configuration);
63
64
        $run_path = $this->getRunPath();
65
66
        if ( empty($this->configuration->get('sockethandler')) ) {
67
            $this->configuration->set('sockethandler', "unix://$run_path/extender.sock");
68
        }
69
70
        if ( empty($this->configuration->get('pidfile')) ) {
71
            $this->configuration->set('pidfile', "$run_path/extender.pid");
72
        }
73
74
        $logger = is_null($logger) ? LogManager::createFromConfiguration($this->configuration, "extender-log")->getLogger() : $logger;
75
        $events = is_null($events) ? EventsManager::create($logger) : $events;
76
77
        parent::__construct(ArrayOps::replaceStrict(self::$default_properties, $this->configuration->get()), $logger, $events);
78
79
        $table = new TasksTable($this->configuration, $this->getLogger(), $this->getEvents());
80
        $table->addBulk($tasks);
81
        $this->setTasksTable($table);
82
83
        $this->setCache(is_null($cache) ? SimpleCacheManager::createFromConfiguration($this->configuration, $this->logger) : $cache);
84
85
    }
86
87
    public function setup() {
88
89
        // try {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
        //     if ( Database::validate($this->configuration) === false ){
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
        //         printf("\nWARNING: %s\n\n", "database seems to be not in sync!");
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
92
        //     }
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
        // } catch (Exception $e) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
        //     printf("\nFATAL ERROR: %s\n\n", $e->getMessage());
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
        //     $this->end(1);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
        // }
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
98
        $this->installWorkers();
99
100
        SocketInjector::inject($this);
101
102
    }
103
104
    protected function installWorkers() {
105
106
        // add workers
107
        $manager = $this->getWorkers();
108
109
        $schedule_worker = new ScheduleWorker("scheduler");
110
        $schedule_worker
111
            ->setConfiguration($this->getConfiguration())
112
            ->setLogger($this->getLogger())
113
            ->setEvents($this->getEvents())
114
            ->setTasksTable($this->getTasksTable());
115
116
        $queue_worker = new QueueWorker("queue");
117
        $queue_worker
118
            ->setConfiguration($this->getConfiguration())
119
            ->setLogger($this->getLogger())
120
            ->setEvents($this->getEvents())
121
            ->setTasksTable($this->getTasksTable());
122
123
        $manager
124
            ->install($schedule_worker, 1, true)
125
            ->install($queue_worker, 1, true);
126
127
    }
128
129
    private function getRunPath() {
130
        return $this->configuration->get('base-path')."/".$this->configuration->get('run-path');
131
    }
132
133
}
134