bnomei / kirby3-janitor

Kirby Plugin for running commands like cleaning the cache from within the Panel, PHP code, CLI or a cronjob
https://forum.getkirby.com/t/kirby3-janitor-plugin/23573
MIT License
89 stars 8 forks source link

Janitor custom command triggered as webhook isn’t working #124

Closed timotheegoguely closed 5 months ago

timotheegoguely commented 7 months ago

Hi,

I'm using you plugin on a website I'm working, and so far, everything was working like a charm – congratulations for all your work on this one.

So, my Janitor panel buttons defined in my home.yml blueprint works perfectly:

fields:
  flush_home:
    type: janitor
    command: 'janitor:flush home --quiet'
    icon: refresh
  select_random_featured_cards:
    type: janitor
    command: 'select-random-featured-cards'
    icon: images

Here is my custom command file site/commands/select-random-featured-cards.php:

<?php

use Bnomei\Janitor;
use Kirby\CLI\CLI;

return [
    'description' => 'Select random featured cards',
    'args' => [] + Janitor::ARGS, // page, file, user, site, data, model
    'command' => static function (CLI $cli): void {

        $home = page('home');

        // Select 7 random featured cards
        $newfeatured = page('photography')
            ->children()
            ->listed()
            ->shuffle()
            ->limit(7)
            ->toArray();

        // Update featured field
        $home = $home->update([
            'featured' => $newfeatured
        ]);
    }
];

I have the following routes defined in my config.php:

  'bnomei.janitor.secret' => 'xxxxxxxxxxxxxxxxx',
  'routes' => [
    [
      'pattern' => 'webhook/(:any)/(:any)',
      'action' => function($secret, $command) {
        if ($secret != janitor()->option('secret')) {
          \Kirby\Http\Header::status(401);
          die();
        }
        if ($command === 'janitor-flush-home') {
          janitor()->command('janitor:flush home --quiet');
        }
        elseif ($command === 'select-random-featured-cards') {
          janitor()->command('select-random-featured-cards');
        }
      }
    ]

When I trigger the janitor-flush-home command via the url https://mywebsite.com/webhook/xxxxxxxxxxxxxxxxx/janitor-flush-home, it works (the home page cache is flushed), but my second command https://mywebsite.com/webhook/xxxxxxxxxxxxxxxxx/select-random-featured-cards isn't working, and I don't understand why.

Am I missing something? Should I use janitor:job or janitor:call? 🤔 Thanks for your help!

bnomei commented 5 months ago

sorry about the late reply. as discussed in the forum the dashes in the filename disrupt janitor. i will fix this asap.

https://forum.getkirby.com/t/janitor-custom-command-triggered-as-webhook-isn-t-working/31655/4?u=bnomei

bnomei commented 5 months ago

its actually something else. since you created your own route and call the command manually you will need to add the --quiet.

janitor()->command('select-random-featured-cards --quiet');

my suggestion would be instead of creating a custom webhook to use the one from the plugin. it has the check for the secret and appends the quiet option automatically.

plugin-janitor/xxxxxxxxxxxxxxxxx/select-random-featured-cards