Batches vs Pipelines

Introduction

When do you use Batches and when do you use Pipelines?

It depends on the complexity of the actions and the data being passed between them.
The short answer is, if it is a simple amount of data and actions, use a Pipeline. Else, use a Batch.

Here are more detailed guidelines:

Pipelines

  • When the returned data does not need to be manipulated, transformed, or mutated outside of the action classes
  • When the Action execute methods do not need multiple parameters
  • If the Action class throws an exception and that exception needs to be caught and handled
  • When the action class can easily be used in an anonymous function

Good Example

class Converter
{
    public function handle($feet)
    {
        return app(Pipeline::class)
            ->send($feet)
            ->through([
                function ($feet, $next) {;
                    return $next(
                        app(ConvertFeetToInches::class)->execute($feet)
                    );
                },
                function ($inches, $next) {
                    return $next(
                        app(ConvertInchesToCM::class)->execute($inches)
                    );
                },
                function ($cm, $next) {
                    return $next(
                        app(ConvertToMeters::class)->execute($cm, 'cm')
                    );
                },
            ])
            ->thenReturn();
    }
}

Batches

  • When the returned data needs to be manipulated, transformed, or mutated outside of the action classes
  • If the next Action is decided by the previous Action
  • When the Action execute methods need multiple parameters
  • If the Action class throws an exception and that exception needs to be caught and handled
  • When the action class cannot easily be used in an anonymous function

Good Example

class CreateUserBatch
{
    public function __construct(
        CreateUser $createUser,
        DoesUserHaveGithubAccount $doesUserHaveGithubAccount,
        SetPermissionsToGithub $setPermissionsToGithub,
        SendWelcomeEmail $sendWelcomeEmail,
        SyncUserToSlack $syncUserToSlack,
        AddUserToSlackChannels $addUserToSlackChannels,
        SyncUserToTrello $syncUserToTrello,
        AddUserToTrelloBoards $addUserToTrelloBoards,
        CreateUserMailbox $createUserMailbox
    ) {
        $this->createUser = $createUser;
        $this->doesUserHaveGithubAccount = $doesUserHaveGithubAccount;
        $this->setPermissionsToGithub = $setPermissionsToGithub;
        $this->sendWelcomeEmail = $sendWelcomeEmail;
        $this->syncUserToSlack = $syncUserToSlack;
        $this->addUserToSlackChannels = $addUserToSlackChannels;
        $this->syncUserToTrello = $syncUserToTrello;
        $this->addUserToTrelloBoards = $addUserToTrelloBoards;
        $this->createUserMailbox = $createUserMailbox;
    }

    public function handle($data, $slackChannels, $trelloBoards)
    {
        $user = $this->createUser($data);
        
        if (! $this->doesUserHaveGithubAccount($user)) {
            try {
                $this->setPermissionsToGithub($user);
            } catch (Exception $e) {
                Log::error($e->getMessage());
                Notification::send(config('services.github.admin'), new GitHubFailed($e));
            }
        }
       
        try {
            $this->syncUserToSlack($user);
            $this->addUserToSlackChannels($user, $slackChannels);
        } catch (Exception $e) {
            Log::error($e->getMessage());
            Notification::send(config('services.slack.admin'), new SlackFailed($e));
        }
        
        $this->syncUserToTrello($user);
        $this->addUserToTrelloBoards($user, $trelloBoards);
        $this->createUserMailbox($user);
        $this->sendWelcomeEmail($user);
    }
}
Previous
Home
Next
Apis