Search code examples
silverstripesilverstripe-4

How can I run a BuildTask task via the command line in SilverStripe 4?


In the SilverStripe 4 documentation, it says I can extend the DevBuild class and call the task using cron in a unix commandline.

https://docs.silverstripe.org/en/4/developer_guides/cli/#running-regular-tasks-with-cron

I've written a class called MyTask at app/src/Tasks/MyTask.php, like below:

use SilverStripe\Dev\BuildTask;

class MyTask extends BuildTask
{
    private static $segment = 'MyTask';

    protected $title = 'My Task';
    protected $description = 'A task that I want to run via cron job';
    protected $enabled = true;

    public function run($request){
        exit('Done run!');
    }
}

After I dev/build?flush=1 I can run the task successfully via the URL at mysite.com/dev/tasks/MyTask. But I can't run ./vendor/bin/sake dev/tasks/MyTask

Note: I can run ./vendor/bin/sake dev/tasks via the command line, this shows me a list of Silverstripe's build tasks, just not mine.

I'm clearly missing something, can anyone help with this?


Solution

  • See Robbie Averill's comment for the solution.

    Make sure you run vendor/bin/sake dev/tasks flush=1 to flush your cache, it may be a different cache for the CLI and web browser –Robbie Averill