Janitor custom command triggered as webhook isn’t working

Hi,

I’m using @bnomei fantastic Janitor plugin on a K3 website I’m working on, 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? :thinking:
Thanks for your help!

my guess it that you are missing the impersonate call and updating is not allowed when using the command.

try adding $cli->out("ok"); to the end of your command and call it in the CLI first to verify it is working without janitor.

kirby select-random-featured-cards might yield “ok” in terminal then. but i guess it will report the missing permissions for updating the page. Add this before calling the update method…

kirby()->impersonate('kirby');

Thanks for your answer!

Indeed, adding kirby()->impersonate('kirby'); before calling the update method makes it work in the CLI, which is cool, but when I call the command via the webhook, it still have no effect…

wild guess… try renaming the command file to not have any dashes (-) in it

1 Like

Incredible as it may seem, that was the source the problem! :sweat_smile:
How did you guess?!

I think you should add a warning about this in the documentation.
Thanks for your help <3

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