Skip to content

Commit

Permalink
Moved from Pusher to Reverb
Browse files Browse the repository at this point in the history
  • Loading branch information
VampireAotD committed Jul 27, 2024
1 parent f67329e commit a210a82
Show file tree
Hide file tree
Showing 23 changed files with 1,056 additions and 67 deletions.
15 changes: 13 additions & 2 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ services:
- "host.docker.internal:host-gateway" # for xdebug
healthcheck:
test: php artisan octane:status || exit 1
start_period: 5s
interval: 10s
timeout: 5s
retries: 5
Expand Down Expand Up @@ -148,6 +147,18 @@ services:
networks:
- local

reverb:
depends_on:
app:
condition: service_healthy
build: *app
container_name: anilibrary-reverb
volumes:
- ./src:/anilibrary
- ./docker/php/supervisor/conf.d/reverb.conf:/etc/supervisor/conf.d/reverb.conf
networks:
- local

networks:
anilibrary:
name: anilibrary
Expand All @@ -158,4 +169,4 @@ volumes:
db-data:
name: anilibrary-db-data
redis-data:
name: anilibrary-redis-data
name: anilibrary-redis-data
24 changes: 24 additions & 0 deletions docker/nginx/conf.d/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ server {
try_files $uri $uri/ @octane;
}

location /app {
try_files $uri $uri/ @reverb;
}

location /apps {
try_files $uri $uri/ @reverb;
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

Expand Down Expand Up @@ -54,4 +62,20 @@ server {

proxy_pass http://anilibrary:8000$suffix;
}

location @reverb {
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_cache_bypass $http_upgrade;

proxy_read_timeout 60s;
proxy_connect_timeout 60s;
proxy_pass http://anilibrary-reverb:8080;
}
}
5 changes: 3 additions & 2 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ARG USER_ID=1000
RUN apk update \
&& apk add --no-cache libstdc++ libpq libzip-dev gmp-dev oniguruma-dev curl git zip unzip supervisor \
&& apk add --no-cache --virtual .build-deps $PHPIZE_DEPS linux-headers brotli-dev pcre-dev pcre2-dev zlib-dev \
&& pecl install xdebug swoole \
&& pecl install xdebug swoole uv \
&& docker-php-ext-install -j$(nproc) \
pcntl \
mbstring \
Expand Down Expand Up @@ -53,7 +53,8 @@ COPY --from=node /usr/local/bin /usr/local/bin

WORKDIR /anilibrary

EXPOSE 8000
EXPOSE 5173
EXPOSE 8000
EXPOSE 8080

ENTRYPOINT ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]
9 changes: 9 additions & 0 deletions docker/php/supervisor/conf.d/reverb.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[program:reverb]
process_name=%(program_name)s_%(process_num)02d
command=php artisan reverb:start --debug
user=anilibrary
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
1 change: 1 addition & 0 deletions docker/php/supervisor/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ file=/tmp/supervisor.sock
[supervisord]
nodaemon=true
user=anilibrary
minfds=10000
logfile=/tmp/supervisord.log
pidfile=/tmp/supervisord.pid

Expand Down
26 changes: 12 additions & 14 deletions src/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null

BROADCAST_CONNECTION=log
BROADCAST_CONNECTION=reverb
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
QUEUE_CONNECTION=redis

CACHE_STORE=file
CACHE_PREFIX=
Expand All @@ -66,20 +66,18 @@ AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1
REVERB_APP_ID=
REVERB_APP_KEY=
REVERB_APP_SECRET=
REVERB_HOST=localhost
REVERB_PORT=80
REVERB_SCHEME=http

VITE_APP_NAME="${APP_NAME}"
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
VITE_REVERB_HOST="${REVERB_HOST}"
VITE_REVERB_PORT="${REVERB_PORT}"
VITE_REVERB_SCHEME="${REVERB_SCHEME}"

TELEGRAM_BOT_TOKEN=
TELEGRAM_BOT_NAME=
Expand Down
22 changes: 10 additions & 12 deletions src/.env.testing
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,18 @@ AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1
REVERB_APP_ID=1
REVERB_APP_KEY=test
REVERB_APP_SECRET=test
REVERB_HOST=testing.com
REVERB_PORT=80
REVERB_SCHEME=http

VITE_APP_NAME="${APP_NAME}"
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
VITE_REVERB_HOST="${REVERB_HOST}"
VITE_REVERB_PORT="${REVERB_PORT}"
VITE_REVERB_SCHEME="${REVERB_SCHEME}"

TELEGRAM_TOKEN=test
TELEGRAM_BOT_NAME=anilibrary_test_bot
Expand Down
26 changes: 26 additions & 0 deletions src/app/Broadcasting/Scraper/ScrapeAnimeChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Broadcasting\Scraper;

use App\Models\User;

final class ScrapeAnimeChannel
{
/**
* Create a new channel instance.
*/
public function __construct()
{
//
}

/**
* Authenticate the user's access to the channel.
*/
public function join(User $user, string $requestedId): array | bool
{
return $user->id === $requestedId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace App\DTO\Events\Pusher;
namespace App\DTO\Events\Scraper;

use App\Enums\Events\Pusher\ScrapeResultTypeEnum;
use App\Enums\Events\Scraper\ScrapeResultTypeEnum;

final readonly class ScrapeResultDTO
final readonly class ScrapeAnimeResultDTO
{
public function __construct(
public string $userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Enums\Events\Pusher;
namespace App\Enums\Events\Scraper;

enum ScrapeResultTypeEnum: string
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/Enums/QueueEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ enum QueueEnum: string
use ProvideValues;

case MAIL_QUEUE = 'mail';
case PUSHER_QUEUE = 'pusher';
case SOCKET_QUEUE = 'socket';
case SCRAPER_QUEUE = 'scraper';
case TELEGRAM_QUEUE = 'telegram';
case ELASTICSEARCH_QUEUE = 'elasticsearch';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace App\Events\Pusher;
namespace App\Events\Scraper;

use App\DTO\Events\Pusher\ScrapeResultDTO;
use App\DTO\Events\Scraper\ScrapeAnimeResultDTO;
use App\Enums\QueueEnum;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
Expand All @@ -13,7 +13,7 @@
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

final class ScrapeResultEvent implements ShouldBroadcast
final class ScrapeAnimeResultEvent implements ShouldBroadcast
{
use Dispatchable;
use InteractsWithSockets;
Expand All @@ -24,7 +24,7 @@ final class ScrapeResultEvent implements ShouldBroadcast
/**
* Create a new event instance.
*/
public function __construct(public readonly ScrapeResultDTO $dto)
public function __construct(public readonly ScrapeAnimeResultDTO $dto)
{
//
}
Expand All @@ -34,15 +34,15 @@ public function __construct(public readonly ScrapeResultDTO $dto)
*/
public function broadcastQueue(): string
{
return QueueEnum::PUSHER_QUEUE->value;
return QueueEnum::SOCKET_QUEUE->value;
}

/**
* The event's broadcast name.
*/
public function broadcastAs(): string
{
return 'scrape.result';
return 'scrape.anime.result';
}

/**
Expand All @@ -63,6 +63,6 @@ public function broadcastWith(): array
*/
public function broadcastOn(): Channel
{
return new PrivateChannel('scraper.' . $this->dto->userId);
return new PrivateChannel('scrape.anime.' . $this->dto->userId);
}
}
14 changes: 7 additions & 7 deletions src/app/Jobs/Scraper/ScrapeAnimeJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace App\Jobs\Scraper;

use App\DTO\Events\Pusher\ScrapeResultDTO;
use App\Enums\Events\Pusher\ScrapeResultTypeEnum;
use App\DTO\Events\Scraper\ScrapeAnimeResultDTO;
use App\Enums\Events\Scraper\ScrapeResultTypeEnum;
use App\Enums\QueueEnum;
use App\Events\Pusher\ScrapeResultEvent;
use App\Events\Scraper\ScrapeAnimeResultEvent;
use App\UseCase\Scraper\ScraperUseCase;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down Expand Up @@ -40,16 +40,16 @@ public function handle(ScraperUseCase $scraperUseCase): void
{
try {
$anime = $scraperUseCase->scrapeByUrl($this->url);
ScrapeResultEvent::broadcast(
new ScrapeResultDTO(
ScrapeAnimeResultEvent::broadcast(
new ScrapeAnimeResultDTO(
$this->userId,
ScrapeResultTypeEnum::SUCCESS,
$anime->id
)
);
} catch (RequestException | ValidationException | Throwable $e) {
ScrapeResultEvent::broadcast(
new ScrapeResultDTO(
ScrapeAnimeResultEvent::broadcast(
new ScrapeAnimeResultDTO(
$this->userId,
ScrapeResultTypeEnum::ERROR,
$e->getMessage()
Expand Down
1 change: 1 addition & 0 deletions src/bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
->withRouting(
web : __DIR__ . '/../routes/web.php',
commands: __DIR__ . '/../routes/console.php',
channels: __DIR__ . '/../routes/channels.php',
health : '/up',
)
->withMiddleware(function (Middleware $middleware) {
Expand Down
1 change: 1 addition & 0 deletions src/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"laravel/framework": "v11.16.0",
"laravel/horizon": "^5.25.0",
"laravel/octane": "^2.5.2",
"laravel/reverb": "^1.0",
"laravel/sanctum": "^4.0.2",
"laravel/tinker": "^2.9.0",
"nutgram/laravel": "^1.4.1",
Expand Down
Loading

0 comments on commit a210a82

Please sign in to comment.