Skip to content

Commit

Permalink
Merge branch 'master' of github.com:danilopolani/filament-memory-tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
danilopolani committed Jul 23, 2021
2 parents 62f8dd2 + 937a5e7 commit 3c71076
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ Track the memory usage of your workers and display them in Filament.

Install the package via composer:

```basgh
```bash
composer require danilopolani/filament-memory-tracker
```

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

```shell
```bash
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:

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

Expand All @@ -41,7 +41,11 @@ Key | Type | Description

## Usage

In your Worker create a new `MemoryTracker` instance and then ping the `track()` method every time you want. An example with [ReactPHP Event Loop](https://reactphp.org/event-loop/):
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](https://reactphp.org/event-loop/).


ℹ️ | The `$realUsage` flag is the same as [memory_get_usage()](https://www.php.net/manual/en/function.memory-get-usage.php).
:---: | :---

```php
<?php
Expand Down Expand Up @@ -85,7 +89,8 @@ class MyWorker extends Command
*/
public function handle()
{
Loop::addPeriodicTimer(5, function () {
// Ping every 5minutes
Loop::addPeriodicTimer(60 * 5, function () {
$this->memoryTracker->track(bool $realUsage = false);
});

Expand All @@ -109,16 +114,15 @@ return [
];
```

> **Note**: the `$realUsage` flag is the same as [memory_get_usage()](https://www.php.net/manual/en/function.memory-get-usage.php)
### 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.

> **Note**: The `TracksRestart` requires the extension **`pcntl`** to be enabled.
⚠️ | The `TracksRestart` requires the extension **`pcntl`** to be enabled.
:---: | :---

```php
<?php
Expand Down Expand Up @@ -150,7 +154,7 @@ class MyWorker extends Command

### Laravel Queue

You can track [Laravel Queue](laravel.com/docs/8.x/queues) too by listening some specific events in a provider, for example your `AppServiceProvider`.
You can track [Laravel Queue](laravel.com/docs/8.x/queues) too by listening to some specific events in a provider, for example your `AppServiceProvider`.

```php
<?php
Expand Down Expand Up @@ -204,8 +208,8 @@ 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.
`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.
Expand Down

0 comments on commit 3c71076

Please sign in to comment.