This episode is for members only

Sign up to access "Job Batching Progress with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
05. Using a factory for different job batches

Transcript

00:00
Hard coding in the jobs directly within this observer probably isn't the best idea. Let's think of another example.
00:06
Imagine that you're creating an interface where a user can upload a video and convert it to a specific type. Now, if you have a single action where you upload a video, you might have different jobs associated with that conversion. For example, you might have different encoding types and you might have
00:22
specific jobs that perform the conversion of a single entity. What we don't want to do is split these up into separate models. So what we want to do is now use that app type that we created to fetch out which set of jobs we want to push.
00:38
And we can do this really easily with a class that predefines all the jobs and then uses a factory based on the type of thing that we're doing. In our case, the type of server that we're creating to give us back them jobs. That might sound complicated, but really all it's doing is just creating a more
00:53
advanced if statement to return to us the jobs that we want to use. OK, let's go ahead and structure this out. So in the main app directory here, I'm going to create a server directory. And inside of here, I'm going to create a types directory.
01:07
This is just going to hold a really simple class which has the type that we want. So in our case, let's call this app server. OK, so inside of this app server class, what we want to do is take in any dependencies that we eventually want to pass through to our jobs.
01:23
So let's create out a constructor in here. And we know that we want to pass in a server. So let's pass in a server directly in here. Make sure we pull that in and from server.
01:35
And we're pretty much good. So when we go ahead and grab this out of the factory that we're going to create, we just pass in any of the dependencies that we need. Now what we can do inside of, say, a jobs method,
01:46
and we could assign an interface to this to make it a little bit easier to know that we need to pass in a jobs method and define this out. It's just an array of all of these jobs. And we saw this from the introduction where we can change around the order of these,
01:59
delete them, or whatever we need to do. So we're going to specifically assign these an order. And this is going to be important a little bit later. So bear this in mind.
02:07
Let's go ahead and create out all of the jobs in here that we want to use. So let's say app jobs server create server. Let's add another one in here at step two to install Nginx. And we'll do three.
02:21
We'll just do finalized server because we'll add in a little bit later a new job when we need to. OK, so now that we've created this, we need a factory to determine, based on the type that we've stored in the database here, which set of jobs that we want to return.
02:37
So again, sounds more complicated than it is. But it's just going to be a class that news up this app server class based on the type that we pass in. So again, we're going to create a PHP class out in here.
02:48
And let's call this server type factory or server factory, whatever we want to call it. So the only thing that we want in here is a static function called make or method called make. And this is just going to pass the server in to determine which type we want to resolve. Or you could just pass in a string here with the server type.
03:09
So let's go ahead and use a match statement in PHP to pass in the server type and then return to us which type of server we want to create. We've only got one at the moment. But if this matches up to app, which is the string we have here,
03:25
then we can just go ahead and return a new app server. And remember, that requires that we pass that server in. OK, so now that we've got our server type factory, we can use this over in our server observer. And then we can extract out the list of jobs we want to pass into our batch.
03:41
So let's call this server type. And we'll go ahead and use our server type factory here to make this out, passing the server in. Let's just kill that so we know that we're getting the right thing back. And then we'll extract out all of them jobs.
03:56
OK, so let's go over to the browser, hit create server. And there we go. We get returned an app server. Now what we need to do is go ahead and just extract them jobs out.
04:05
Again, let's just die dump on this so we know that this is getting returned properly. And let's just hit create server. And there we go. So we have a list of the jobs that we want to pass into that batch.
04:16
OK, so now that we've done that, we can get rid of this hard coded list of these. And we can just pass from server type the list of jobs we want. Now, the benefit of this is at the point of creation when we create our server. So over in our component here, whatever we pass in here is going to get resolved out
04:37
as long as we actually have that type. Of course, you'd want to validate this and make sure that it matched up with the actual types of server we can create or the types of jobs that you will be creating. But now this is all just going to flow really nicely.
04:49
So once again, let's come over to the database, get rid of these servers, get rid of these job batches, and let's go and try this out. So we're creating a app server here. Now that I've pressed that, let's come back over.
05:01
Over in this, we have a server type of app. And if we have a look at our job batches, that's been created within three jobs that come from that resolved class from our server type factory. So of course, all of this is completely optional, but you will find at some stage that when
05:16
you are processing a single thing, you'll probably want slightly different variations of it. What we've done here means that we can very easily come over to our server type factory. If we have a slightly different type of server we want to create, we're just going to go
05:30
ahead and add it in here. For example, if we just wanted a database server, we'll want to create the server, create some database, and then go ahead and finalize everything. That's going to have a completely new set of rules like this.
05:42
So we just copy this app server over and just to find out a new list of jobs in there. Okay, so now that we've done this, let's get rid of that database one. You can go ahead and add as many types here for the thing that you are processing as you want, but we've now changed this over so it's a lot cleaner.

Episode summary

In this episode, we tackle a common problem: hardcoding jobs when dealing with different types of tasks. Rather than stuffing all your logic inside an observer, we look for a cleaner solution using the factory pattern.

We start by considering a scenario where, for example, users can upload a video for conversion. Depending on what type of conversion is needed, we may have different job batches. Instead of creating a separate model for every job variation, we introduce a ServerTypeFactory that helps decide which jobs to run based on the type of server (or task) being processed.

We build a class to represent each server type (like AppServer) and give it a method to return an ordered list of jobs to run for that type. Then, we use our factory to resolve which class to use based on the type information, and pull out the corresponding job list.

You get to see all this wired up, tested, and working—no more hardcoded job lists! When you try creating a server, the right set of jobs gets bundled into the batch automatically. Plus, if you ever need to support more types (like a dedicated database server), it's as easy as adding another class and updating your factory—everything stays neat and easy to maintain.

Episode discussion

No comments, yet. Be the first!