The main focus of this release is to allow workflows to contain multiple instances of the same job. There are a few breaking changes which are listed below. There will be a proper migration guide once version 3 gets released.
Adding multiple instances of the same job
When you want to add multiple instances of the same job to a workflow, you have to provide explicit id
s for them as part of the addJob
method call.
public function definition(): WorkflowDefinition
{
return Workflow::define('Workflow name')
->addJob(new EncodePodcast($this->podcast, 'mp3'), id: 'encode_mp3', name: 'Encode Podcast as mp3')
->addJob(new EncodePodcast($this->podcast, 'wav'), id: 'encode_wav', name: 'Encode Podcast as wav')
->addJob(new TranslatePodcast($this->podcast), dependencies: ['encode_mp3', 'encode_wav']);
}
You don't have to provide an id if you use the same job instance only once inside a workflow. In these cases, the FQCN of the job will be used as the id.
public function definition(): WorkflowDefinition
{
return Workflow::define('Workflow name')
->addJob(new EncodePodcast($this->podcast, 'mp3'), name: 'Encode Podcast as mp3')
->addJob(new TranslatePodcast($this->podcast), dependencies: [EncodePodcast::class]);
}
This also works if there are duplicate jobs in nested workflows.
class NestedWorkflow extends AbstractWorkflow
{
public function definition(): WorkflowDefinition
{
return Workflow::define('Name')
->addJob(new TestJob1());
}
}
class OuterWorkflow extends AbstractWorkflow
{
public function definition(): WorkflowDefinition
{
return Workflow::define('Name')
->addJob(new TestJob1())
->addWorkflow(new NestedWorkflow(), dependencies: [TestJob1::class]);
}
}
The TestJob1
dependency of the NestedWorkflow
will be correctly scoped to the TestJob1
defined in OuterWorkflow
. Internally, this works by prefixing all job ids in the nested workflow with the id of the nested workflow when calling addWorkflow
.
Adding multiple instances of the same job with providing explicit ids for them will result in a DuplicateJobException
.
return Workflow::define('Workflow name')
->addJob(new EncodePodcast($this->podcast, 'mp3'))
// Will throw a DuplicateJobException because a job with the id `EncodePodcast::class` already exists.
->addJob(new EncodePodcast($this->podcast, 'wav'));
This works exactly the same way for nesting multiple instance of the same workflow.
public function definition(): WorkflowDefinition
{
return Workflow::define('Name')
->addJob(new TestJob1())
->addWorkflow(new NestedWorkflow(), dependencies: [TestJob1::class], id: 'nested_workflow_1')
// Works because we provided different ids for the two workflows.
->addWorkflow(new NestedWorkflow(), dependencies: [TestJob1::class], id: 'nested_workflow_2');
}
Whenever you provide explicit ids for either jobs or nested workflows, you have to refer to these ids when using them as dependencies.
$definition
->addJob(new TestJob1(), id: 'test_job')
// Will blow up because there is no job with an id of `TestJob1::class`
->addJob(new TestJob2(), dependencies: [TestJob1::class])
// This works
->addJob(new TestJob3(), dependencies: ['test_job']);
Breaking Changes
PHP 8 required
This version of Venture requires PHP 8. This is because in order to provide a nice API, both internally as well externally, named arguments are heavily used.
WorkflowDefinition::addJobWithDelay
has been removed.
This method has always been a hack around the fact that addJob
takes so many nullable parameters. It really was just addJob
with a different order of parameters. With named parameters this problem goes away entirely. If you want to add a job without any dependencies, you skip all parameters you don't care about and simply write:
Workflow::define('Name')
->addJob(new TestJob(), delay: now()->addMinutes(5));
hasJob
, hasJobWithDependencies
and hasJobWithDelay
now require job id instead of instance
Previously, you could pass either the FQCN of the job or an instance of the job to any of these function like so:
// Use FQCN
$definition->hasJob(TestJob::class);
// Use job instance
$job = new TestJob();
$definition->hasJob($job);
Internally, Venture would check if you provided an instance and call get_class
on it. This no longer works. Now, you always need to provide the job id.
$job1 = new TestJob1();
$definition = Workflow::define('Test')
->addJob($job1, id: 'job_1')
->addJob(new TestJob2()) // When no id is provided, the FQCN of the class is used as the id.
$definition->hasJob($job1); // Error: Expected type 'string' but got 'TestJob1'
$definition->hasJob(TestJob1::class); // false, because an explicit id was provided above
$definition->hasJob('job_1'); // true,
$definition->hasJob(TestJob2::class); // true
Source code(tar.gz)
Source code(zip)