Skip to content

Commit

Permalink
Change custom monitor data to json instead of serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
romanzipp committed Mar 12, 2018
1 parent 104df8d commit c02217d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
38 changes: 14 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/romanzipp/Laravel-Queue-Monitor/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/romanzipp/Laravel-Queue-Monitor/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/romanzipp/Laravel-Queue-Monitor/badges/build.png?b=master)](https://scrutinizer-ci.com/g/romanzipp/Laravel-Queue-Monitor/build-status/master)

This package offers monitoring like "Laravel Horizon" for database queue.
This package offers monitoring like [Laravel Horizon](https://laravel.com/docs/horizon) for database queue.

## Enhancements
## Features

* Monitor all jobs like [Laravel Horizon](https://laravel.com/docs/horizon), but not only for redis
* Handles failed jobs with exception
* Support for milliseconds
* Model for Queue Monitorings

Expand Down Expand Up @@ -51,7 +53,7 @@ $ php artisan migrate

## Usage

The package automaticly logs all dispatched jobs.
The package automaticly logs **all dispatched jobs**.

### Exclude job from beding monitored

Expand All @@ -71,21 +73,11 @@ class ExampleJob implements ShouldQueue
use SerializesModels;
use DontMonitor; // <---

/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
Expand Down Expand Up @@ -113,21 +105,11 @@ class ExampleJob implements ShouldQueue
use SerializesModels;
use QueueMonitor; // <---

/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// Save progress, if job driver supports
Expand All @@ -136,7 +118,7 @@ class ExampleJob implements ShouldQueue
$this->queueProgress($percentage);
});

// Save data if finished
// Save data if finished. Must be type of array
$this->queueData(['foo' => 'bar']);
}
}
Expand Down Expand Up @@ -170,6 +152,14 @@ Monitor::today();

// Chain Scopes
Monitor::today()->failed();

// Get parsed custom Monitor data

$monitor = Monitor::find(1);

$monitor->data; // Raw data string

$monitor->parsed_data; // JSON decoded data, always array
```

## ToDo
Expand Down
12 changes: 11 additions & 1 deletion src/Models/Monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,21 @@ public function finishedAtExact(): Carbon
return Carbon::parse($this->finished_at_exact);
}

public function basename()
public function getBasenameAttribute()
{
return Arr::last(explode('\\', $this->name));
}

public function basename()
{
return $this->basename;
}

public function getParsedDataAttribute(): array
{
return json_decode($this->data, true) ?? [];
}

/**
* Determine wether job is finished
* @return boolean
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/QueueMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public function queueProgress(int $progress)

/**
* Set Monitor data
* @param mixed $data Custom data
* @param array $data Custom data
* @return void
*/
public function queueData($data)
public function queueData(array $data)
{
$monitor = $this->getQueueMonitor();

Expand All @@ -43,7 +43,7 @@ public function queueData($data)
}

$monitor->update([
'data' => serialize($data),
'data' => json_encode($data),
]);
}

Expand Down

0 comments on commit c02217d

Please sign in to comment.