Completed
Push — 2.0 ( 7f0480...29c56f )
by Marco
13:59
created

ExtenderDaemon::getRunPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Comodojo\Extender;
2
3
use \Comodojo\Foundation\Base\ConfigurationTrait;
4
use \Comodojo\Foundation\Logging\LoggerTrait;
5
use \Comodojo\Foundation\Events\EventsTrait;
6
use \Comodojo\Foundation\Events\Manager as EventsManager;
7
use \Comodojo\Foundation\Base\Configuration;
8
use \Comodojo\Foundation\Logging\Manager as LogManager;
9
use \Comodojo\Foundation\Utils\ArrayOps;
10
use \Comodojo\Daemon\Daemon as AbstractDaemon;
11
use \Comodojo\Extender\Workers\ScheduleWorker;
12
use \Comodojo\Extender\Workers\QueueWorker;
13
use \Comodojo\Extender\Task\Table as TasksTable;
14
use \Comodojo\Extender\Components\Database;
15
use \Comodojo\Extender\Traits\TasksTableTrait;
16
use \Comodojo\Extender\Queue\SocketInjector as QueueSocketInjector;
17
use \Comodojo\Extender\Schedule\SocketInjector as ScheduleSocketInjector;
18
use \Comodojo\Extender\Traits\CacheTrait;
19
use \Comodojo\SimpleCache\Manager as SimpleCacheManager;
20
use \Psr\Log\LoggerInterface;
21
use \Exception;
22
23
/**
24
 * @package     Comodojo Extender
25
 * @author      Marco Giovinazzi <[email protected]>
26
 * @license     MIT
27
 *
28
 * LICENSE:
29
 *
30
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36
 * THE SOFTWARE.
37
 */
38
39
class ExtenderDaemon extends AbstractDaemon {
40
41
    use ConfigurationTrait;
42
    use EventsTrait;
43
    use LoggerTrait;
44
    use CacheTrait;
45
    use TasksTableTrait;
46
47
    protected static $default_properties = array(
48
        'pidfile' => '',
49
        'sockethandler' => '',
50
        'socketbuffer' => 1024,
51
        'sockettimeout' => 2,
52
        'socketmaxconnections' => 100,
53
        'niceness' => 0,
54
        'arguments' => '\\Comodojo\\Daemon\\Console\\DaemonArguments',
55
        'description' => 'Extender Daemon'
56
    );
57
58
    public function __construct(
59
        array $configuration,
60
        array $tasks,
61
        EventsManager $events = null,
62
        SimpleCacheManager $cache = null,
63
        LoggerInterface $logger = null
64
    ) {
65
66
        $this->configuration = new Configuration(self::$default_properties);
67
        $this->configuration->merge($configuration);
68
69
        $run_path = $this->getRunPath();
70
71
        if ( empty($this->configuration->get('sockethandler')) ) {
72
            $this->configuration->set('sockethandler', "unix://$run_path/extender.sock");
73
        }
74
75
        if ( empty($this->configuration->get('pidfile')) ) {
76
            $this->configuration->set('pidfile', "$run_path/extender.pid");
77
        }
78
79
        $logger = is_null($logger) ? LogManager::createFromConfiguration($this->configuration, "extender-log")->getLogger() : $logger;
80
        $events = is_null($events) ? EventsManager::create($logger) : $events;
81
82
        parent::__construct(ArrayOps::replaceStrict(self::$default_properties, $this->configuration->get()), $logger, $events);
83
84
        $table = new TasksTable($this->configuration, $this->getLogger(), $this->getEvents());
85
        $table->addBulk($tasks);
86
        $this->setTasksTable($table);
87
88
        $this->setCache(is_null($cache) ? SimpleCacheManager::createFromConfiguration($this->configuration, $this->logger) : $cache);
89
90
    }
91
92
    public function setup() {
93
94
        // try {
95
        //     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...
96
        //         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...
97
        //     }
98
        // } 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...
99
        //     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...
100
        //     $this->end(1);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
101
        // }
102
103
        $this->installWorkers();
104
105
        $commands = $this->getSocket()->getCommands();
106
107
        QueueSocketInjector::inject($commands);
108
        ScheduleSocketInjector::inject($commands);
109
110
    }
111
112
    protected function installWorkers() {
113
114
        // add workers
115
        $manager = $this->getWorkers();
116
117
        $schedule_worker = new ScheduleWorker("scheduler");
118
        $schedule_worker
119
            ->setConfiguration($this->getConfiguration())
120
            ->setLogger($this->getLogger())
121
            ->setEvents($this->getEvents())
122
            ->setTasksTable($this->getTasksTable());
123
124
        $queue_worker = new QueueWorker("queue");
125
        $queue_worker
126
            ->setConfiguration($this->getConfiguration())
127
            ->setLogger($this->getLogger())
128
            ->setEvents($this->getEvents())
129
            ->setTasksTable($this->getTasksTable());
130
131
        $manager
132
            ->install($schedule_worker, 1, true)
133
            ->install($queue_worker, 1, true);
134
135
    }
136
137
    private function getRunPath() {
138
        return $this->configuration->get('base-path')."/".$this->configuration->get('run-path');
139
    }
140
141
}
142