I want to run my cron job in a specific time for example I want it to run it at
12:31:01
Is it possible?
Here is my code:
$schedule->command('UpdatePriceStatus:pricestatus')->weekdays()->dailyAt('12:31');
The documents in laravel doesn't include the seconds. But would it be possible if I do this:
$specificTime = '123101'
$currentTime = date('His');
$schedule->command('UpdatePriceStatus:pricestatus')->weekdays()->daily()->when(function () {
if ($specificTime == $currentTime) {
return true;
}
});
or this
$schedule->command('UpdatePriceStatus:pricestatus')->weekdays()->dailyAt('12:31:01');
Related
I have a question about the task scheduling in laravel framework. I already have defined the commands currency:update and currency:archive in the list of console commands. Now, I want to run these two commands in the schedule method but with the following condition:
if this is a time to run the currency:archive command, don't run the other command currency:update until the previous command (i.e. currency:archive) ends; otherwise run the currency:update command.
This is my current code in schedule method:
$schedule->command('currency:archive')
->dailyAt('00:00')
->withoutOverlapping();
$schedule->command('currency:update')
->everyMinute()
->withoutOverlapping();
How should I modify it?
Thanks.
In the laravel schedule docs the following two features are mentioned:
Truth test contraints docs
$schedule->command('emails:send')->daily()->skip(function () {
return true;
});
Task hooks docs
$schedule->command('emails:send')
->daily()
->before(function () {
// Task is about to start...
})
->after(function () {
// Task is complete...
});
You could consider setting a variable like $achriveCommandIsRunning to true in the before() closure. In the skip() closure you can return $archiveCommandIsRunning; and in the after() closure you can set the variable back to false again.
Thanks,
I modified my code as you said and it works now.
Here is the final code:
$isArchiveCommandRunning = false;
$schedule->command('currency:archive')
->dailyAt('00:00')
->before(function () use (&$isArchiveCommandRunning) {
$isArchiveCommandRunning = true;
})
->after(function () use (&$isArchiveCommandRunning) {
$isArchiveCommandRunning = false;
})
->withoutOverlapping();
$schedule->command('currency:update')
->everyMinute()
->skip(function () use (&$isArchiveCommandRunning) {
return $isArchiveCommandRunning;
})
->withoutOverlapping();
I need to switch off ConfirmableTrait in Laravel 5.
On my production server I have many jobs, some of them prompt alert "Do you really wish to run this command?" and for that reason job is failed.
Thanks in advance.
edit ConfirmableTrait like this
public function confirmToProceed($warning = 'Application In Production!', $callback = null)
{
return true;
}
second way override confirmToProceed method add in your job or create new trait file and use them
public function confirmToProceed($warning = 'Application In Production!', $callback = null)
{
return true;
}
I need to start a game every 30 seconds, cron job minimum interval is a minute, so I use queue delay to do it
app/Jobs/StartGame.php
public function handle()
{
// Start a new issue
$this->gameService->gameStart();
// Start new issue after 15 seconds
$job = (new \App\Jobs\StartGame)->onQueue('start-game')->delay(30);
dispatch($job);
}
And I start first game by console
app/Console/Commands/StartGame.php
public function handle()
{
$job = (new \App\Jobs\StartGame)->onQueue('start-game');
dispatch($job);
}
The question is, I want to use cron job to check if the start game queue is running, if not then dispatch, in case of something like server stop for maintenance, is it possible?
You can write a log of the "start-date" in a file, for example : data_start.log
1- every time gameStart() is called the content of file (date) is checked, and verified if is between 29sec and 31sec.
2- update the content of file.
example of write :
<?php
$file = "data_start.log";
$f=fopen($file, 'w');
fwrite( $f, date('Y-m-d H:i:s') . "\n");
?>
I have created a background job like this:
Parse.Cloud.job("ResetLeaderboard",
function(request, response)
{
Parse.Cloud.useMasterKey();
var query = new Parse.Query("Leaderboard");
query.find(
{
success: function(results)
{
response.success("Success!");
},
error: function(error)
{
response.error(error);
}
})
.then(
function(results)
{
return Parse.Object.destroyAll(results);
});
});
I want to run this job every 15 days. But there is no option available at www.parse.com to set time interval for more than a day.
I think I need to use a time stamp and compare that value with current time. Can somebody show me the standard way to do this?
You're right that the job scheduling UI is constrained to a single day. The way to solve the problem is to have the job run daily, but to have it do nothing on 14 out of 15 runs. Those do-nothing runs will be wasteful, but microscopically so, and parse is paying the bills anyway.
The specifics of the solution depend on specific requirements. If you require maximum control, like exactly 15 days down to the millisecond, starting at a millisecond-specific time, you'd need to create some scratch space in the database where state (in particular, date) from the prior run is kept.
But the job looks like a cleanup task, where the requirement of "very nearly 15 days, beginning within 15 days" is sufficient. With that simpler requirement, your intuition is correct that simple date arithmetic will work.
Also, importantly, it looks to me like your intention is to find several objects in need of deletion, then delete them. The posted logic doesn't quite do that. I've repaired the logic error and cleaned up the promise handling as well...
// Schedule this to run daily using the web UI
Parse.Cloud.job("ResetLeaderboard", function(request, response) {
if (dayOfYear() % 15 === 0) {
var query = new Parse.Query("Leaderboard");
query.find().then(function(results) {
Parse.Cloud.useMasterKey();
return Parse.Object.destroyAll(results);
}).then(function() {
response.success("Success!");
}, function(error) {
response.error(error);
});
} else {
response.success("Successfully did nothing");
}
});
function dayOfYear() {
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
}
The dayOfYear function is thanks to Alex Turpin, here
I want to schedule a job class that checks if a boolean var changed to true or , which is initially not set to any value, using cron expression every night at sometime(say 1'o clock).The scheduler should quit the job if var is set to true or false, otherwise continue running the job at schedule for the max of 15 days & then set it to true automatically. I think IoC container pattern is suitable to do this. Please provide a brief picture of the whole code to implement this.
Spring has built-in scheduling capabilities. While the full implementation is in your court, here is an example of a scheduled method, in this case for 1AM every day:
private Boolean scheduleToggle = null;
#Scheduled(cron = "0 0 01 * * ?")
public void myScheduledJob() {
if(scheduleToggle != null) {
return;
} else {
// run the job
scheduleToggle = true;
return;
}
}
For a full explanation and configuration details, see: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/scheduling.html