Skip to content

Commit

Permalink
Added schedule record for telescope (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
guandeng authored Dec 14, 2024
1 parent ec2bb27 commit 4ff93d5
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ An elegant debug assistant for the hyperf framework.
- [x] guzzle
- [x] cache
- [x] rpc server/client
- [x] schedule

## Installation

Expand Down
1 change: 1 addition & 0 deletions publish/telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'guzzle' => env('TELESCOPE_ENABLE_GUZZLE', false),
'cache' => env('TELESCOPE_ENABLE_CACHE', false),
'rpc' => env('TELESCOPE_ENABLE_RPC', false),
'schedule' => env('TELESCOPE_ENABLE_SCHEDULE', true),
],
'recording' => true,
'timezone' => env('TELESCOPE_TIMEZONE', 'Asia/Shanghai'),
Expand Down
6 changes: 6 additions & 0 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@
<span>Commands</span>
</router-link>
</li>
<li class="nav-item">
<router-link active-class="active" to="/schedule" class="nav-link d-flex align-items-center">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm.75-13a.75.75 0 00-1.5 0v5c0 .414.336.75.75.75h4a.75.75 0 000-1.5h-3.25V5z" clip-rule="evenodd"></path></svg>
<span>Schedule</span>
</router-link>
</li>
<li class="nav-item">
<router-link active-class="active" to="/client-request" class="nav-link d-flex align-items-center">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-1.5 0a6.5 6.5 0 11-11-4.69v.447a3.5 3.5 0 001.025 2.475L8.293 10 8 10.293a1 1 0 000 1.414l1.06 1.06a1.5 1.5 0 01.44 1.061v.363a1 1 0 00.553.894l.276.139a1 1 0 001.342-.448l1.454-2.908a1.5 1.5 0 00-.281-1.731l-.772-.772a1 1 0 00-1.023-.242l-.384.128a.5.5 0 01-.606-.25l-.296-.592a.481.481 0 01.646-.646l.262.131a1 1 0 00.447.106h.188a1 1 0 00.949-1.316l-.068-.204a.5.5 0 01.149-.538l1.44-1.234A6.492 6.492 0 0116.5 10z" clip-rule="evenodd"></path></svg>
Expand Down
1 change: 1 addition & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __invoke(): array
Contract\PrunableRepository::class => fn ($container) => $container->get(Contract\EntriesRepository::class),
],
'listeners' => [
Listener\CronEventListener::class,
Listener\CommandListener::class,
Listener\DbQueryListener::class,
Listener\ExceptionHandlerListener::class,
Expand Down
27 changes: 27 additions & 0 deletions src/Controller/ScheduleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Telescope\Controller;

use FriendsOfHyperf\Telescope\EntryType;

class ScheduleController extends EntryController
{
/**
* The entry type for the controller.
*
* @return string
*/
protected function entryType()
{
return EntryType::SCHEDULE;
}
}
2 changes: 2 additions & 0 deletions src/EntryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class EntryType

public const COMMAND = 'command';

public const SCHEDULE = 'schedule';

public const DUMP = 'dump';

public const EVENT = 'event';
Expand Down
62 changes: 62 additions & 0 deletions src/Listener/CronEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Telescope\Listener;

use FriendsOfHyperf\Telescope\IncomingEntry;
use FriendsOfHyperf\Telescope\Telescope;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use FriendsOfHyperf\Telescope\TelescopeContext;
use Hyperf\Crontab\Event;
use Hyperf\Event\Contract\ListenerInterface;

class CronEventListener implements ListenerInterface
{
public function __construct(
private TelescopeConfig $telescopeConfig
) {
}

public function listen(): array
{
return [
Event\AfterExecute::class,
Event\FailToExecute::class,
];
}

/**
* @param Event\AfterExecute|Event\FailToExecute|object $event
*/
public function process(object $event): void
{
if (! $this->telescopeConfig->isEnable('schedule')) {
return;
}

TelescopeContext::getOrSetBatch();

$output = match (true) {
$event instanceof Event\AfterExecute => 'success',
$event instanceof Event\FailToExecute => '[fail]' . (string) $event->getThrowable(),
default => '',
};

Telescope::recordSchedule(IncomingEntry::make([
'command' => $event->crontab->getName(),
'description' => $event->crontab->getMemo(),
'expression' => $event->crontab->getRule(),
'timezone' => $event->crontab->getTimezone(),
'user' => '',
'output' => $output,
]));
}
}
3 changes: 3 additions & 0 deletions src/Listener/RegisterRoutesListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function process(object $event): void
Router::post('/commands', [Controller\CommandsController::class, 'index']);
Router::get('/commands/{id}', [Controller\CommandsController::class, 'show']);

Router::post('/schedule', [Controller\ScheduleController::class, 'index']);
Router::get('/schedule/{id}', [Controller\ScheduleController::class, 'show']);

Router::delete('/entries', [Controller\EntriesController::class, 'destroy']);

Router::post('/events', [Controller\EventsController::class, 'index']);
Expand Down
5 changes: 5 additions & 0 deletions src/Telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public static function recordEvent(IncomingEntry $entry): void
static::record(EntryType::EVENT, $entry);
}

public static function recordSchedule(IncomingEntry $entry): void
{
static::record(EntryType::SCHEDULE, $entry);
}

public static function recordException(IncomingEntry $entry): void
{
static::record(EntryType::EXCEPTION, $entry);
Expand Down

0 comments on commit 4ff93d5

Please sign in to comment.