Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Add octane support #365

Draft
wants to merge 15 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .docker/common/crontab
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* * * * * cd /var/www && php artisan schedule:run >> /dev/null 2>&1
57 changes: 0 additions & 57 deletions .docker/common/wait-for-it.sh

This file was deleted.

63 changes: 47 additions & 16 deletions .docker/develop/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
FROM php:8-fpm-alpine
# Here we are going to install PHP extension requirements that did not come
# with the default PHP container. This will be necessary for Laravel and Swoole.
FROM php:8.0-cli-alpine3.13 as php-base

WORKDIR /var/www

RUN apk update && apk add --no-cache \
zip \
Expand All @@ -10,29 +14,56 @@ RUN apk update && apk add --no-cache \
bash \
tzdata \
nano \
nodejs npm \
&& pecl install redis \
&& docker-php-ext-enable redis \
nodejs npm

RUN pecl install redis

RUN docker-php-ext-enable redis \
&& docker-php-ext-install zip pdo_mysql exif opcache pcntl \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --quit \
&& npm --global install yarn \
&& rm -rf /var/cache/apk/* /tmp/* /var/tmp/*

ENV TZ=Asia/Baku
RUN apk add --no-cache --virtual php_dependencies $PHPIZE_DEPS && \
apk add --no-cache libstdc++ && \
docker-php-ext-install bcmath ctype pdo_mysql pcntl && \
pecl install swoole && \
docker-php-ext-enable swoole && \
apk del php_dependencies && \
chown -R www-data:www-data /var/www

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# This stage is so that we can build up everything that doesn't require Laravel
# so as to not bust the caching for those items.
FROM php-base as octane-base
# This really shouldn't be modified, so we aren't advertising the env variable.
# It allows us to globally install chokidar without modifying the Laravel package.json.
ENV NODE_PATH "/home/www-data/.npm-global/lib/node_modules"

RUN mv $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini
RUN apk add --no-cache nodejs npm && \
mkdir "/home/www-data/.npm-global/" && \
npm config set prefix "/home/www-data/.npm-global/" && \
npm install -g chokidar

#COPY ./.docker/develop/php/local.ini $PHP_INI_DIR/conf.d
#COPY ./.docker/develop/php/xlaravel.pool.conf /usr/local/etc/php-fpm.d/
# This is our final container. We will copy over our built version of Laravel.
FROM octane-base

WORKDIR /var/www
USER www-data

# Allow the user to specify Swoole options via ENV variables.
ENV SWOOLE_MAX_REQUESTS "500"
ENV SWOOLE_TASK_WORKERS "auto"
ENV SWOOLE_WATCH $true
ENV SWOOLE_WORKERS "auto"

# Expose the ports that Octane is using.
EXPOSE 8000

ARG PUID=1000
ENV PUID ${PUID}
ARG PGID=1000
ENV PGID ${PGID}
# Run Swoole
CMD if [[ -z $SWOOLE_WATCH ]] ; then \
php artisan octane:start --server="swoole" --host="0.0.0.0" --workers=${SWOOLE_WORKERS} --task-workers=${SWOOLE_TASK_WORKERS} --max-requests=${SWOOLE_MAX_REQUESTS} ; \
else \
php artisan octane:start --server="swoole" --host="0.0.0.0" --workers=${SWOOLE_WORKERS} --task-workers=${SWOOLE_TASK_WORKERS} --max-requests=${SWOOLE_MAX_REQUESTS} --watch ; \
fi

RUN groupmod -o -g ${PGID} www-data && \
usermod -o -u ${PUID} -g www-data www-data
# Check the health status using the Octane status command.
HEALTHCHECK CMD php artisan octane:status --server="swoole"
9 changes: 9 additions & 0 deletions .docker/develop/php/supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[supervisord]
nodaemon=true

[program:octane]
command=php -d variables_order=EGPCS /var/www/artisan octane:start --server=roadrunner --host=0.0.0.0 --port=8080
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.github
.deploy
readme.md
_ide_helper.php
.php_cs
.php_cs.cache
.php-cs-fixer.cache
5 changes: 4 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;

class Kernel extends ConsoleKernel
{
Expand All @@ -25,7 +26,9 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('cache:clear')->everyMinute();
$schedule->call(function () {
DB::table('users')->update(['karma' => 0]);
})->lastDayOfMonth('00:01');
}

/**
Expand Down
32 changes: 25 additions & 7 deletions app/Http/Controllers/Api/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,38 @@ public function store(ArticleRequest $request): JsonResponse
public function vote(VoteRequest $request): JsonResponse
{
$article = Article::findOrFail($request->id);
($request->type === 'up') ? self::upOrDownVote($request->user(), $article) : self::upOrDownVote($request->user(), $article, 'down');
($request->type === 'up')
? self::upOrDownVote($request->user(), $article)
: self::upOrDownVote(
$request->user(),
$article,
'down'
);

return response()->json(['result' => 'true']);
}

public static function upOrDownVote(User $user, $target, string $type = 'up'): bool
{
$hasVoted = $user->{'has'.ucfirst($type).'Voted'}($target);
$hasVoted = $user->{'has' . ucfirst($type) . 'Voted'}($target);
DB::beginTransaction();

try {
$user->{$type.'Vote'}($target);
$user->{$type . 'Vote'}($target);
if ($hasVoted) {
$user->cancelVote($target);
foreach ($target->hubs as $hub) {
$type === 'up' ? $hub->rating-- : $hub->rating++;
$hub->save();
}
$type === 'up' ? $target->creator->rating-- : $target->creator->rating++;
$target->author->save();
if ($type === 'up') {
$target->creator->rating--;
$target->creator->karma--;
} else {
$target->creator->rating++;
$target->creator->karma++;
}
$target->creator->save();
DB::commit();

return false;
Expand All @@ -92,8 +104,14 @@ public static function upOrDownVote(User $user, $target, string $type = 'up'): b
$type === 'up' ? $hub->rating++ : $hub->rating--;
$hub->save();
}
$type === 'up' ? $target->creator->rating++ : $target->creator->rating--;
$target->author->save();
if ($type === 'up') {
$target->creator->rating++;
$target->creator->karma++;
} else {
$target->creator->rating--;
$target->creator->karma--;
}
$target->creator->save();
DB::commit();
} catch (Exception $e) {
DB::rollback();
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"jackiedo/dotenv-editor": "^1.2",
"jcc/laravel-vote": "dev-master",
"laravel/framework": "^8.12",
"laravel/octane": "^1.0",
"laravel/passport": "^10.1",
"laravel/socialite": "^5.1",
"laravel/tinker": "^2.5",
Expand All @@ -30,7 +31,8 @@
"qcod/laravel-gamify": "^1.0",
"rennokki/laravel-eloquent-query-cache": "^2.6",
"sentry/sentry-laravel": "^2.3",
"spatie/laravel-medialibrary": "^9.0.0"
"spatie/laravel-medialibrary": "^9.0.0",
"spiral/roadrunner": "^2.2"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.5",
Expand Down
Loading