The Time Travelling Task Manager (TTTM)

Funny Laravel docs · Warning: May cause actual learning AND laughter!

Browse funny laravel docs
On this page

Welcome to the Future, Captain! ๐Ÿš€๐Ÿ•ฐ๏ธ

Preparing for Battle ๐Ÿ› ๏ธ๐ŸŒ

Launch Sequence Initiated!

  1. Running the Mission Control Center
  2. Mission Reports
  3. Mission Alerts
  4. Mission Events

Blast Off! ๐Ÿš€๐Ÿš€๐Ÿš€

Mission Control Center Activated!

  1. Subatomic Scheduled Missions
    • Missions that need to be run more frequently than every minute should be manually triggered from your bridge. The universe wonโ€™t wait for you to catch up, Captain!
  2. Launching the Mission Control Center Locally
    • If you prefer a hands-on approach or need to test your missions in private, you can run the TTTM on your very own spaceship. Just remember to keep an eye on those power levels!

Greetings, time management virtuosos! ๐Ÿ•ฐ๏ธโœจ

In the good old days, scheduling tasks on your server was like organizing a symphony โ€“ each task got its own unique cron configuration. But, as your orchestra grew, keeping track of everyoneโ€™s cues became a headache! Your symphony sheet was nowhere to be found in source control, and you had to teleport into the server (we know, we miss the DeLorean too) just to see your crew or add new acts.

Enter Laravelโ€™s command scheduler โ€“ the maestro of modern task management ๐ŸŽน๐ŸŽ‰! This new conductor lets you elegantly and dramatically define your command schedule right within your Laravel application. With this orchestra leader, a single cron entry on your server is enough to set off an symphony of tasks without needing a hundred backstage passes.

Your new symphony sheet is typically located in the routes/console.php file of your Laravel app โ€“ think of it as the green room where the acts gather before taking center stage.

Now, letโ€™s get this party started! ๐Ÿฅณ๐ŸŽ‰โœจ

Alright, buckle up! Itโ€™s time to dive into Laravelโ€™s Time-Travelinโ€™ Taskmaster, where we send commands back in time (well, every day at midnight)! ๐Ÿ•ฐ๏ธ

First off, letโ€™s learn how to schedule tasks like a boss, right in your applicationโ€™s routes/console.php file. For this tutorial, weโ€™ll clear out a table called โ€˜recent_usersโ€™ every day at midnight (just in case someone tries to sneak in after curfew). Hereโ€™s the code for that:

<?php

// Time-Travelin' Taskmasters and Database Destroyers
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schedule;

Schedule::call(function () {
    DB::table('recent_users')->delete();
})->daily();

But wait! Thereโ€™s more! You can also schedule Invokable Objects, which are PHP classes with a superhero-like __invoke method:

Schedule::call(new DeleteRecentUsers)->daily();

If youโ€™re particular about keeping your routes/console.php file neat and tidy, you can move the scheduling to your applicationโ€™s bootstrap/app.php file using the withSchedule method:

use Illuminate\Console\Scheduling\Schedule;

->withSchedule(function (Schedule $schedule) {
    $schedule->call(new DeleteRecentUsers)->daily();
})

Now, for those of you who like to keep track of your tasks and their next run times, you can use the schedule:list Artisan command:

php artisan schedule:list

Just remember, with great power comes great responsibility. Donโ€™t go deleting important data willy-nilly! ๐Ÿ˜…

Time-Traveling with Artisan Commands! ๐Ÿ•ฐ๏ธ๐Ÿš€

Want to bend time like a boss? With Laravelโ€™s unique scheduling system, you can run your Artisan commands at specific times, even if it means traveling back or forth in the digital universe! ๐Ÿ˜ฒ

Now, letโ€™s get this party started! ๐ŸŽ‰ Besides just scheduling closures, you can also command your Artisan tasks and system commands to stand at attention on a calendar. How about setting up a regular email sending spree? Yes, please! ๐Ÿ“ง

To do so, make use of the command method, which helps you summon an Artisan command using either its name or class. No incantations required! ๐Ÿ˜‰

Use y'all's fancy Email Sending Command class, 'SendEmailsCommand', and bring in Laravel's helpful Facade, Schedule:

    use App\Console\Commands\SendEmailsCommand;
    use Illuminate\Support\Facades\Schedule;

Now that we've assembled our team, let's give them some marching orders!

    Schedule::command('emails:send Taylor --force')->daily();

This snippet tells Laravel to run the 'emails:send' command every day, with Taylor as the recipient and the '--force' option activated. Ain't that swell? ๐Ÿ˜Ž

But wait, there's more! If you prefer using the Artisan command class name, you can pass an array of arguments to be supplied at runtime:

    Schedule::command(SendEmailsCommand::class, ['Taylor', '--force'])->daily();

This is exactly the same as before but with a classy twist! Now your esteemed commander, SendEmailsCommand, will lead the troops with Taylor and the enchanting '--force' option. ๐Ÿดโ€โ˜ ๏ธโœจ

Alrighty then! Letโ€™s dive into the art of scheduling Artisan Closure Commands, shall we? Itโ€™s like setting your own personal Laravel butler to perform tasks for you on a regular basis. How swell is that?

First off, if you fancy scheduling a command thatโ€™s defined by a closure, just link up the scheduling methods post-command definition like so:

Artisan::command('trash:messy_room', function () {
    DB::table('clutter')->delete(); // Boom! Instant clean-up.
})->purpose('Tidy Up Your Digital Home')
->daily(); // Every day, without fail, like your mom asking if you've fed the goldfish.

Now, what if you need to pass arguments to this command? Fear not! Simply provide them to the schedule method:

Artisan::command('letters:write {recipient} {--urgent}', function ($recipient) {
    // Pen, paper, ink... oh my!
})->purpose('Write a letter, you romantic soul')
->schedule(['Betty', '--urgent'])// You can even schedule multiple letters at once, talk about productivity!
->daily();

Just remember to keep the code tidy and organized, or else your butler (or in this case, Artisan) might get a little confused. Happy scheduling, buddy!

Setting the Mood for Scheduled Tasks! ๐ŸŽ‰๐Ÿ•ฐ๏ธ

Ready to unleash your inner time traveler and automate tedious tasks like a boss? Letโ€™s dive into Laravelโ€™s fabulous scheduler, where jobs get a much-deserved vacation from real-time pressure. ๐Ÿ๏ธ๐Ÿน

First up, the job method is your new best friend! Itโ€™s the secret ingredient that turns your queued job into a stress-free, time-released capsule ready for delivery. No more need to define those clumsy closures using the call method. ๐Ÿคนโ€โ™‚๏ธ๐Ÿ“

use App\Jobs\HeartbeatPump;
use Illuminate\Support\Facades\Timer; // Yes, it's a timer, not a stopwatch!

Timer::tickle(new HeartbeatPump)->everyFiveMinutes();

Feeling extra fancy? You can even specify the queue name and connection using optional arguments. Itโ€™s like giving your job an upgrade to first class with extra legroom! ๐Ÿ๐Ÿฟ

use App\Jobs\HeartbeatPump;
use Illuminate\Support\Facades\Timer;

// Dispatch the job to the "heartbeats" queue on the "sqs" connection...
Timer::tickle(new HeartbeatPump, 'heartbeats', 'sqs')->everyFiveMinutes();

Now, letโ€™s discuss scheduling shell commands. Imagine if your server could order pizza for you when itโ€™s low on memory! ๐Ÿ•๐Ÿ’ป

use Illuminate\Support\Facades\Artisan;
use Carbon\Carbon;

// Schedule a shell command to run at exactly 3:14pm every Friday...
Artisan::scheduleCommand('database:optimize --force', new Carbon('2023-07-14', '15:14'))->everyFriday();

And there you have it! Now youโ€™re ready to rule the roost when it comes to automating tasks and making your server work like a well-oiled, automated, pizza-ordering machine. ๐Ÿค–๐Ÿ•๐Ÿš€

Ahoy there, Laravel swashbucklers! Ever found yourself in a pickle where you needed to unleash the mighty power of shell commands upon your server, but didnโ€™t fancy doing it by hand? Well, buckle up, because weโ€™ve got just the pirate youโ€™re looking for!

Enter the exec method, a trusty sidekick that lets you issue commands to the operating system like a seasoned sea dog. Just give it a holler:

use Illuminate\Support\Facades\Schedule;

Schedule::exec('shiver me timbers /home/forge/script.js')->daily();

Now, you might be wondering if this sea shanty has more verses. Well, matey, it surely does! You can also adjust the frequency of these command executions by using various options like:

  • everyMinute() - Perfect for those times when ye need to keep a keen eye on the horizon
  • hourly() - For when youโ€™ve got time on your hands and want to run that script a few hours later
  • twicedaily() - Ideal for those who like their scripts with an extra side of scripts
  • weekdays() - Aye, itโ€™s just like every day, but with a little less Saturday and Sunday

So hoist the sails, grab your mead (erโ€ฆ coffee), and letโ€™s get this party started! Arr! ๐ŸŒˆโ˜ ๏ธ๐Ÿดโ€โ˜ ๏ธโš“๏ธ

Ahoy there, Captain! Setting sail through Laravelโ€™s sea of scheduling options, eh? Buckle up and letโ€™s navigate the shores of time management!

Weโ€™ve already docked at a few ports showcasing how to configure tasks for specific intervals. But fret not, matey, thereโ€™s an entire archipelago of task schedule frequencies just waiting to be explored:

MethodDescription
->cron('* * * * *');Set sail for a custom cron schedule, if ye be so inclined.
->everySecond();Steer the ship every second, if ye dare!
->everyTwoSeconds();For those who like their tasks twice as nice.
->everyFiveSeconds();Five golden minutes โ€˜tween each task, aye?
->everyTenSeconds();If ten be your lucky number, this is for thee!
->everyFifteenSeconds();A quarter-hourly option, great for tea time.
->everyTwentySeconds();For those with an eye for precision, but not too much.
->everyThirtySeconds();Thirty seconds, a minuteโ€™s companion.
->everyMinute();One minute intervals, the perfect frequency for boredom.
->everyTwoMinutes();Double the fun, double theโ€ฆ boredom?
->everyThreeMinutes();Three times the joy, three times the yawns.
->everyFourMinutes();Four times a charm, or four times a bore?
->everyFiveMinutes();Five minutes โ€˜twixt each task, yeโ€™ll get used to it.
->everyTenMinutes();If ten be your limit, set your sails accordingly.
->everyFifteenMinutes();A half-hourly option, ideal for long naps.
->everyThirtyMinutes();Thirty minutes apart, a decent gap between tasks.
->hourly();Every hour on the hour, like the relentless ticking of a clock.
->hourlyAt(17);Run the task every hour at 17 minutes past the hour, great for late night owls!
->everyOddHour($minutes = 0);For those who love the odd numbers.
->everyTwoHours($minutes = 0);Two hours apart, just enough time to make a plan.
->everyThreeHours($minutes = 0);Three hours apart, yeโ€™ll have plenty of tasks complete!
->everyFourHours($minutes = 0);Four hours apart, a good time for lunch.
->everySixHours($minutes = 0);Six hours apart, a decent spacing for sleepyheads.
->daily();Set your sails to run every day at midnight, a ye olde tradition.
->dailyAt('13:00');Run the task daily at 1 PM, perfect for lunch breaks!
->twiceDaily(1, 13);Set sail twice a day, at 1 AM and 1 PM.
->daysOfMonth([1, 10, 20]);Limit the task to specific days of the month, ye scallywag!
->weekly();Set your sails for a weekly voyage, every Sunday at midnight.
->weeklyOn(1, '8:00');Run the task every week on Monday at 8 AM, the start of a new workweek!
->monthly();Set your sails for monthly trips, on the first day of each month at midnight.
->monthlyOn(4, '15:00');Run the task every month on the 4th at 3 PM, ideal for tea time!
->twiceMonthly(1, 16, '13:00');Set sail twice a month, on the 1st and 16th at 1 PM.
->lastDayOfMonth('15:00');Run the task on the last day of the month at 3 PM, perfect for end-of-month tasks!
->quarterly();Set your sails for quarterly journeys, on the first day of each quarter at midnight.
->quarterlyOn(4, '14:00');Run the task every quarter on the 4th at 2 PM, great for afternoons!
->yearly();Set your sails for an annual voyage, on the first day of each year at midnight.
->yearlyOn(6, 1, '17:00');Run the task every year on June 1st at 5 PM, ideal for summer evenings!
->timezone('America/New_York');Set the timezone for the task, useful for global adventures.

These methods can be combined with additional constraints to create finely-tuned schedules that only run on certain days of the week:

MethodDescription
->weekdays();Limit the task to weekdays.
->weekends();Limit the task to weekends.
->sundays();Limit the task to Sunday.
->mondays();Limit the task to Monday.
->tuesdays();Limit the task to Tuesday.
->wednesdays();Limit the task to Wednesday.
->thursdays();Limit the task to Thursday.
->fridays();Limit the task to Friday.
->saturdays();Limit the task to Saturday.
->days(array|mixed);Limit the task to specific days.
->between($startTime, $endTime);Limit the task to run between start and end times.
->unlessBetween($startTime, $endTime);Limit the task to not run between start and end times.
->when(Closure);Limit the task based on a truth test.
->environments($env);Limit the task to specific environments.

Ahoy, matey! Set sail for Laravelโ€™s vast sea of scheduling options with this newfound knowledge at your helm! Arrrrgh, good luck!

Alright, buckle up, Laravel enthusiasts! Letโ€™s dive into the world of day constraints - the unsung heroes of your command line tasks! ๐Ÿ•ฐ๏ธโ˜€๏ธ

Ever wondered how to schedule a task like a boss but only on specific days of the week? Well, wonder no more because the days method is here to save the day (or rather, specific days)!

use Illuminate\Support\Facades\Schedule;

// You're just one line away from scheduling commands hourly on Sundays and Wednesdays... talk about a power duo ๐Ÿ’ช๐ŸŒž
Schedule::command('emails:send')
    ->hourly()
    ->days([0, 3]);

But wait! Thereโ€™s more. If you fancy yourself as a constants connoisseur (and who doesnโ€™t, really?), you can use the delightful offerings from the Illuminate\Console\Scheduling\Schedule class when defining your days of glory:

use Illuminate\Support\Facades;
use Illuminate\Console\Scheduling\Schedule;

// Now we're talking. Let's schedule 'emails:send' hourly on Sundays and Wednesdays using constants ๐ŸŽ‰๐ŸŽŠ
Facades\Schedule::command('emails:send')
    ->hourly()
    ->days([Schedule::SUNDAY, Schedule::WEDNESDAY]);

Just remember, with great power comes great responsibility. So, donโ€™t go scheduling a task to run every hour on every day (unless you have an unlimited data plan, in which caseโ€ฆ carry on! ๐Ÿ“ต๐ŸŽ‰)

Happy scheduling! ๐Ÿค–๐ŸŽˆ

Alright, letโ€™s get this party started! When youโ€™re juggling more tasks than a one-armed circus performer, the between method is your trusty trapeze. This magical tool allows you to confine your jobs within certain hours of the day, ensuring that even late sleepers like Uncle Narcolepsy wonโ€™t be woken up by your emails at 3 AM.

Schedule::command('emails:send')
    ->hourly()
    ->between('7:00', '22:00'); // You know what they say, "Work hard, sleep hard"!

On the flip side, there are times when you want to take a break, like during your afternoon catnap or during the World Cup final. The unlessBetween method comes to the rescue! Itโ€™s like having a personal assistant who knows when to let the calls roll over because youโ€™re busy watching Ronaldo score another hat-trick.

Schedule::command('emails:send')
    ->hourly()
    ->unlessBetween('23:00', '4:00'); // Sleep is for the weak! Or those who want to miss your emails...

Now, go forth and automate, you magnificent time-management maestro! ๐ŸŽ‰๐Ÿ•’๐Ÿš€

Alrighty then! Letโ€™s dive into the world of Laravelโ€™s Time-Traveling Task Force, where commands arenโ€™t just executed, theyโ€™re unleashed like a herd of digital buffaloes on the vast prairie of your web app. But sometimes, you might need to corral these buffaloes and let โ€˜em loose only when conditions are just right - thatโ€™s where our trusty Truth Test Constraints come in!

First off, weโ€™ve got the when sheriff. This trickster can limit the galloping of a command based on the result of a truth test. Itโ€™s like having a cunning coyote at the herdโ€™s tail, waiting to let them loose only if it spots a rabbit (or in this case, if the given closure returns true).

Schedule::command('emails:send')->daily()->when(function () {
    return true; // A coyote's trick: always returns a "rabbit"
});

Next up, weโ€™ve got the skip deputy. If this lawman sees you coming, heโ€™ll hold back the command herd. Essentially, itโ€™s the inverse of when. If the skip method returns true, the scheduled task will take a detour around your app like a cowboy dodging tumbleweeds.

Schedule::command('emails:send')->daily()->skip(function () {
    return true; // A lawman's warning: always points the herd away
});

Now, when youโ€™re chaininโ€™ when methods like a pro cowboy, remember that the command will only break free and rampage across your app if all when conditions return true. Itโ€™s like trying to lasso a whole herd at once - if one coyote misses its mark, the buffalo stays corralled!

So saddle up and harness the power of Truth Test Constraints to manage those command herds with ease. Happy corralling! ๐Ÿค 

Alrighty, letโ€™s get this tech-talk party started! In the grand world of Laravel, weโ€™ve got this swanky feature called environments, and itโ€™s a real crowd-pleaser. Now, donโ€™t be fooled by its casual nameโ€”itโ€™s as serious as a lava flow at a volcano convention.

So, how does this fancy pants feature work? Well, imagine youโ€™ve got a task that only Aunt Mable in the โ€˜stagingโ€™ environment or Uncle Sam in โ€˜productionโ€™ can handle (we all know those folks who can only manage email duties during specific phases). Thatโ€™s where environments come into play!

Schedule::command('emails:send')
    ->daily()
    ->environments(['staging', 'production']);

In this example, every day at dusk, the command to send emails will only be dispatched to the lively residents of โ€˜stagingโ€™ and โ€˜production.โ€™ So, next time you find yourself with a task that requires some serious โ€˜stage managingโ€™, donโ€™t hesitate to call upon environmentsโ€”your Laravel lifesaver!

P.S. Remember, while weโ€™re all about having fun here, our commitment to accuracy remains unwavering. So, if youโ€™d like a deeper dive into those environmental configurations, check out the official docs. Happy coding, party people! ๐Ÿฅณ๐Ÿš€

Alrighty, letโ€™s get this time-tangling tutorial rolling! ๐Ÿ•ฐ๏ธโœจ

Timezones: The Great Synchronizer of Tasks ๐Ÿ•๐ŸŒ

Welcome to the grand olโ€™ world of Laravel timezones where we set our scheduled tasks loose in different time zones, creating a harmonious symphony of digital events across the globe! ๐ŸŽ‰๐ŸŽถ

To get started, youโ€™ll need to learn the timezone method. This magical command allows you to specify that a task should be like a traveling troubadour, performing its duties within a given timezone:

use Illuminate\Support\Facades\Schedule;

// Let's imagine our task is generating a report in the style of a 1920's jazz band.
Schedule::command('report:generate')
    ->timezone('America/New_York') // In this case, our troubadour will play their tunes in New York City.
    ->at('2:00'); // Every day at 2 PM sharp! Just like Louis Armstrong himself.

In case you find yourself frequently assigning the same timezone to all your tasks, you can set a default one by defining a schedule_timezone option within your applicationโ€™s app configuration file:

'timezone' => 'UTC', // A universal choice for setting the tempo of your Laravel event.

'schedule_timezone' => 'America/Chicago'; // This is our hometown, where all good Laravel tasks come from! ๐Ÿ™๏ธ

[!!๐Ÿšจ WARNING !!] Timezones can sometimes be a wild, unpredictable beast. Some even have the audacity to follow daylight savings time, which means they might skip or double-book your scheduled tasks when their clocks spring forward or fall back! ๐Ÿ•ฐ๏ธโณ

To avoid any potential snafus, we recommend performing tasks without a timezone whenever possible. Itโ€™s like dancing to a metronome rather than a swinging jazz riff - it might be less fun but itโ€™ll definitely keep your tasks on beat! ๐ŸŽถ๐Ÿ•บ

Now that weโ€™ve got the rhythm, letโ€™s groove on to our next topic: preventing task overlaps! ๐Ÿ’ƒ๏ธ๐Ÿ•บ๏ธ

Stay tuned for more Laravel dance lessons coming your way! ๐Ÿ•บ๏ธโœจ

The Saga of the Never-Ending Emails! ๐Ÿ“ง๐Ÿ•ฐ๏ธ

By popular demand, your favorite superhero, Laravel, has come up with a cape for handling those pesky, never-ending tasks that just wonโ€™t quit! ๐Ÿฆธโ€โ™‚๏ธ

By default, our friendly neighborhood superhero will keep on running scheduled tasks even if the previous one is still in action. But fear not, citizens of Laravel Land, we have a solution for you! ๐Ÿ›ก๏ธ

To prevent these task overlaps, simply use the withoutOverlapping method:

use Illuminate\Support\Facades\Schedule;

Schedule::command('emails:send')->withoutOverlapping();

In this example, our caped crusader will send out those emails every minute if they arenโ€™t already in the middle of a superheroic struggle. The withoutOverlapping method is a lifesaver when you have tasks that are as unpredictable as the Weather Wizard himself! โšก๏ธ

If needed, you can even specify how many minutes must pass before the โ€œwithout overlappingโ€ lock takes a well-deserved break. By default, itโ€™ll take a breather after 24 hours:

Schedule::command('emails:send')->withoutOverlapping(10);

Behind the scenes, the withoutOverlapping method employs your applicationโ€™s cache to obtain locks. If you find yourself in a pickle where a task is stuck due to an unexpected server mishap, you can use the schedule:clear-cache Artisan command to clear those pesky cache locks. ๐Ÿงน

But wait, thereโ€™s more! Our superhero has also included a nifty feature that allows you to run tasks on just one server, keeping your other servers free to deal with emergencies. Stay tuned for more details on that in an upcoming issue! ๐ŸŒŸ๐Ÿš€

๐Ÿšจ Attention, all you multi-tasking mavericks! ๐Ÿš€

Before we dive in, letโ€™s get one thing straight: if your appโ€™s cache driver is anything other than database, memcached, dynamodb, or redis, this ainโ€™t the blog post for you. You might as well be trying to teach a pig Latin - it just ainโ€™t gonna work!

Now, suppose your applicationโ€™s scheduler is running wild on multiple servers like a pack of marauding digital wolves. In that case, you might find yourself in a pickle when a scheduled job decides to play hide-and-seek across all three servers, generating the same report three times - oh dear! ๐Ÿ™€

To keep your appโ€™s sanity intact, simply use the onOneServer method when defining your schedule. It works like a secret squirrel, ensuring that only one server will grab the task and secure an atomic lock on the job to prevent other servers from running amok at the same time:

use Illuminate\Support\Facades\Schedule;

Schedule::command('report:generate')
    ->onFridays()
    ->at('17:00')
    ->onOneServer();

And if you want to customize the cache store used by the scheduler for atomic locking, feel free to use the useCache method:

Schedule::useCache('database');

Just remember, just like a well-oiled machine, your app needs a bit of maintenance to keep it running smoothly! ๐Ÿ› ๏ธ๐Ÿ’ช

Alright, letโ€™s lighten things up a bit! Hereโ€™s how you can tell Laravel to keep your single server from feeling overwhelmed yet still keep its multi-tasking chops intact:

Giving Jobs on the Rocks (Single Server) Names

Sometimes, just like in a busy bar, youโ€™ve got different orders with custom twists coming in, but you donโ€™t want to hire more bartenders. In Laravel terms, that means scheduling the same job with distinct parameters and running it all on one server. To pull this off without confusion, you can give each drink (schedule definition) a unique name using the name method:

If you're hankering for a laravel.com uptime check:
Schedule::job(new CheckUptime('https://laravel.com'))
    ->name('check_uptime:laravel.com')
    ->everyFiveMinutes()
    ->onOneServer();

Feeling fancy for vapor.laravel.com? No problem! Just give it a new name and order up:
Schedule::job(new CheckUptime('https://vapor.laravel.com'))
    ->name('check_uptime:vapor.laravel.com')
    ->everyFiveMinutes()
    ->onOneServer();

Similarly, if youโ€™ve got a scheduled closure thatโ€™s all about the API request count reset, donโ€™t forget to give it a distinctive handle before sending it out:

And for a nightly round of User API request count resets:
Schedule::call(fn () => User::resetApiRequestCount())
    ->name('reset-api-request-count')
    ->daily()
    ->onOneServer();

Remember, in the world of Laravel, a job well done is all about staying organized and making sure no task goes unnoticed! Cheers to efficient multitasking! ๐Ÿฅƒ๐Ÿป

Party Like Itโ€™s 1999 (But in a More Organized Manner)

By the stroke of midnight, every night (well, technically every day at 00:00), your trusty Laravel steed starts juggling tasks like Cirque du Soleil on a sugar rush. By default, these circus acts are performed in a line, each one waiting for its turn before it takes center stage - a bit like that awkward family game night where everyoneโ€™s waiting for their turn at Charades.

However, if youโ€™ve got tasks that wonโ€™t quit (like your uncle who just wonโ€™t stop telling stories about the good old days), these long-running performances might delay your other acts from starting as promptly as youโ€™d like. But fear not, for help is here in the form of the ever-reliable runInBackground method!

use Illuminate\Support\Facades\Schedule;

Schedule::command('analytics:report')
    ->daily()
    ->runInBackground();

Attention

The runInBackground method is a bit like inviting the clowns to their own show โ€“ theyโ€™re only allowed to join when scheduling tasks via the command and exec methods. So, unless you want to witness a chaotic circus where your tasks take center stage all at once (and no one wants that), remember to stick with these trusted methods!

Now, sit back, relax, and let Laravel handle the scheduling like a pro while you focus on making those amazing apps. And who knows? With this newfound multi-tasking skill, maybe your code will finally be as graceful as a trapeze artist! ๐Ÿคนโ€โ™‚๏ธ๐Ÿคนโ€โ™€๏ธ๐Ÿš€

Keeping Your App in Tip-Top Shape (aka Maintenance Mode)! ๐Ÿงป

Hey there, coding cowboy/cowgirl! You know what they say - an app that doesnโ€™t take a break every now and then is like a horse without a saddle. And we all know how that endsโ€ฆ ๐ŸŽ๐Ÿ”ฅ

When you decide to give your server some TLC (Tender Love and Code), your applicationโ€™s scheduled tasks will be put on hold, just like a loyal dog waiting for its master to return home. We wouldnโ€™t want those tasks causing chaos while youโ€™re mid-server-massage, now would we?

But what if youโ€™ve got a particularly stubborn task that insists on being done, even when the โ€œDo Not Disturbโ€ sign is up? Fear not! You can call upon the magical evenInMaintenanceMode method, as powerful as a unicornโ€™s horn-blast, to force that task to run during maintenance mode:

Schedule::command('emails:send')->evenInMaintenanceMode();

Just remember, even the bravest of tasks need their downtime. So, use this power wisely, and always take time for a well-deserved server spa day! ๐Ÿง–โ€โ™€๏ธ๐Ÿง”๐Ÿผ๐Ÿง–โ€โ™‚๏ธ

Pausing Scheduled Tasks: Sometimes we all need a nap. If you ever find yourself in the middle of a server slumber party and want to wake up a specific task, just give it a gentle nudge with the wakeUp method:

Schedule::command('emails:send')->dontStart(); // This will pause the task, but not cancel it.
// Later on...
Schedule::command('emails:send')->wakeUp(); // Bring that slumbering task back to life!

Now go forth and conquer, one scheduled task at a time! ๐Ÿ‘‹๐Ÿ’ฅ๐Ÿš€

A Temporary Timeout for Your Laravel Tasks! ๐Ÿ•’โฑ๏ธ

Need a break from those relentless tasks? No problemo! Laravelโ€™s got your back with the schedule:pause Artisan command. Itโ€™s like calling a time-out, but for your scripts!

php artisan schedule:pause ๐Ÿ›‘๐Ÿ•’

Just like when your toddler is having a meltdown and you need a moment to breathe, when the scheduler is paused, no tasks will be processed. But donโ€™t worry, you can get back to it all with the schedule:continue command:

php artisan schedule:continue ๐Ÿ•’โฉ

If thereโ€™s a task that simply must happen even when youโ€™re on a break, like sending important emails or reminding your cat to stop eating your plants (seriously, Fluffy, give it a rest!), you can mark it with the evenWhenPaused method:

Schedule::command('emails:send')->evenWhenPaused(); ๐Ÿ“ง๐ŸŒฑ

Just remember to use these commands wisely, or your cat might end up with more emails than you! ๐Ÿ˜บ๐Ÿ’Œ

Time Management for Lazy Developers (aka Task Grouping)

Listen up, procrastinators! Laravelโ€™s here to save the day with its time management system for lazy developers. You know, those of us who donโ€™t want to repeat ourselves a billion times when setting up scheduled tasks with similar configurations. Thatโ€™s where task grouping comes inโ€”it simplifies your code and ensures everyoneโ€™s favorite thing: consistency!

So, how do we get this party started? First, gather round and call upon the Laravelโ€™s scheduler like you would a genie in a bottle. But instead of three wishes, youโ€™ll be granted the power to define tasks (wow, what a letdown).

use Illuminate\Support\Facades\Schedule;

// This is your genie call, summon it!
Schedule::daily()
    // Set the stage for this task drama: On One Server, in America/New_York timezone.
    ->onOneServer()
    ->timezone('America/New_York')
    // Time to group tasks like a boss
    ->group(function () {
        // Now cast your spells...
        Schedule::command('emails:send --force');
        Schedule::command('emails:prune');
    });

Now, go forth and conquer your to-do lists with Laravelโ€™s task grouping feature. Or, you know, keep procrastinating. Itโ€™s all the same to us! ๐Ÿ˜œ

Alrighty, mate! So youโ€™ve created some fancy Laravel tasks that are just itching to be set free on your server. But how do we unleash these beasties? Fear not, my friend! Weโ€™re about to learn the art of summoning our tasks with the magical schedule:run Artisan spell.

This enchantment scans all your scheduled tasks and decides if they should dance based on the current time of your server. Basically, itโ€™s like having a clockwork taskmaster that keeps tabs on your crew.

Now, when you want to turn up the party with Laravelโ€™s scheduler, all you gotta do is add a lone cron configuration entry to your server. And guess what? You can call this new mate using the schedule:run command every minute.

If youโ€™re still scratching your head about adding cron entries, fear not! Consider recruiting the help of a trusty managed platform like Laravel Cloud. Theyโ€™re total pros at managing those scheduled tasks for you:

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

Pro tip: If you want your tasks to run more frequently than every minute (crazier than a one-legged drummer at a techno rave), just adjust the cron entryโ€™s time accordingly. Just remember, more frequent tasks might make your server dance harder โ€“ so be careful not to overwork it! ๐Ÿ•บ๐Ÿ’ƒ

Ah, the ticking time bomb of cron jobs! Typically confined to a minuteโ€™s worth of mischief on most operating systems. But fret not, dear Laravel devs, for our scheduler is here to blow that ceiling wide open!

You can schedule tasks to run faster than a cheetah chasing its tail, yes, even every second if you so desire:

use Illuminate\Support\Facades\Schedule;

// Delete all the recent users who've been acting fishy (pun intended)
Schedule::call(function () {
    DB::table('recent_users')->delete();
})->everySecond();

But wait, thereโ€™s more! When youโ€™ve got a minute to kill, our scheduler wonโ€™t bail out on you before the buzzer. Instead, itโ€™ll keep running until the final second ticks away, ensuring all sub-minute tasks get their moment in the spotlight.

However, beware the time-consuming tasks that could leave your server stuck in a never-ending loop of deletion and regret (weโ€™re looking at you,DeleteRecentUsers job). To avoid such calamity, itโ€™s best to dispatch queued jobs or background commands for the heavy lifting:

use App\Jobs\DeleteRecentUsers;

// Delete recent users in ten-second intervals, while keeping cool and collected
Schedule::job(new DeleteRecentUsers)->everyTenSeconds();

// For good measure, let's also delete those pesky users every ten seconds... in the background
Schedule::command('users:delete')->everyTenSeconds()->runInBackground();

Oh, and remember, if you need to interrupt a sub-minute task thatโ€™s gone rogue (weโ€™ve all been there), just hit it with a Ctrl + C, or better yet, use Laravelโ€™s built-in task cancellation feature. Because who needs a time-out when youโ€™ve got a task timeout? ๐Ÿ˜‰

Ahoy there, Captain! Sail the seas of Laravel land and thou shalt encounter a perplexing problem: tasks so swift theyโ€™re sub-minute, yet take the full minute to complete. Aye, verily, this can cause quite the ruckus during deployment. Fear not, for I shall guide thee through the foggy waters!

As the schedule:run command doth set sail for a full 60 seconds when sub-minute tasks are defined, sometimes we need to hail it from the shore during deployments. If left unchecked, an already running instance of this command would continue hoisting the colors of your previously deployed code until the clock strikes the hour (or minute, rather).

To signal the schedule:run ship to return to port, ye may hoist the Jolly Roger (or in modern terms, add) the schedule:interrupt command to yer deployment script. Invoke this command after yer application hath completed deploying:

php artisan schedule:interrupt

And there ye have it, matey! Now, when thou heareth the cannons fire and doth see the schedule:run ship turn tail, thou knowest that the old code hath been replaced by the new. Sail on, sailor, and remember to always keep yer ship in tip-top shape!

Ahoy there! Buckle up, matey! Scheduling in Laravel isnโ€™t just about setting sail for automation island. Itโ€™s a swashbuckling adventure where tasks are delegated and executed with precision, like a well-oiled pirate crew! ๐Ÿดโ€โ˜ ๏ธ

Now, you might be tempted to draw ye olโ€™ sea serpent of scheduler cron entries on yer local development machine. But hold yer horses, me hearties! Instead, ye can hoist the Jolly Roger with the schedule:work Artisan command. This command ainโ€™t no timid landlubber - it runs in the foreground, like Captain Jack Sparrow on a mission, invokinโ€™ the scheduler every minute (or shall we say, every tick of the clock tower) until ye terminate the command. And when tasks are defined that require less time than a pirateโ€™s rum-swilling session, this valiant commander will continue running within each minute to process those scurvy tasks!

So, if ye be ready to set sail on this automation voyage, letโ€™s cast off with:

php artisan schedule:work

Arrr, thatโ€™s the spirit of Laravel scheduling for ya! Now go forth and conquer the seven seas of tasks at hand! ๐ŸŒฌ๏ธ๐ŸŒŠ

Alrighty, letโ€™s get our Laravel aprons on and dive into the world of task output! ๐Ÿฅ„โœจ

First off, we have the Laravel scheduler, a handy tool that helps us manage outputs like a pro chef manages his sous-vide bags. Using the sendOutputTo method, you can send your taskโ€™s output to a file for later perusal - perfect for when you need to review your recipes after a long night of coding. ๐Ÿ“๐Ÿฝ๏ธ

use Illuminate\Support\Facades\Schedule;

Schedule::command('emails:send')
    ->daily()
    ->sendOutputTo($filePath);

Now, if youโ€™d prefer to add your output to an existing file instead of overwriting it, no problem! The appendOutputTo method is here to help. ๐Ÿ“๐Ÿ‹๏ธโ€โ™‚๏ธ

Schedule::command('emails:send')
    ->daily()
    ->appendOutputTo($filePath);

Next up, we have the emailOutputTo method, which lets you email your output to an email address of your choice. Just donโ€™t forget to configure Laravelโ€™s email services first! ๐Ÿ“ง๐Ÿ’Œ

Schedule::command('report:generate')
    ->daily()
    ->sendOutputTo($filePath)
    ->emailOutputTo('taylor@example.com');

If you only want to email the output when your scheduled task or command fails (i.e., when it returns a non-zero exit code), then use the emailOutputOnFailure method to ensure that only burning disasters get reported. ๐Ÿ”ฅ๐Ÿš’

Schedule::command('report:generate')
    ->daily()
    ->emailOutputOnFailure('taylor@example.com');

Warning

Remember, the emailOutputTo, emailOutputOnFailure, sendOutputTo, and appendOutputTo methods are exclusive to the command and exec methods. So donโ€™t go trying to apply them to your soufflรฉ recipes - trust us, it wonโ€™t work! ๐Ÿฅ˜๐Ÿฒ๐Ÿš€

Now you know how to handle task outputs like a boss! Go forth and schedule tasks with confidence! ๐Ÿ†๐ŸŽ‰๐Ÿฅณ

Alright, letโ€™s dive into the world of Task Hooks โ€“ the swiss army knife of Laravel scheduling!

With our trusty before and after methods, you can ensure your mom never forgets to take her medication (or execute a scheduled task, if you prefer). Just remember: before she takes her medicine, do any last-minute checks; after, send congratulatory emails or reminders for the next dose!

use Illuminate\Support\Facades\Schedule;

// Mom's daily pill reminder
Schedule::command('medicineTime:remind')
    ->daily()
    ->before(function () {
        // Check if it's actually mom taking the pills...
    })
    ->after(function () {
        // Send a friendly reminder for the next dose!
    });

Now, letโ€™s discuss our new favorite superheroes: onSuccess and onFailure. These guys are like Batman and Robin, always ready to help in times of need (or success/failure, if you will). If your scheduled command succeeds, theyโ€™ll celebrate with a victory dance; if it fails, theyโ€™ll don their capes and investigate the crime scene!

Schedule::command('emails:send')
    ->daily()
    ->onSuccess(function () {
        // The task succeeded! Let's throw a party!
    })
    ->onFailure(function () {
        // Oh no, something went wrong. Time for detective work!
    });

But what if your command leaves behind some clues? No worries, just type-hint an Illuminate\Support\Stringable instance as the argument of your hookโ€™s closure definition to access any output it might have produced:

use Illuminate\Support\Stringable;

// Our trusty sidekick, Stringable, helps us make sense of the command output.
Schedule::command('emails:send')
    ->daily()
    ->onSuccess(function (Stringable $output) {
        // The task succeeded, and here's what it had to say...
    })
    ->onFailure(function (Stringable $output) {
        // The task failed, and here's the incriminating evidence!
    });

Now that youโ€™ve mastered Task Hooks, your Laravel apps will be as smooth as a well-oiled crime-fighting machine!

Alright, Laravel buddies! Letโ€™s dive into the world of URL pinging - the digital equivalent of sending a postcard from your scheduled tasks to their BFFs! ๐Ÿ“ฎ

With our super cool pingBefore and thenPing methods, you can automate sending a friendly โ€œHey, Iโ€™m about to start!โ€ or โ€œJust wrapped up!โ€ message to that chill service like Envoyer, your taskโ€™s social media manager. ๐Ÿค–

Hereโ€™s the party trick:

Schedule::command('emails:send')
    ->daily()
    ->pingBefore($url) // Start of the party
    ->thenPing($url); // End of the party

But, what if you want to keep your secrets? You can use pingOnSuccess and pingOnFailure to send a covert message only when the task succeeds or fails (like a mission update). Failure here means the scheduled Artisan or system command ended with a mysterious exit code. ๐Ÿ•ต๏ธโ€โ™‚๏ธ

Schedule::command('emails:send')
    ->daily()
    ->pingOnSuccess($successUrl) // Mission accomplished!
    ->pingOnFailure($failureUrl); // Uh-oh, something went wrong!

Now, letโ€™s add a bit more mystery to it. Use the pingBeforeIf, thenPingIf, pingOnSuccessIf, and pingOnFailureIf methods when you want to send your message only if a specific condition is met. Itโ€™s like having an intelligent PA for your tasks!

Schedule::command('emails:send')
    ->daily()
    ->pingBeforeIf($condition, $url) // If the condition is true, start sending party invites!
    ->thenPingIf($condition, $url); // If the condition is true, send a "Thanks for coming!" message.

Schedule::command('emails:send')
    ->daily()
    ->pingOnSuccessIf($condition, $successUrl) // If the condition is true and mission accomplished, send a secret intel!
    ->pingOnFailureIf($condition, $failureUrl); // If the condition is true and something went wrong, send an emergency alert! ๐Ÿšจ

Happy pinging!

Yo, Event Party! ๐ŸŽ‰๐ŸŽŠ

In the wild world of Laravel, events are like invitations to a glamorous ball where all the cool kids hang out. During the scheduling process, our bouncer (a.k.a your application) dishes out these fancy event invites left and right! ๐Ÿ•บ๏ธ๐Ÿ’ƒ๏ธ

You can totally RSVP to any of these soirees by creating some dapper listener suits (yes, you read that right). Hereโ€™s the guest list for tonightโ€™s bash:

Event Name
Illuminate\Console\Events\ScheduledTaskStarting - Cocktail hour starts, grab your champagne flutes! ๐Ÿฅ‚
Illuminate\Console\Events\ScheduledTaskFinished - Time to hit the dance floor after a successful schedule execution! ๐Ÿ’ƒ๏ธ๐Ÿ•บ๏ธ
Illuminate\Console\Events\ScheduledBackgroundTaskFinished - Background tasks are like those quiet, but oh-so-important friends at the party. Kudos for a job well done! ๐Ÿ†๐ŸŽ‰
Illuminate\Console\Events\ScheduledTaskSkipped - Oopsie, someone didnโ€™t show up to the party! Letโ€™s figure out why later. ๐Ÿšซ๐ŸŽŠ
Illuminate\Console\Events\ScheduledTaskFailed - Oh noes! Someone spilled the punch bowlโ€ฆ but hey, no one gets left behind, letโ€™s clean it up together! ๐Ÿคง๐Ÿฅ›๏ธ

So, grab your tuxedos and party hats, folks! Letโ€™s get this event-filled Laravel party started! ๐ŸŽ‰๐ŸŽŠ๐Ÿ’ƒ๏ธ๐Ÿ•บ๏ธ

Our network