venture icon indicating copy to clipboard operation
venture copied to clipboard

Workflow State Aware Jobs

Open hskrasek opened this issue 2 years ago • 2 comments

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?

hskrasek avatar Dec 02 '22 22:12 hskrasek

Hey! Did you manage to solve this?

oliverbj avatar May 16 '23 19:05 oliverbj

@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.

hskrasek avatar Jun 02 '23 15:06 hskrasek