danilopolani/filament-memory-tracker

Track the memory usage of your workers and display them in Filament

v2.1.1 2023-02-15 16:38 UTC

This package is auto-updated.

Last update: 2024-10-15 20:18:30 UTC


README

Latest Stable Version Total Downloads

Track the memory usage of your workers and display them in Filament.

Filament Worker Memory widget preview

If you're using Filament v1, please navigate the 1.x branch.

Installation

Install the package via composer:

composer require danilopolani/filament-memory-tracker

Then publish the assets and the config of the package:

php artisan vendor:publish --tag=filament-memory-tracker-assets
php artisan vendor:publish --tag=filament-memory-tracker-config

If you're upgrading from v1 to v2 please note that the namespace changed from \DaniloPolani\ to \DaniloPolani\.

Upgrade

When upgrading be sure to republish the assets:

php artisan vendor:publish --tag=filament-memory-tracker-assets --force

Configuration

There are a few notable configuration options for the package.

Usage

In your Worker create a new MemoryTracker instance and then ping the track() method every time you want. There's an example with ReactPHP Event Loop.

<?php

namespace App\Console\Commands;

use DaniloPolani\FilamentMemoryTracker\MemoryTracker;
use Illuminate\Console\Command;
use React\EventLoop\Loop;

class MyWorker extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'worker:run';

    /**
     * The memory tracker instance.
     *
     * @var MemoryTracker
     */
    protected MemoryTracker $memoryTracker;

    /**
     * Class constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->memoryTracker = new MemoryTracker('Worker');
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // Ping every 5minutes
        Loop::addPeriodicTimer(60 * 5, function () {
            $this->memoryTracker->track(bool $realUsage = false);
        });

        return 0;
    }
}

Then don't forget to add your tracker name inside the configuration too:

<?php

return [
    // ...

    'trackers' => [
        'Worker',
    ],

];

Track restarts

You can track the latest Worker restart date and memory usage as well! If you're working on a custom Worker, you should intercept the exit signals and then call the $memoryTracker->trackRestart() method. Otherwise you can use the Trait provided by the package to achieve that:

  1. Include DaniloPolani\FilamentMemoryTracker\Concerns\TracksRestart inside your class;
  2. Call $this->trackRestartMemory(MemoryTracker $memoryTrackerInstance) inside your constructor.
<?php

namespace App\Console\Commands;

use DaniloPolani\FilamentMemoryTracker\MemoryTracker;
use DaniloPolani\FilamentMemoryTracker\Concerns\TracksRestart;
use Illuminate\Console\Command;
use React\EventLoop\Loop;

class MyWorker extends Command
{
    use TracksRestart;

    // ...

    public function __construct()
    {
        parent::__construct();

        $this->memoryTracker = new MemoryTracker('Worker');
        $this->trackRestartMemory($this->memoryTracker);
    }

    // ...
}

Laravel Queue

You can track Laravel Queue too by listening to some specific events in a provider, for example your AppServiceProvider.

<?php

namespace App\Providers;

use DaniloPolani\FilamentMemoryTracker\MemoryTracker;
use Filament\Filament;
use Illuminate\Queue\Events\Looping;
use Illuminate\Queue\Events\WorkerStopping;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $memoryTracker = new MemoryTracker('Queue');

        // Track memory usage
        Event::listen(Looping::class, function () use ($memoryTracker) {
            $memoryTracker->track();
        });

        // Track restarts
        Event::listen(WorkerStopping::class, function () use ($memoryTracker) {
            $memoryTracker->trackRestart();
        });
    }
}

Additional notes

  • The widget will refresh every 5s automatically;
  • By default the widget will be shown full-width if there's more than 1 tracker; otherwise, the widget will be a single block:

Memory Tracker widget single block

APIs

These are the available methods of the MemoryTracker class:

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email danilo.polani@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.