Cron with wget and flock - laravel

I run a php function through laravel controller to process some data from database A and insert it to database B.
public function process_data()
{
$selectA = DB::connection('mysql')->select('select * from A where processed is null');
foreach ($selectA as $data) {
// process code here
$insertB = DB::connection('mysql')->insert('insert into B .... ');
$updateA = DB::connection('mysql')->update('update A set processed = 1 where id = ?', [$data->id]);
}
}
I thought this cron will execute the function once every minute and flock would prevent multiple executions but I found out that there are duplicated entries in database B.
What's wrong with my cron command below?
* * * * * /usr/bin/flock -w 0 /home/ubuntu/cron1.lock wget -O - http://mywebsite.com/process_data &> /dev/null

Related

InvalidArgumentException : Invalid CRON field value 30 at position 4

I tried To run this command php artisan schedule:run on laravel project I got this error
InvalidArgumentException : Invalid CRON field value 30 at position 4
at /var/www/vendor/dragonmantank/cron-expression/src/Cron/CronExpression.php:155
151| */
152| public function setPart($position, $value)
153| {
154| if (!$this->fieldFactory->getField($position)->validate($value)) {
> 155| throw new InvalidArgumentException(
156| 'Invalid CRON field value ' . $value . ' at position ' . $position
157| );
158| }
159|
this is command on my Kernel
protected function schedule(Schedule $schedule)
{
// Schedule to delete old messages every old days
$schedule -> command(DeleteOldMessages::class, ['days' => config('marketplace.days_old_messages')])
->days(config('marketplace.days_old_messages'));
// Make the command for releasing purchases runs each X days
$schedule -> command(ReleasePurchasesCommand::class, ['days' => config('marketplace.days_old_purchases')])
->days(config('marketplace.days_old_purchases'));
// Run completing command for purchases every defined number of days
$schedule -> command(CompletePurchaseCommand::class) -> days(config('marketplace.days_complete'));
}
I dont know what issue is
Thanks in advance for any help.
The scheduler ->days() function is expecting an array of days of the week on which the task should run. For instance ([1,3,5]) meaning run on Monday,Wednesday and Friday)
I assume with values of 20 and 30, you are expecting the tasks to run at a specified interval. This is not possible with cron based scheduling.
What you probably want is to run the tasks every day and within the task, determine if any records exceed the specified limits.

Git Bash console output breaks down when using Laravel Console choice method

I've a little problem with my console. When I use choice method from Laravel Console, the output totally breaks down. I wanted to google this but I can't find any solution. It's really annoying.
Anyone know how to fix this?
I'm using Git Bash provided by Git for Windows.
That Git bash is an emultion of unix console and it makes me able to use unix like commands. I don't want to stop using this.
Here is the console output
Give your group a name:
Something
Give your group a description (blank for skip):
>
Do you want to assign scopes to your new group? (y/n):
y
Select existing scope name: [0] Random [1] Exit (It's not a
scope)
0 0?[K
?[32mSelect existing scope name?[39m: [?[33m0?[39m] Random
[?[33m1?[39m] Exit (It's not a scope)
>
And the php code as below.
$name = $this->ask('Give your group a name');
$description = $this->ask('Give your group a description (blank for skip)');
$groups = app()->make(ScopeGroupRepository::class);
/** #var ScopeGroup $group */
$group = $groups->perform(new Create($name, $description));
$willCreateScopes = $this->answerToBoolean(
$this->ask('Do you want to assign scopes to your new group? (y/n)')
);
if(!$willCreateScopes) {
return $this->displayCreatedGroupInfo($group);
}
$scopes = app()->make(ScopeRepository::class);
/** #var Collection $unassigned */
$unassigned = $scopes->perform((new ShowAllUnassignedToGroup())->setGroupId($group->id));
if($willCreateScopes) {
do {
$scopes = $unassigned->map(function (Scope $scope){
return $scope->id;
})->toArray();
array_push($scopes, 'Exit (It\'s not a scope)');
// Here it breaks down
$selected = $this->choice('Select existing scope name', $scopes);
} while($selected !== 'Exit (It\'s not a scope)');
}
Thanks for help.

Scheduled task with parameter laravel

in Laravel 5.6 i have a controller that invokes a command through a Process component.
The command run fine, is about a compression of a folder with the name the user gives.
$command = 'tar -czvf '.$nameFile.' storage/images/';
$process = new Process($command);
$process->setTimeout(1800);
$process->run();
I need to schedule that job 3 times a day. I saw task scheduling with file app/Console/Kernel.php; the problem is to execute the process with the file name the user gives.
How can i programm a Schedule task in this situation?
Thanks
Assume you run the schedule tasks three times a day, per each user input. You can create a eloquent model for such purpose, says ProcessTask, and save the required data in Controller:
ProcessTask::create(['user_id' => $userId, 'name_file' => $nameFile]);
Then, you can make scheduling task as:
$schedule->call(function () {
$tasks = ProcessTask::all();
foreach ($tasks as $task) {
$command = 'tar -czvf '.$task->name_file.' storage/images/';
....
}
})->hourly()
->when(function() { return date('H') % 8 == 0; }) // run tasks at 00:00, 08:00, 16:00
->name('processTask')
->withoutOverlapping();
If the scheduling tasks are time-consuming, the best practice is to dispatch tasks to queue and let workers consume it.

Laravel Cron not auto run (Windows)

I use Cron (https://github.com/liebig/cron) for Laravel 4 to run specific task in specific time
I have this code in my Global.php
Event::listen('cron.collectJobs', function() {
Cron::add('example1', '* * * * *', function() {
$trip = new TripReminder();
$trip->checkDateAndSendEmail();
});
Cron::setRunInterval(1);
$report = Cron::run();
print_r ($report);
});
=>This Code use to Send an email to the customers in every minute (just demo and check)
in cmd, i run this command "php artisan cron:run"
It can run well, the email was sent also, but, it only run 1 times.
I don't know what i'm missing.
Please help !
Thank you !

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