PHP code example of danilopolani / filament-memory-tracker

1. Go to this page and download the library: Download danilopolani/filament-memory-tracker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

danilopolani / filament-memory-tracker example snippets




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;
    }
}



return [
    // ...

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

];



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);
    }

    // ...
}



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();
        });
    }
}
bash
php artisan vendor:publish --tag=filament-memory-tracker-assets
php artisan vendor:publish --tag=filament-memory-tracker-config
bash
php artisan vendor:publish --tag=filament-memory-tracker-assets --force