kirby3-janitor icon indicating copy to clipboard operation
kirby3-janitor copied to clipboard

Janitor custom command triggered as webhook isn’t working

Open timotheegoguely opened this issue 10 months ago • 0 comments

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!

timotheegoguely avatar Apr 11 '24 09:04 timotheegoguely