Laravel Task Scheduling: Managing Task Output

Laravel

Laravel's task scheduling system makes it simple to manage the output from your tasks, if any. Output from task processes can be appended to a file or emailed to any given email address (provided that email services have been previously configured). The following table lists each of the various output management methods that are defined on the Illuminate\Console\Scheduling\Event base class. The following sections will examine each of the methods in further detail.

Method Description
sendOutputTo($location, $append = false) Send the task output to the given file location. If $append is set to true, the output will be appended at the end of the file.
appendOutputTo($location) Send the task output to the given file location. This is a helper method that calls sendOutputTo with a value of true supplied to the $append parameter.
emailOutputTo($addresses, $onlyIfOutputExists = false) E-mails the results of the task to the given email addresses. This method will always email the results even if no output has been generated (this can be changed by supplying a true value as the argument for the $onlyIfOutputExists parameter).
emailWrittenOutputTo($addresses) Only E-mails generated output to the provided addresses. This method is a helper method that calls emailOutToj with a value of true supplied to the $onlyIfOutputExists parameter.

It is important to note that the helper methods for managing task output are only available when scheduling tasks using the command or exec methods (the command method is a utility wrapper for Artisan commands built on top of the exec method). This is because the scheduling system does not directly manage the output of a task, but rather adds the output control instructions to the generated command that is issued to the environment operating system.

Sending Output to a File

Sending output to a file is the base of task output management. The main method to accomplish this is the sendOutputTo($location, $append = false) method. It accepts the file $location as the argument and can be configured to either overwrite the existing file contents or append to the file by supplying a truth value argument to the $append parameter (by default the sendOutputTo method will overwrite the file contents). The following examples will assume that the Inspire command has been registered in the console kernel.

<?php

// ...

protected function schedule(Schedule $schedule)
{
    $schedule->command('inspire')
             ->everyMinute()
             ->appendOutputTo(storage_path('logs/inspire.log'));
}

// ...

The previous example would run the inspire Artisan command every minute and append the output to the inspire.log file within the application's storage log directory. After the command has been executed a few times, the log file might contain something similar to the following output (the exact output will differ as the inspire command selects messages at random):


Simplicity is an acquired taste. - Katharine Gerould


Simplicity is an acquired taste. - Katharine Gerould


Smile, breathe, and go slowly. - Thich Nhat Hanh


Removing ANSI Color Characters

By default, the output of any Symfony-based command that uses the output helpers might contain ANSI color sequences. These can be disabled when sending the output to files or emails by calling the commands with the --no-ansi option. In the previous code example the command that would have to be ran is inspire --no-ansi.

E-mailing Output

The output of tasks can also be emailed after the task has completed. Emailing output can be accomplished by either using the emailOutputTo and emailWrittenOutputTo methods. Each of these methods require that the output to have been previously saved to a file location using either the sendOutputTo or sendWrittenOutputTo methods.

The emailOutputTo method accepts either a single E-mail address, or an array of E-mail addresses as the argument for the $addresses parameter. Some tasks do not produce any output but will still cause an email to be sent. This can be changed by supplying a truth value as the argument for the $onlyIfOutputExists parameter. By default, the emailOutputTo method will always send an E-mail, even if the task does not generate any output. The emailWrittenOutputTo method is a helper method that internally makes a call to the emailOutputTo method. The emailWrittenOutputTo method will not send an E-mail when there is no task output.

The following examples demonstrate how to use the E-mail methods. It is important to note that the sendOutputTo (or any of the other file-based output management methods) method has been called first:

<?php

// ...

protected function schedule(Schedule $schedule)
{
    // Sending output to a single address.
    $schedule->command('command:name --options')
             ->daily()
             ->appendOutputTo($fileLocation)
             ->emailOutputTo('example@example.com');

    // Sending output to multiple addresses.
    $schedule->command('command:name --options')
             ->daily()
             ->appendOutputTo($fileLocation)
             ->emailOutputTo([
                'example@example.com',
                'example_2@example.com'
             ]);

    // Only send an E-mail if there is actually output.
    $schedule->command('command:name --options')
             ->daily()
             ->appendOutputTo($fileLocation)
             ->emailWrittenOutputTo('example@example.com');

    // Or...
    $schedule->command('command:name --options')
             ->daily()
             ->appendOutputTo($fileLocation)
             ->emailOutputTo('example@example.com', true);
}

// ...

Start the Discussion

Leave a comment

Subscribe to our newsletter