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

Overview

Filament Memory Tracker

Latest Stable Version Total Downloads

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

Filament Worker Memory widget preview

Installation

Install the package via composer:

composer require danilopolani/filament-memory-tracker

Then publish the config of the package and the assets as well:

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

Upgrade

When upgrading, you should republish the assets:

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

Configuration

There are a few notable configuration options for the package.

Key Type Description
cache_store String Define the cache store used to track memory usage. By default it will be your CACHE_DRIVER env value.
trackers Array A list of trackers names to be displayed in the dashboard. They must be the same used in your MemoryTracker() instance. See Usage below to discover more.
date_format String The DateTime format to display dates.

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.

ℹ️ The $realUsage flag is the same as memory_get_usage().


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:



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.
⚠️ The TracksRestart requires the extension pcntl to be enabled.


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.



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:

Key Description
track(): void Track the current memory usage for the worker.
trackRestart(bool $resetPeak = true): void Track a restart. If $resetPeak is true, the memory peak will be purged as well.
getHistory(): array Get the worker's history of memory usage.
getPeak(): array|null Get the worker's memory peak. Returns null if no peak found.
getLatestRestart(): array|null Get the worker's latest restart data. Returns null if no restart found.
purge(): void Purge all the data of the current worker.
purgeHistory(): void Purge the track history only of the current worker.
purgePeak(): void Purge the memory peak of the current worker.
purgeRestart(): void Purge the latest restart data of the current worker.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] 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.

You might also like...
Display movie posters on screens using web technologies.
Display movie posters on screens using web technologies.

The web application creates a digital movie poster display for use on LED screens. Intended to run on a Raspberry Pi 4, but will run on any web server with Apache/NGINX, PHP 7.3+, and MySQL/Postgres.

 Create and Control Page Redirects through Filament
Create and Control Page Redirects through Filament

Create and Control Page Redirects through Filament A Filament resource to create and maintain page redirects for your website. Installation You can in

Lookup IPs (IPv4 + IPv6), store them in MySQL and Redis + check if the IP is a VPN/Proxy

Lookup IPs (IPv4 + IPv6), store them in MySQL and Redis + check if the IP is a VPN/Proxy

This is a Task Manager system for managing your task. You can categorize your tasks and upload music to the project And a whole host of other features

taskManager Login and register Each user can have their own task Categorize tasks by creating folders Edit and Delete Folders Search for Tasks Show nu

Our team created for you one of the most innovative CRM systems that supports mainly business processes and allows for customization according to your needs. Be ahead of your competition and implement YetiForce!
Our team created for you one of the most innovative CRM systems that supports mainly business processes and allows for customization according to your needs. Be ahead of your competition and implement YetiForce!

We design an innovative CRM system that is dedicated for large and medium sized companies. We dedicate it to everyone who values open source software,

The simplest, most intuitive way to host your photos on your website.

Gallery 3.1+ (development version) About Gallery 3 is a web based software product that lets you manage your photos on your own website. You must have

Keep your forms alive, avoid `TokenMismatchException` by gently poking your Laravel app.

Poke Keep your forms alive, avoid TokenMismatchException by gently poking your Laravel app. Keep this package free Your support allows me to keep this

This plugin was created to help you migrate your existing gallery from your old site.
This plugin was created to help you migrate your existing gallery from your old site.

WP Migrate Gallery 1. Steps to Import Gallery 1.1. Install ACF PRO 1.2. Create CPT Gallery 1.3. Create Gallery Taxomy 1.4. Copy Gallery media files to

A great looking and easy-to-use photo-management-system you can run on your server, to manage and share photos.
A great looking and easy-to-use photo-management-system you can run on your server, to manage and share photos.

Lychee A great looking and easy-to-use photo-management-system. Since the 1st of April 2018 this project has moved to it's own Organisation (https://g

Releases(v2.1.0)
Owner
Theraloss
Senior Software Engineer & Tech Lead in love with Laravel and cats. Oh, almost forgot, I also work with Node, Vue, Angular, Ionic and much more.
Theraloss
DaybydayCRM an open-source CRM, to help you keep track of your daily workflow.

====================== DaybydayCRM is an everyday customer relationship management system (CRM) to help you keep track of your customers, tasks, appoi

Casper Bottelet 2.1k Jan 3, 2023
Learning Management System made in vanilla PHP to learn core concepts and usage of some basic utils

Learning Management System Learning Management System made in vanilla PHP to learn core concepts and usage of some basic utils. Report Bug · Request F

TitansLab 1 Mar 30, 2022
Kyle is a web application built with Laravel for web developers and small companies to efficiently track and stay on top of yearly expenses related to services

Kyle Kyle is a web application built with Laravel for web developers and small companies to efficiently track and stay on top of yearly expenses relat

Laravelista 36 Jul 15, 2022
WebDirStat is a disk usage utility for web servers written in PHP

WebDirStat is disk usage utility for web servers, it’s a single PHP file that gives you statistics about disk usage inside a specific Directory ordered by size, in a form of a simple tree table.

Yassine 2 Oct 14, 2021
Feature plugin to count CSS and JS assets loaded by the theme on front-end and display recommandations in Site Health

=== Site Health - Audit Enqueued Assets === Contributors: audrasjb, whodunitagency Tags: site health, performance audit, performance test, assets, scr

Jb Audras 3 Nov 5, 2021
Simple Laravel Invoice Generator Sling — open-source web application that helps you create invoices and track income.

Simple Laravel Invoice Generator Sling — open-source web application that helps you create invoices and track income. Table of Contents About

Ray Icemont 4 Nov 22, 2022
📇A contacts app for Nextcloud. Easily sync contacts from various devices with your Nextcloud and edit them online.

Nextcloud Contacts A contacts app for Nextcloud. Easily sync contacts from various devices with your Nextcloud and edit them online. This app only sup

Nextcloud 485 Dec 30, 2022
Log and display the product stock movements

Aropixel Sylius Stock Movement Plugin Log and display the product stock movements. When the stock of a product is updated, the stock movement will be

Aropixel 1 Oct 28, 2021
Crater is an open-source web & mobile app that helps you track expenses, payments & create professional invoices & estimates.

Introduction Crater is an open-source web & mobile app that helps you track expenses, payments & create professional invoices & estimates. Web Applica

Bytefury 6.7k Jan 1, 2023
Rafa Cake and Bakery is a web-based application project that aims to introduce Rafa Cake and Bakery, introduce what products are sold and can also order them via Whatsapp.

Rafa-cake-and-bakery Rafa Cake and Bakery is a web-based application project that aims to introduce Rafa Cake and Bakery, introduce what products are

Aan Evian Nanda 2 Jun 19, 2022