• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IlyasDeckers / ody-core / 13575371463

27 Feb 2025 08:34PM UTC coverage: 28.921% (+0.6%) from 28.348%
13575371463

push

github

IlyasDeckers
Refactoring/cleaning up/getting rid of redundant helper functions

0 of 68 new or added lines in 8 files covered. (0.0%)

9 existing lines in 5 files now uncovered.

544 of 1881 relevant lines covered (28.92%)

8.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Console/Commands/Websockets/StartCommand.php
1
<?php
2

3
namespace Ody\Core\Console\Commands\Websockets;
4

5
use Ody\Core\Console\Style;
6
use Ody\Core\Server\Dependencies;
7
use Ody\Core\Server\Http;
8
use Ody\Swoole\HotReload\Watcher;
9
use Ody\Swoole\ServerState;
10
use Swoole\Process;
11
use Symfony\Component\Console\Attribute\AsCommand;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Helper\Table;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ChoiceQuestion;
18

19
#[AsCommand(
20
    name: 'websockets:start',
×
21
    description: 'start http server'
×
22
)]
×
23
class StartCommand extends Command
24
{
25
    private ServerState $serverState;
26
    private Style $io;
27

28
    protected function configure(): void
×
29
    {
30
        $this->addOption(
×
31
            'daemonize',
×
32
            'd',
×
33
            InputOption::VALUE_NONE,
×
34
            'The program works in the background'
×
35
        )->addOption(
×
36
            'watch',
×
37
            'w',
×
38
            InputOption::VALUE_NONE,
×
39
            'If there is a change in the program code, it applies the changes instantly'
×
40
        );
×
41
    }
42

43
    /**
44
     * @throws \Exception
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output): int
×
47
    {
48
        /*
49
         * load Ody style
50
         */
51
        $this->io = new Style($input, $output);
×
52

53
        /*
54
         * Get a server state instance
55
         */
56
        $this->serverState = ServerState::getInstance();
×
57

58
        if (!Dependencies::check($this->io)) {
×
59
            return Command::FAILURE;
×
60
        }
61

NEW
62
        if ($this->serverState->websocketServerIsRunning()) {
×
NEW
63
            if (!$this->handleRunningServer($input, $output)) {
×
NEW
64
                return Command::FAILURE;
×
65
            }
66
        }
67

68
        $listenMessage = "listen on ws://" . config('websockets.host') . ':' . config('websockets.port');
×
69

70
        /*
71
         * send running server
72
         * send listen message
73
         */
74
        $this->io->success('Websocket server server running…');
×
75
        $this->io->info($listenMessage, true);
×
76

77
        /*
78
         * check if exist daemonize not send general information
79
         */
80
        if (!$input->getOption('daemonize')) {
×
81
            $serverSocketType = match (config('websockets.sock_type')) {
×
82
                SWOOLE_SOCK_TCP => 'TCP',
×
83
                SWOOLE_SOCK_UDP => 'UDP',
×
84
                default => 'other type'
×
85
            };
×
86
          
87
            /*
88
             * create general information table
89
             */
90
            $table = new Table($output);
×
91
            $table
×
92
                ->setHeaderTitle('general information')
×
93
                ->setHeaders([
×
94
                    '<fg=#FFCB8B;options=bold> PHP VERSION </>',
×
95
                    '<fg=#FFCB8B;options=bold> ODY VERSION </>',
×
96
                    '<fg=#FFCB8B;options=bold> WORKER COUNT </>',
×
97
                    '<fg=#FFCB8B;options=bold> SOCKET TYPE </>',
×
98
                    '<fg=#FFCB8B;options=bold> WATCH MODE </>'
×
99
                ])
×
100
                ->setRows([
×
101
                    [
×
102
                        '<options=bold> ' . PHP_VERSION . '</>',
×
103
                        '<options=bold> ' . ODY_VERSION . ' </>',
×
104
                        '<options=bold> ' . config('websockets.additional.worker_num') . '</>',
×
105
                        "<options=bold> $serverSocketType</>",
×
106
                        $input->getOption('watch') ? '<fg=#C3E88D;options=bold> ACTIVE </>' : "<fg=#FF5572;options=bold> DEACTIVE </>"
×
107
                    ],
×
108
                ]);
×
109
            $table->setHorizontal();
×
110
            $table->render();
×
111

112
            /*
113
             * send info message for stop server
114
             */
115
            $this->io->info('Press Ctrl+C to stop the server');
×
116

117
            /*
118
             * create watcher server
119
             */
120
            if ($input->getOption('watch')) {
×
121
                (new Process(function (Process $process) {
×
122
                    $this->serverState->setWatcherProcessId($process->pid);
×
123
                    (new Watcher())->start();
×
124
                }))->start();
×
125
            }
126
        }
127

128
        /*
129
         * create and start server
130
         */
131
        \Ody\Swoole\Websockets\Server::init()
×
132
            ->createServer()
×
133
            ->start($input->getOption('daemonize'));
×
134

135
        return Command::SUCCESS;
×
136
    }
137

NEW
138
    private function handleRunningServer(InputInterface $input, OutputInterface $output): bool
×
139
    {
140
        $this->io->error('failed to listen server port[' . config('websockets.host') . ':' . config('websockets.port') . '], Error: Address already', true);
×
141

142
        $helper = $this->getHelper('question');
×
143
        $question = new ChoiceQuestion(
×
144
            'Do you want the server to terminate? (defaults to no)',
×
145
            ['no', 'yes'],
×
146
            0
×
147
        );
×
148
        $question->setErrorMessage('Your selection is invalid.');
×
149

150
        $answer = $helper->ask($input, $output, $question);
×
151

152

153
        if ($answer != 'yes') {
×
NEW
154
            return false;
×
155
        }
156

157
        posix_kill($this->serverState->getMasterProcessId(), SIGTERM);
×
158
        posix_kill($this->serverState->getManagerProcessId(), SIGTERM);
×
159

160
        $watcherProcessId = $this->serverState->getWatcherProcessId();
×
161
        if (!is_null($watcherProcessId) && posix_kill($watcherProcessId, SIG_DFL)) {
×
162
            posix_kill($watcherProcessId, SIGTERM);
×
163
        }
164

165
        foreach ($this->serverState->getWorkerProcessIds() as $processId) {
×
166
            posix_kill($processId, SIGTERM);
×
167
        }
168

169
        sleep(1);
×
170

NEW
171
        return true;
×
172
    }
173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc