Skip to content

Commit

Permalink
Added Laravel Horizon, test tasks, and jobs, along with minor improve…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
fabioassuncao committed Jan 10, 2024
1 parent c3d9283 commit e985f76
Show file tree
Hide file tree
Showing 26 changed files with 1,359 additions and 11 deletions.
8 changes: 6 additions & 2 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ RUN apk add --no-cache php83 \
php83-intl \
php83-exif \
php83-pcntl \
php83-sockets
php83-sockets \
php83-posix

RUN ln -s /usr/bin/php83 /usr/bin/php

Expand All @@ -50,5 +51,8 @@ COPY . /var/www/html
RUN composer install --no-dev
RUN chown -R nobody:nobody /var/www/html/storage

COPY ./.docker/entrypoint.sh /usr/local/bin/entrypoint
RUN chmod +x /usr/local/bin/entrypoint

EXPOSE 8000
ENTRYPOINT ["php", "-d", "variables_order=EGPCS", "artisan", "octane:start", "--server=frankenphp", "--host=0.0.0.0", "--port=8000"]
ENTRYPOINT ["/usr/local/bin/entrypoint"]
55 changes: 55 additions & 0 deletions .docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

set -e

# Run our defined exec if args empty
if [ -z "$1" ]; then

: ${CONTAINER_MODE:='manual'}
: ${CONTAINER_ROLE:='app'}
: ${APP_ENV:='production'}
artisan="php -d variables_order=EGPCS /var/www/html/artisan"

if [ "$CONTAINER_MODE" = "automatic" ]; then
echo "Preparing application..."
chown -R nobody:nobody /var/www/html/storage
php artisan storage:link || true
php artisan config:cache || true
php artisan migrate --force || true
fi

if [ "$CONTAINER_ROLE" = "app" ]; then

echo "INFO: Running octane..."
exec $artisan octane:start --server=frankenphp --host=0.0.0.0 --port=8000

elif [ "$CONTAINER_ROLE" = "worker" ]; then

echo "INFO: Running the queue..."
exec $artisan queue:work -vv --no-interaction --tries=3 --sleep=5 --timeout=300 --delay=10

elif [ "$CONTAINER_ROLE" = "horizon" ]; then

echo "INFO: Running the horizon..."
exec $artisan horizon

elif [ "$CONTAINER_ROLE" = "scheduler" ]; then

while true; do
if [ -d "/var/www/html/vendor" ] ; then
echo "INFO: Running scheduled tasks."
exec $artisan schedule:run --verbose --no-interaction &
else
echo "WARNING: Directory /var/www/html/vendor does not yet exist."
fi
sleep 60s
done

else
echo "Could not match the container role \"$CONTAINER_ROLE\""
exit 1
fi

else
exec "$@"
fi
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel_k8s
DB_USERNAME=root
DB_DATABASE=workshop_k8s
DB_USERNAME=workshop_k8s
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=redis
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
SESSION_LIFETIME=120

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ This project is a simple demo. I chose to use FrankenPHP with an app server, but

## Included Items

* PHP 8.3
* [PHP 8.3](https://www.php.net/releases/8.3/pt_BR.php)
* [Laravel](https://laravel.com/)
* [Laravel Octane](https://laravel.com/docs/10.x/octane)
* [Laravel horizon](https://laravel.com/docs/10.x/horizon)
* [Laravel Filament](https://filamentphp.com/)
* [FrankenPHP](https://frankenphp.dev/)
* [AKS](https://azure.microsoft.com/pt-br/products/kubernetes-service)
* [DOKS](https://www.digitalocean.com/products/kubernetes)

![Filament Demo](./screenshot.png)
![Filament Demo](./screenshot-1.png)

![Laravel Horizon](./screenshot-2.png)

## Upcoming Meetings

Expand Down
19 changes: 19 additions & 0 deletions app/Console/Commands/ProcessTextFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Console\Commands;

use App\Jobs\CreateTextFile;
use Illuminate\Console\Command;

class ProcessTextFile extends Command
{
protected $signature = 'process:textfile';

protected $description = 'Process text file by triggering a job';

public function handle()
{
dispatch(new CreateTextFile());
$this->info('Text file processing job pushed to the queue.');
}
}
20 changes: 20 additions & 0 deletions app/Console/Commands/TestTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class TestTask extends Command
{
protected $signature = 'test:task';

protected $description = 'Test task executed every minute';

public function handle()
{
$message = 'Hello, this is a test task running at ' . now();
Log::info($message);
$this->info('Task executed successfully.');
}
}
3 changes: 2 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
//
$schedule->command('test:task')->everyMinute();
$schedule->command('process:textfile')->everyMinute();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Pages/Auth/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function mount(): void
parent::mount();

$this->form->fill([
'email' => 'admin@filamentphp.com',
'email' => 'admin@admin.com',
'password' => 'password',
'remember' => true,
]);
Expand Down
35 changes: 35 additions & 0 deletions app/Jobs/CreateTextFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;

class CreateTextFile implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public function handle()
{
sleep(rand(5, 30));

$filePath = 'local/textfile.txt';
$content = 'Date and Time: ' . now() . "\n";

// Append existing content if file exists
if (Storage::exists($filePath)) {
$existingContent = Storage::get($filePath);
$content .= $existingContent;
}

// Write content to the file
Storage::put($filePath, $content);
}
}
36 changes: 36 additions & 0 deletions app/Providers/HorizonServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\HorizonApplicationServiceProvider;

class HorizonServiceProvider extends HorizonApplicationServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
parent::boot();

// Horizon::routeSmsNotificationsTo('15556667777');
// Horizon::routeMailNotificationsTo('[email protected]');
// Horizon::routeSlackNotificationsTo('slack-webhook-url', '#channel');
}

/**
* Register the Horizon gate.
*
* This gate determines who can access Horizon in non-local environments.
*/
protected function gate(): void
{
Gate::define('viewHorizon', function ($user) {
return in_array($user->email, [
//
]);
});
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"flowframe/laravel-trend": "^0.1.1",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/horizon": "^5.21",
"laravel/octane": "^2.2",
"laravel/sanctum": "^3.2",
"laravel/tinker": "^2.8",
Expand Down
80 changes: 79 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
App\Providers\AuthServiceProvider::class,
App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\HorizonServiceProvider::class,
App\Providers\Filament\AdminPanelProvider::class,
App\Providers\Filament\AppPanelProvider::class,
App\Providers\RouteServiceProvider::class,
Expand Down
Loading

0 comments on commit e985f76

Please sign in to comment.