ksassnowski / venture

Venture allows you to create and manage complex, async workflows in your Laravel apps.
https://laravel-venture.com
MIT License
804 stars 32 forks source link

Workflow State Aware Jobs #64

Open hskrasek opened 2 years ago

hskrasek commented 2 years ago

Afternoon,

I'm currently doing research for work around using the Venture package for one of our projects. Something I noticed and wanted to see if you were interested in as a future feature/enhancement is making workflow jobs aware of their parent workflows state. For example, say you use a workflow to model an email campaign where follow-up emails are sent. But if any point in time, the receiver replies, we don't want to send any of the remaining follow-up emails:

<?php declare(strict_types=1);

namespace App;

use Sassnowski\Venture\AbstractWorkflow;
use Sassnowski\Venture\WorkflowDefinition;

class WorkflowTest extends AbstractWorkflow
{
    public function definition(): WorkflowDefinition
    {
        $this->define('Workflow Name')
            ->addJob(id: 'send-first-followup', (new SendFollowUp())->delay($this->settings->first_delay))
            ->addJob(id: 'send-second-followup', (new SendFollowUp())->delay($this->settings->second_delay))
            ->addJob(id: 'send-third-followup', (new SendFollowUp())->delay($this->settings->third_delay));
    }
}

At the moment, I think this is possible with some logic on the end users' side, doing something like this in the Job class

<?php declare(strict_types=1);

namespace App\Jobs;

use Sassnowski\Venture\WorkflowableJob;
use Sassnowski\Venture\WorkflowStep;

class SendFollowup implements WorkflowableJob
{
    use WorkflowStep;

    public function handle(): void
    {
        if ($this->workflow()->isCancelled() || $this->workflow()->isFinished()) {
            return;
        }

        // send email
    }
}

And then, in our business logic, when we receive an inbound email from the user (or another event like an unsubscribe), we can manually reference the workflow and change its state to canceled or finished.

What are your thoughts on making WorkflowableJob's state aware so that little to no code is needed to make workflow jobs aware of their parent's state and not execute or dispatch jobs if the workflow has already finished?

oliverbj commented 1 year ago

Hey! Did you manage to solve this?

hskrasek commented 1 year ago

@oliverbj The solution I highlighted in the original issue is how we handle this right now. It's a little "clunk" or duplicative across multiple jobs, but that if statement at the top of the handle method works.