تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Intro to Task Scheduler0:00

Sometimes you need to run a task on some sort of schedule. For example, you might want to clean up some temporary files in a folder, or you might want to send out a weekly email to your users about their stats or usage. The Laravel Task Scheduler makes this incredibly easy with its fluent syntax and convenient methods. It even allows you to test it out locally using a handy php artisan command. So let's take a look at a few examples here. Let's start with the most basic one. So if you go into your app/Console/Kernel.php file, here's where you can schedule your tasks. We have this schedule method here, and you can already see one that's commented out here. So in this case, it's calling the inspire artisan command, but you can also do it inline. So let's start with that. So we can say $schedule->call(...) and this will provide a callback. And within here is where you can run some sort

Scheduling Inline Callbacks0:39

artisan command, but you can also do it inline. So let's start with that. So we can say schedule call and this will provide a callback. And within here is where you can run some sort of code that runs on a schedule. So for now, we'll just use the logger. So we can see in the Laravel log, let's say log()->info, let's say inline task time. And how about we also output the current time and we can say now(). Okay, now we can chain on a method to tell Laravel how often we want to run this. So if we go to the docs, you'll see a bunch of convenient methods here under frequency options. So you can say everyMinute, hourly, weekly, or whatever your needs are. And you can even make use of the cron method if none of these methods do what you want. So I'm going to use everyMinute here because we're going to run this locally. So let's say everyMinute, make sure to import Log here. Let's save that.

Running Scheduler Locally1:24

methods do what you want. So I'm going to use every minute here because we're going to run this locally. So let's say every minute, make sure to import Log here. Let's save that. And now to run the scheduler, you have to make use of the php artisan schedule:run command, which will determine if it has to run. Now on your server, you also have to make sure that this command runs every minute using a cron job. Now if you're using Laravel Forge, it's already configured on every new server that you provision. Now if you want to run it locally, you can make use of this command here. So php artisan schedule:work. So let's see if our task runs every minute. So let's run that command. And I'm going to wait a minute to see if it runs. And I'll open my laravel.log here. And we'll see if it appears here. Okay, I waited a few seconds and you can see the log appear here. So our schedule

Temp Upload Cleanup Use Case2:05

minute to see if it runs. And I'll open my Laravel log here. And we'll see if it appears here. Okay, I waited a few seconds and you can see the log appear here. So our schedule is working. So let's go ahead and stop this. And let's take a look at a few more examples that are more practical. So in the last video, we used file_put_contents to upload files. And whenever we selected a file here, it would automatically get uploaded to a temp folder. And then after we hit the Submit button, then it gets moved to another folder. However, sometimes someone can upload a file but never hit this button here. So it will be a good idea to clean out the temporary files every so often. So I do have Finder open here. And you can see I have three files in our temp folder. So let's see if we can delete files that are older than say five days old. Back to our code. And we can do it in line here. But I like to make

three files in our temp folder. So let's see if we can delete files that are older than say five days old. Back to our code. And we can do it in line here. But I like to make use of artisan commands whenever I schedule tasks. So similar to what we see here, having an artisan command allows you to run the code even if it's not on the schedule. So let's comment this out. And there are several ways to write artisan commands. You can say php artisan make:command. And we can create a new file, say UploadCleanup. And that would work. But you can also make use of the routes/console.php file, which sort of works like the routes/web.php file, but for your console commands. So let's do it that way. So if you scroll down here, you'll see that it is loading the routes/console.php file in order to be aware of our commands. So let's open that up routes/console.php. And you can see the inspire command.

Building Artisan Cleanup Command3:25

down here, you'll see that it is loading the routes/console.php file in order to be aware of our commands. So let's open that up routes/console.php. And you can see the inspire command right here. So let's go ahead and make a new one for our upload cleanup command. So we'll name it upload:cleanup. Let's give it a description of clean up the temp uploads folder. And let's start with some basic output on the console. So we can say this, I'll make use of the info command. And to start we can say cleaning up the temp uploads folder. And then we'll do the work after that. And then we'll have some sort of success message here. So how about we say x files have been deleted on and again, we can provide the timestamp here. So we can say now() and let me change this to double quotes because I'm going to inject a variable later on. Okay, so let's run this command should

can provide the timestamp here. So we can say now and let me change this to double quotes because I'm going to inject a variable later on. Okay, so let's run this command should be php artisan upload:cleanup. And we do get that output. So now let's work on the actual logic for deleting older files. So let's just see if we can get a list of files first. So we can say $files equals, we can use the listContents method on the Storage disk. So we can say Storage::disk('public'). And we'll make use of listContents here. And we want to list the contents of the temp folder. And let's make sure to import Storage here. And I'm going to make use of collections to loop over the $files and dump them out to the console just to see what we're working with. So let's say $files = collect($files) to make it a collection. And let's use each here to loop.

to loop over the files and dump them out to the console just to see what we're working with. So let's say collect files to make it a collection. And let's use each to loop over them. And we should get a $file. So say function should be a $file. And then within here, let's just dump out the $file. So let's say dump the $file. Okay, let's run this again and see what we get. Okay, so we get these three files in our folder here, which correspond to this folder. And we should also get information about the date modified here. So you can see these ones are a bit older, and this one is new. And you can see that information right here in the last modified field. So we have this and the other two files as well. So we only want to delete older files. So let's filter those out first. So we can make use of the filter method to do that. So right here, before the each, let's say filter. And

only want to delete older files. So let's filter those out first. So we can make use of the filter method to do that. So right here, before the each, let's say filter. And again, we have a callback. We have our file here. And for the filter condition, let me just move this up. We also want to make sure it's a file because it does list folders as well. For example, if I had a folder in here, I'm actually going to make one here just to back up the older files. So let's just say backup. And I just want to back these up because we are going to delete them. So let's just copy these and put them in here. Let's go back. And if you take a look at the output here, again, let's actually run that one more time. Now we should get that folder as well right here. But in this case, we only want to work with files. So let's add that to the filter clause. So back to our code, we want to make sure it's a file that we're working with. So let's say

well right here. But in this case, we only want to work with files. So let's add that to the filter clause. So back to our code, we want to make sure it's a file that we're working with. So let's say return, we have file, and we have the type field. Again, this right here, type is either directory or file. So say type equals a file. And we also want to make sure it's an older file. So let's grab this. And we can make use of the lastModified field to check that. So say and file.lastModified. And how about we say files older than five days. So these files right here are older than five days. But this one is not. So after we run this code again, it should only return these two files, because this one is newer. Okay, let's try that out. You have to use Carbon here. So we can say now(), subDays(), and I said 5. And let's use getTimestamp() here. Okay, so by the time we get to this each method, it should have filtered out only files that are older than 5 days old.

can say now, subDays, and I said five. And let's use getTimestamp here. Okay, so by the time we get to this each method, it should have filtered out only files that are older than five days old. So let's save that. Let's run this again. Let me just clear this out. Let's run it again. And you can see we only get those two files now. So this and this. Cool. And you can see we do have the path here. So we can use this to delete the file. So we can do that within here. Let me just comment this out. Again, we can make use of Storage. So let me just grab this actually. And let's delete the file here. So let's say delete. And the path is filePath, filePath. And like I said, down here, we also want the count of how many files we deleted. So how about we say count here. And also save that to a variable. So let's say numberOfFiles equals this. And then we can use that within here. So numberOfFiles. So if we did this, right, it should say two files have been

And also save that to a variable. So let's say numberOfFiles equals this. And then we can use that within here. So numberOfFiles. So if we did this, right, it should say two files have been deleted. And I have a typo here. And of course, it should delete those files as well. So let's try that out. Let's run that again. Let me clear this. Let's run that. Again, remember, we do have our files showing here in Finder. Let's run that. Okay, two files have been deleted. Let's check Finder again. And they have been deleted. Cool. So let me put those files back because now we want to run on a scheduler. So let me grab it from here. Let's copy that and put them back. Okay. And you can see the date modified is still the same. So now let's go back to our Kernel and schedule this command. So it's pretty much the same as what we have here. So let me just copy this. Let's put it underneath here. Let's uncomment it. And let's put the correct command.

Scheduling Command and Emailing Output9:34

and schedule this command. So it's pretty much the same as what we have here. So let me just copy this. Let's put it underneath here. Let's uncomment it. And let's put the correct command in here. So it's upload:cleanup. And how about we run this every minute. So we can see it working locally, every minute. And if you want, you can also save the output of the task somewhere. So you can save it to a file, you can append it to a file, or you can even email it to someone. So we can chain on the email output to provide an email. And that should send out an email. So let's try this. So let's put it right here. Let me just change the email here. And let me just make sure my mailer is sent to log. So we can see it in there. So mailer, let's set it to log. Again, let me go to my level.log, clear this out, save that. And let's go ahead and run our task scheduler locally.

mailer, let's set it to log. Again, let me go to my level.log, clear this out, save that. And let's go ahead and run our task scheduler locally. So it's gonna run this every minute. In reality, you probably want to run this daily or weekly. But like I said, for testing locally, I want to run it every minute. So let me just comment this out. So let's go ahead and save that. Let's run our local scheduler. So it's scheduled work. Again, if we open up Finder, we should have two files that should be deleted. So these two that are on July 7. So let's try that out. Okay, so it looks like it ran that command. Let's check out our Finder here. Those files were deleted. Let's check out our level.log to see if the email sent out. And there is the email. And you can see the output here. And there's even a log for the output as well right here.

And there is the email. And you can see the output here. And there's even a log for the output as well right here. Cool. And of course, you are free to schedule multiple artisan commands here if you need to. So if you want to run this as well, we can have multiple tasks on a schedule like this. So yeah, definitely make use of the task scheduler along with artisan commands within Laravel to schedule any tasks you need to run. So let's go ahead and make a commit here. This is episode seven. Let's say, Laravel task scheduler.

Scheduling TasksRunning the Local SchedulerEmailing Output

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟