schedule task log to docker process when running in docker environment

This commit is contained in:
xiaomlove
2025-05-19 13:18:47 +07:00
parent 273fa3e1b8
commit 3dfe0cbd27
5 changed files with 44 additions and 17 deletions

View File

@@ -6,6 +6,8 @@ use App\Http\Middleware\Locale;
use Carbon\Carbon;
use Filament\Support\Assets\Css;
use Filament\Support\Facades\FilamentAsset;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Console\Events\ScheduledTaskStarting;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;
@@ -44,6 +46,7 @@ class AppServiceProvider extends ServiceProvider
if (env('APP_ENV') == "production" && in_array($forceScheme, ['https', 'http'])) {
URL::forceScheme($forceScheme);
}
$this->customScheduleTask();
Filament::serving(function () {
Filament::registerNavigationGroups([
@@ -64,4 +67,26 @@ class AppServiceProvider extends ServiceProvider
do_action('nexus_boot');
}
private function customScheduleTask(): void
{
if (!isRunningInConsole()) {
return;
}
/** @var Dispatcher $eventDispatcher */
$eventDispatcher = $this->app->make(Dispatcher::class);
$eventDispatcher->listen(
events: [ScheduledTaskStarting::class],
listener: static function (ScheduledTaskStarting $event): void {
$event->task->onOneServer()->withoutOverlapping();
// When we are using stterr as output for logs then schedule tasks will not output
// any logs due the /dev/null usage. Let's fix this by appending the output to
// the docker process.
if (getenv("RUNNING_IN_DOCKER") == "1" && $event->task->output === $event->task->getDefaultOutput()) {
$event->task->appendOutputTo("/proc/1/fd/1");
}
}
);
}
}