Completed
Push — 2.0 ( 76e968...4129d9 )
by Marco
13:01
created

ExtenderDaemon::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 2
eloc 14
nc 1
nop 5
1
<?php namespace Comodojo\Extender;
2
3
use \Comodojo\Extender\Workers\ScheduleWorker;
4
use \Comodojo\Extender\Workers\QueueWorker;
5
use \Comodojo\Extender\Task\Table as TasksTable;
6
use \Comodojo\Extender\Task\Request;
7
use \Comodojo\Extender\Task\TaskParameters;
8
use \Comodojo\Extender\Traits\ConfigurationTrait;
9
use \Comodojo\Extender\Traits\TasksTableTrait;
10
use \Comodojo\Extender\Traits\EntityManagerTrait;
11
use \Comodojo\Extender\Components\Database;
12
use \Comodojo\Extender\Queue\Manager as QueueManager;
13
use \Comodojo\Daemon\Daemon as AbstractDaemon;
14
use \Comodojo\Daemon\Traits\LoggerTrait;
15
use \Comodojo\Daemon\Traits\EventsTrait;
16
use \Comodojo\Dispatcher\Traits\CacheTrait;
17
use \Comodojo\Foundation\Events\Manager as EventsManager;
18
use \Comodojo\Foundation\Base\Configuration;
19
use \Comodojo\Foundation\Logging\Manager as LogManager;
20
use \Comodojo\Foundation\Utils\ArrayOps;
21
use \Comodojo\SimpleCache\Manager as SimpleCacheManager;
22
use \Doctrine\ORM\EntityManager;
23
use \Psr\Log\LoggerInterface;
24
25
class ExtenderDaemon extends AbstractDaemon {
26
27
    use ConfigurationTrait;
28
    use EventsTrait;
29
    use LoggerTrait;
30
    use CacheTrait;
31
    use TasksTableTrait;
32
    use EntityManagerTrait;
33
34
    protected static $default_properties = array(
35
        'pidfile' => 'extender.pid',
36
        'socketfile' => 'unix://extender.sock',
37
        'socketbuffer' => 8192,
38
        'sockettimeout' => 15,
39
        'niceness' => 0,
40
        'arguments' => '\\Comodojo\\Daemon\\Console\\DaemonArguments',
41
        'description' => 'Extender Daemon'
42
    );
43
44
    public function __construct(
45
        array $configuration,
46
        array $tasks,
47
        EventsManager $events = null,
48
        SimpleCacheManager $cache = null,
49
        LoggerInterface $logger = null
50
    ) {
51
52
        $this->configuration = new Configuration(self::$default_properties);
53
        $this->configuration->merge($configuration);
54
55
        parent::__construct(ArrayOps::replaceStrict(self::$default_properties, $this->configuration->get()), $logger, $events);
56
57
        $table = new TasksTable($this->configuration, $this->getLogger(), $this->getEvents());
58
        $table->addBulk($tasks);
59
        $this->setTasksTable($table);
60
61
        $this->setCache(is_null($cache) ? SimpleCacheManager::createFromConfiguration($this->configuration, $this->logger) : $cache);
62
63
        $this->setEntityManager(Database::init($this->configuration)->getEntityManager());
64
65
    }
66
67
    public function setup() {
68
69
        $this->installWorkers();
70
71
        $this->pushQueueCommands();
72
        $this->pushScheduleCommands();
73
74
    }
75
76
    protected function installWorkers() {
77
78
        // add workers
79
        $manager = $this->getWorkers();
80
81
        $schedule_worker = new ScheduleWorker("scheduler");
82
        $schedule_worker
83
            ->setConfiguration($this->getConfiguration())
84
            ->setLogger($this->getLogger())
85
            ->setEvents($this->getEvents())
86
            ->setTasksTable($this->getTasksTable())
87
            ->setEntityManager($this->getEntityManager());
88
89
        $queue_worker = new QueueWorker("queue");
90
        $queue_worker
91
            ->setConfiguration($this->getConfiguration())
92
            ->setLogger($this->getLogger())
93
            ->setEvents($this->getEvents())
94
            ->setTasksTable($this->getTasksTable())
95
            ->setEntityManager($this->getEntityManager());
96
97
        $manager
98
            ->install($schedule_worker, 1, true)
99
            ->install($queue_worker, 1, true);
100
101
    }
102
103
    protected function pushQueueCommands() {
104
105
        $this->getSocket()->getCommands()
106 View Code Duplication
            ->add('queue:add', function(Request $request, $daemon) {
0 ignored issues
show
Unused Code introduced by
The parameter $daemon is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
                $manager = new QueueManager(
108
                    $this->getConfiguration(),
109
                    $this->getLogger(),
110
                    $this->getEvents(),
111
                    $this->getEntityManager()
112
                );
113
114
                return $manager->add($name, $request);
0 ignored issues
show
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
115
            })
116 View Code Duplication
            ->add('queue:addBulk', function(array $requests, $daemon) {
0 ignored issues
show
Unused Code introduced by
The parameter $daemon is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
118
                $manager = new QueueManager(
119
                    $this->getConfiguration(),
120
                    $this->getLogger(),
121
                    $this->getEvents(),
122
                    $this->getEntityManager()
123
                );
124
125
                return $manager->addBulk($requests);
126
127
            });
128
129
    }
130
131
    protected function pushScheduleCommands() {
132
133
        $this->getSocket()->getCommands()->add('scheduler:refresh', function($data, $daemon) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $daemon is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
134
135
            return $this->getWorkers()->get("scheduler")->getOutputChannel()->send('refresh');
136
137
        });
138
139
    }
140
141
}
142