Laravel : Task Scheduling [ In Parallel ] - laravel

I have multiple tasks that need to be done every hour or two. All of them have been scheduled via Laravel using below comamnds as cron jobs
$schedule->command('email:notifications1')
->cron('15 * * * * *');
$schedule->command('email:notifications2')
->cron('15 * * * * *');
$schedule->command('email:notifications3')
->cron('15 * * * * *');
Issue:
All of the above tasks are pretty time consuming & it seems from the results that these tasks are not running in parallel. And each tasks runs after the previous has ended.
Requirment
How can i run them in parallel? I want all tasks to be executed (in parallel) as soon as the clock tick the specified time.
Laravel Version 5

You can easily have multiple parallel commands running if you add runInBackground() to the chain.
Like this:
$schedule->command('email:notifications1')
->cron('15 * * * * *')->runInBackground();
$schedule->command('email:notifications2')
->cron('15 * * * * *')->runInBackground();
$schedule->command('email:notifications3')
->cron('15 * * * * *')->runInBackground();
This creates a new process in the background so the Scheduler doesn't have to wait until the command executes. This doesn't even interfere with the withoutOverlapping() method because that works with mutex files.
Now you also have the benefit of having your commands in version control.

The Laravel scheduler can only run one command at a time because of the limitations of PHP.
You could, however, add the cronjobs directly in your crontab file, this way, they will be executed in parallel in separate processes.
15 * * * * * php /path/to/artisan email:notifications1
15 * * * * * php /path/to/artisan email:notifications2
15 * * * * * php /path/to/artisan email:notifications3
Another way to fix this is to let the jobs start at another time. Because a new php process is started every minute by the cron job, these do not affect each other.
For example:
$schedule->command('email:notifications1')
->cron('5 * * * * *');
$schedule->command('email:notifications2')
->cron('10 * * * * *');
$schedule->command('email:notifications3')
->cron('15 * * * * *');

Related

laravel migration error while creating new one

i tried creating new migration and in my migration file i got the following text
/**
* Gets the first element of `array`.
*
* #static
* #memberOf _
* #since 0.1.0
* #alias first
* #category Array
* #param {Array} array The array to query.
* #returns {*} Returns the first element of `array`.
* #example
*
* _.head([1, 2, 3]);
* // => 1
*
* _.head([]);
* // => undefined
*/
function head(array) {
return (array && array.length) ? array[0] : undefined;
}
module.exports = head;
instead of the normal migration code,
i tried installing the composer , composer dump-autoload
tried clearing cache
but still no luck , it was not like this earlier as i have created many migrations and they were working fine but suddenly it gave me this error,
any idea what is causing this error

SpringBoot Scheduler Cron over running

is there any expert having the issue using springboot scheduler
trying to set it to run between 2pm til 10pm on weekday every 15mins/per hour, but it seem like trigger by minute, is that because my cron is wrong or i shld do smthg to control it ?
running in linux server via springboot-web-started
#Scheduled(cron = "0 15 14-22 * * MON-FRI")
private void fireDownload() {
log.info("fireDownload");
this.jmsXXXX.run(Constants.XXXX);
}
version
spring-boot 2.4.2
java 11
Please try this
#Scheduled(cron = "0 */15 14-22 * * MON-FRI")
You say in a comment that this is not working, so let's test this with a simple proof-of-concept that fires every 5 minute
#Scheduled(cron = "0 */5 8-22 * * MON-FRI")
private void cronPOC() {
log.info("cronPOC triggered by cron");
}
Screen-shot below shows that the POC is indeed working.
While we're at testing, let's put #GerbenJongerius suggestion from comment above to the test as well (with some tiny changes in order to speed things up).
#Scheduled(cron = "0 0/5 8-22 ? * MON-FRI")
private void cronPOC() {
log.info("cronPOC triggered by cron v2");
}
... and this is also working
Some Spring cron examples with explanations here:
https://stackoverflow.com/a/26147143/14072498

How to cronjob task schedule every 40minutes?

How to run 1 job every 40 minutes or 45 minutes in laravel. I have read the laravel documentation with the cron('') function that only performs the correct action for the time specified in cron(''), it does not have to be repetitive after a while.
You can do it by creating 3 crons in the Scheduler:
...->cron('00,45 */3 * * *');
...->cron('30 1-22/3 * * *');
...->cron('15 2-23/3 * * *');
Adapted from: https://www.linuxquestions.org/questions/linux-enterprise-47/cron-every-45-minutes-4175501255/page2.html for laravel

spring redis increment

i want to substitute memcached for spring-redis.
memcached has function
* #param key key
* #param delta
* increment delta
* #param initValue
* the initial value to be added when value is not found
* #param timeout
* operation timeout
* #param exp
* the initial vlaue expire time, in seconds. Can be up to 30
* days. After 30 days, is treated as a unix timestamp of an
* exact date.
* #return
* #throws TimeoutException
* #throws InterruptedException
* #throws MemcachedException
*/
long incr(String key, long delta, long initValue, long timeout, int exp)
throws TimeoutException, InterruptedException, MemcachedException;
spring-redis has function
Long increment(K key, long delta);
how can i set operation timeout(not expire) in spring-redis?
spring-redis doesnt allow you to configure timeout per individual request (as in memcached realization). You can configure timeout per connection with a configuration setting in application.properties:
spring.redis.timeout=5000

Multiple cronjob emails

I have 3 jobs in my crontab. I want to recieve emails if only 1 of them fails and not for other two. Is there any way to restric emails to one type of cronjob?
Redirect the output of the two you don't care about to /dev/null if you don't ever want to see the output or to some file if you do.
Your cron likely supports this:
# This job produces mail.
* * * * * echo Hello
# These jobs do not.
MAILTO=
* * * * * echo Foo
* * * * * echo Bar

Resources