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

<%- if @topic_view.topic.tags.present? %>
<%= t 'js.tagging.tags' %>: <%- @topic_view.topic.tags.each do |t| %> <%= t %> <%- end %>
<% end %>

Silverstripe Version 4.6.1:

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 BuildTask class and call the task using cron in a unix commandline.

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 it via the command line ./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?

SS seems to use a different cache directory when it is run by different user accounts, so when you run dev/build?flush=1 in your browser, it is clearing the cache for the web server user. If you’re running the CLI task using a different user, it will use a different cache directory, which has not been flushed.

If you make sure you run the CLI task with the web-server user, it should work. Alternatively, flush the cache from the command line as well. You can do this in the same command, e.g.:
./vendor/bin/sake dev/tasks/MyTask flush=1

Oh yes, that worked straight away. I just flushed like you said. Thanks Steve :+1:

1 Like