I am doing ERP project in cloud base & I am using one source for multiple customers and using different database.
I want to run script every day at 00:00 with different time-zone
Here what i tried......
This is my Kernel class
protected function schedule(Schedule $schedule)
{
$schedule->command('attendance:null')->hourly();
}
This is my attendance:null command class
class AttendanceInsertNull extends Command{
protected $signature = 'attendance:null';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return \App\Console\Commands\AttendanceInsertNull
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$locations = Location::get();
foreach ($locations as $location) {
$schedule1 = new Schedule();
$schedule1->call(function(){
$timeZone = $location->timezone;
$this->info('RUN');
})->at('00:00')->timezone($timeZone);
}
}
}
What i am doing is, Check with different timezone in location loop.
please anyone can help to solve this issue for me.
Related
So, I want to make a function which will keep updating my product inventory and I need the function to be running continuously as long as the server or application would be running.....
Thanks In Advance!!!
My Expectations:
while (true)
{
$product->quantity += $updated
// This would be running continuously
}
Ok, after some help I got the solution to use laravel commands with task scheduling with the least available running of every one minute.
First, Make a new command:
php artisan make:command ProductQuantityUpdate // Command Name
Second, Locate the ProductQuantityUpdate.php at App\Console\Commands\ProductQuantityUpdate.php and type your desired function in the handle():
Note: You can also edit the command name & description through the:
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'productquantity:update';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Update Product Quantity!';
Full Code [App\Console\Commands\ProductQuantityUpdate.php]:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ProductQuantityUpdate extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'productquantity:update';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Update Product Quantity!';
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$pro = \App\Models\Product::get();
foreach($pro as $product)
{
$qty = 0;
$selecteds = \App\Models\AttributeValue::where('product_id', $product->id)->get();
if($selecteds->count() > 0)
{
foreach($selecteds as $select)
{
$qty += $select->quantity;
}
$product->quantity = $qty;
$product->save();
}
}
$this->info('Product Quantity Updated!');
return Command::SUCCESS;
}
}
Third, Go to App\Console\Kernel.php and add the command, and schedule function:
protected $commands = [
Commands\ProductQuantityUpdate::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('productquantity:update')->everyMinute();
// Check the documentation for available timings!
}
To check your command, type this in the console:
php artisan list
To check your schedule, type this in the console:
php artisan schedule:list
Documentation that also helped me : Couldways documentation
i have mysql table which is having earnings records with date. i want to get total earning on daily ,weekly and monthly basis.
I tried to get these records on basis of date but is confusing.
i want to get these reports on daily , weekly and monthly basis.
You can use the existing Laravel cron job scheduling to fulfill your specific requirement.
Please refer following Laravels official documentation
https://laravel.com/docs/5.8/scheduling#scheduling-queued-jobs
I'll show a simple example to get an idea about this configuration.
php artisan make:command DailyUpdate
above command will create a class component in the following location
app/Console/Commands directory.
Generated class file will be similar to below,
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class DailyUpdate extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'command:name';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
}
}
After that change the following according to your requirement.
protected $signature = 'command:name';
protected $description = ‘Command description’;
Inside the handle() you can place the implementation that you need to repeat on a scheduled basis.
public function handle() {
// implementation
}
After all the changes, you need to register that command.
In app/console/kernal.php change the configuration as follows,
protected $commands = [
Commands\DailyUpdate::class
];
protected function schedule(Schedule $schedule)
{
$schedule->command('hour:update')
->daily();
}
i have created following command in laravel using command php artisan make:command RedisDataListener
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RedisDataListener extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'listen:subscribe';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Subscribe to redis channel for listening events';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle() {
$value = cache()->has('tracking_application:mykey') ? cache()->get('tracking_application:mykey') : 'no';
print_r(array(
$value
));
}
}
but when i'm using cache()->get('key') it is not working. Same function works fine when i use in controller.
i'm using redis as the cache server.
also tried with cache::get() but nothing happen.
I'm using Laravel 5.3.26 and cannot set the scheduler to run automatically although i have the cron job ready.
I' ve created a new command ligmena:update, below is the code:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
class ligmena extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'ligmena:update';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Update';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle() {
//
$today = strtotime("now");
$energa_symvolaia = DB::table('symvolaia')->where('eidos_kinisis', '1')->get();
foreach ($energa_symvolaia as $es) {
$imerominia_lixis = strtotime(str_replace("/", "-", $es->imerominia_lixis));
if ($today > $imerominia_lixis)
DB::table('symvolaia')->where('id', '<', $es->id)->update(['eidos_kinisis' => '4']);
}
}
}
?>
Below is the code of Kernel.php
<?php
namespace App\Console;
use DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel {
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
\App\Console\Commands\ligmena::class,
//
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule) {
// $schedule->command('inspire')
// ->hourly();
$schedule->command('ligmena:update')->everyMinute();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands() {
require base_path('routes/console.php');
}
}
?>
I've setup the cron job like this:
php /home/site.com/public_html/testdemo/artisan schedule:run >> /dev/null 2>&1
and it runs every minute.
If I run the command manually it works fine.
Any suggestions?
I 've solved the issue using raw mysql.
The cron job was running fine and the code was ok but it was not changing the DB.
After i changed to raw mysql it worked fine
I have registered my command as php artisan run:process and it looks like
class queuedTransaction extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'run:process';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Running the queued transaction.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle(){
DB::table('transfers')->truncate();
}
}
this is my kernel.php
class Kernel extends ConsoleKernel
{
protected $commands = [
\App\Console\Commands\Inspire::class,
\App\Console\Commands\queuedTransaction::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('run:process')->everyMinute();
}
}
Now I want to run php artisan run:process every minute and truncate transfers table, but the cron is not working now. Just to make sure, if the command itself is working or not , I put my command inside my routes and call it, its truncating the table meaning command is doing its work.
In Kernel.php your command should be included in:
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
MyCommand::class
];
You can probably remove Inspire too :)