In this episode, we're focused on properly triggering a job batch and storing the batch ID in our application so we can keep track of its progress later.
We start by refining our database. Using the server example, we create a servers
table and make sure it has a batch_id
column. This lets us later query the batch for a particular server and check on its progress. We also think ahead, adding a type
and a nullable provisioned_at
column for extra flexibility.
Then, we set up a Livewire component with a simple UI—a button you click to create a new server. When the button is clicked, the server is created in the database. But instead of immediately creating the job batch right there (which would work but isn't the cleanest way), we opt to use a model observer. This keeps our logic organized and ensures that creating batches happens automatically when a server is created, no matter where in the app the creation happens.
Inside the observer, we spin up the job batch based on the server's type and grab the newly generated batch ID. We save this back to the server, so now every server knows exactly what batch it's tied to. As a bonus, we quickly explore everything the batch class gives us, like checking pending jobs, cancellations, failures, etc.
The end result: whenever a server is created, a batch of jobs gets kicked off and the batch's ID is tracked for that server. This sets us up for real-time progress tracking and management, which we'll build on later in the course!