laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.24k stars 10.92k forks source link

Task Scheduling repeted #13611

Closed it02Inartec closed 8 years ago

it02Inartec commented 8 years ago

Hi, I found the following problem to schedule my tasks:

I followed the documentation and: With sudo crontab -e i added * * * * * php /MiRutaAlProyectoLaravel/artisan schedule:run > /dev/null 2>&1

my kernel.php $schedule->call('App\Http\Controllers\IniciarController@EnviaMensaje')->everyMinute();

My IniciarController.php ` public function EnviaMensaje(Request $request){ $destinos = ""; $campania = \DB::table('campania') ->where('fecha_inicio', date("Y-m-d H:i:00")) ->whereNotNull('grupo_id') ->where('status', 2)->get(); $rows = count($campania);

if($rows>0){
    $token = Token::where('id', $campania[0]->token_id)->first();

    if($scampaign[0]->grupo_id==0){
        $clientes  = \DB::table('clientes')->get();
    }
    else{
        $clientes  = \DB::table('clientes')->where('grupo', $campania[0]->grupo)->get();
    }
    foreach ($clientes as $key => $value) {
        $destinos .= $value->telefono.',';
    }
    $destinos = substr($destinos,0,-1);
    $client = new \GuzzleHttp\Client();
    $response = $client->request('GET', env('API_URL'), [
        'query' => [
            'token'         => $token->token,
            'destinations'  => $destinos,
            'message'       => $campania[0]->body
        ]
    ]);
    $id = $campania[0]->id;
    $campania               = Scampaign::find($id);
    $campania->status  = 4;
    $campania->save();
}

}`

But the function is executed 2 times, so I get 2 times a message in my phone. I want to know that I'm doing wrong or that I lack, I can not make it run only once per minute.

Please, I await your response, thanks.

billmn commented 8 years ago

I haven't found any problem with scheduler, have you checked using "crontab -l" that there is only 1 cron on this project?

For test you can use this code in the "handle" method:

file_put_contents(__DIR__.'/test.txt', date('Y-m-d H:i:s')."\n", FILE_APPEND);

So in app/Console/Commands/test.txt you can see when and how many times the scheduler execute your command.

it02Inartec commented 8 years ago

Excuse me,

This is my kernel.php

namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel {

protected $commands = [ ];

protected function schedule(Schedule $schedule)  {
    $schedule->call('App\Http\Controllers\IniciarController@EnviaMensaje')->everyMinute();
}
}

the "file_put_contents..." within the function "schedule"? or in my IniciarController@EnviaMensaje?

billmn commented 8 years ago

I haven't see that you call a controller method, sorry. Use this code in IniciarController@EnviaMensaje:

file_put_contents(storage_path('app/test.txt'), date('Y-m-d H:i:s')."\n", FILE_APPEND);

and check test.txt file in "storage/app" path

it02Inartec commented 8 years ago

Ok,

1.- vagrant@homestead:~$ sudo crontab -l

2.- I put the line on IniciarController@EnviarMensaje file_put_contents(__DIR__.'/test.txt', date('Y-m-d H:i:s').'\n', FILE_APPEND);

3.- In the file test.txt I see the following things: 2016-05-19 17:23:04\n2016-05-19 17:23:06\n

billmn commented 8 years ago

Can you show me only the output of "crontab -l" ?

it02Inartec commented 8 years ago

Image and video hosting by TinyPic

billmn commented 8 years ago

Seems ok, try to execute "ps -A" and make sure that "crond" isn't running twice

it02Inartec commented 8 years ago

excuse me where execute "ps -A"?

billmn commented 8 years ago

In the terminal

it02Inartec commented 8 years ago

vagrant@homestead:~$ ps -A PID TTY TIME CMD 1 ? 00:00:01 init 2 ? 00:00:00 kthreadd 3 ? 00:00:00 ksoftirqd/0 5 ? 00:00:00 kworker/0:0H 6 ? 00:00:00 kworker/u2:0 7 ? 00:00:01 rcu_sched 8 ? 00:00:00 rcu_bh 9 ? 00:00:00 rcuos/0 10 ? 00:00:00 rcuob/0 11 ? 00:00:00 migration/0 12 ? 00:00:00 watchdog/0 13 ? 00:00:00 khelper 14 ? 00:00:00 kdevtmpfs 15 ? 00:00:00 netns 16 ? 00:00:00 perf 17 ? 00:00:00 khungtaskd 18 ? 00:00:00 writeback 19 ? 00:00:00 ksmd 20 ? 00:00:00 khugepaged 21 ? 00:00:00 crypto 22 ? 00:00:00 kintegrityd 23 ? 00:00:00 bioset 24 ? 00:00:00 kblockd 25 ? 00:00:00 ata_sff 26 ? 00:00:00 md 27 ? 00:00:00 devfreq_wq 28 ? 00:00:00 kworker/u2:1 31 ? 00:00:00 kswapd0 32 ? 00:00:00 fsnotify_mark 33 ? 00:00:00 ecryptfs-kthrea 45 ? 00:00:00 kthrotld 46 ? 00:00:00 acpi_thermal_pm 47 ? 00:00:00 scsi_eh_0 48 ? 00:00:00 scsi_tmf_0 49 ? 00:00:00 scsi_eh_1 50 ? 00:00:00 scsi_tmf_1 55 ? 00:00:00 ipv6_addrconf 75 ? 00:00:00 deferwq 76 ? 00:00:00 charger_manager 117 ? 00:00:00 kpsmoused 119 ? 00:00:00 scsi_eh_2 120 ? 00:00:00 scsi_tmf_2 128 ? 00:00:00 kdmflush 129 ? 00:00:00 bioset 131 ? 00:00:00 kdmflush 134 ? 00:00:00 bioset 187 ? 00:00:00 jbd2/dm-0-8 188 ? 00:00:00 ext4-rsv-conver 296 ? 00:00:00 kworker/0:1H 334 ? 00:00:00 upstart-udev-br 343 ? 00:00:00 ext4-rsv-conver 347 ? 00:00:00 systemd-udevd 379 ? 00:00:00 iprt-VBoxWQueue 410 ? 00:00:00 rpciod 411 ? 00:00:00 nfsiod 415 ? 00:00:00 dbus-daemon 434 ? 00:00:00 rsyslogd 458 ? 00:00:00 systemd-logind 546 ? 00:00:00 rpc.idmapd 654 ? 00:00:00 rpcbind 661 ? 00:00:00 upstart-file-br 664 ? 00:00:00 upstart-socket- 667 ? 00:00:00 rpc.statd 709 ? 00:00:00 dhclient 914 tty4 00:00:00 getty 916 tty5 00:00:00 getty 920 tty2 00:00:00 getty 921 tty3 00:00:00 getty 923 tty6 00:00:00 getty 951 ? 00:00:05 redis-server 953 ? 00:00:00 cron 997 ? 00:00:00 kauditd 999 ? 00:00:00 mysqld_safe 1268 ? 00:00:03 mysqld 1289 ? 00:00:00 nginx 1291 ? 00:00:00 nginx 1519 ? 00:00:00 sshd 1537 ? 00:00:00 postgres 1562 ? 00:00:00 postgres 1563 ? 00:00:00 postgres 1564 ? 00:00:00 postgres 1565 ? 00:00:00 postgres 1566 ? 00:00:00 postgres 1629 ? 00:00:00 beanstalkd 1688 ? 00:00:00 hhvm 1695 ? 00:00:00 memcached 1713 ? 00:00:00 php-fpm7.0 1714 ? 00:00:05 php-fpm7.0 1715 ? 00:00:04 php-fpm7.0 1745 ? 00:00:00 supervisord 1797 ? 00:00:01 VBoxService 1825 tty1 00:00:00 getty 2064 ? 00:00:05 php-fpm7.0 2526 ? 00:00:01 kworker/0:0 3348 ? 00:00:00 sshd 3363 ? 00:00:00 kworker/0:2 3367 ? 00:00:00 sshd 3368 pts/0 00:00:00 bash 3657 pts/0 00:00:00 ps

it02Inartec commented 8 years ago

I use date_default_timezone_set("America/Havana"); in my IniciarController.

I thought it might be the time zone in cron, so I added TZ=America/Havana And I changed on my kernel.php $schedule->call('App\Http\Controllers\IniciarController@EnviaMensaje')->everyMinute()->timezone('America/Havana');

But nothing, 2 message in my phone T.T

billmn commented 8 years ago

Ok seems there is only one cron process. Execute this command to see which users have active crontabs, there should be only one result.

sudo ls /var/spool/cron/crontabs
it02Inartec commented 8 years ago

vagrant@homestead:~$ sudo ls /var/spool/cron/crontabs root vagrant vagrant@homestead:~$

billmn commented 8 years ago

This means you have 2 crontabs running, one for root user and one for vagrant.

Try to remove crontab from all the users and add the crontab again only for vagrant user (don't use sudo with crontab -e)

it02Inartec commented 8 years ago

Perfect! I have searched the Internet how to remove a crontab like you me you've indicated, so:

vagrant@homestead:~$ sudo crontab -r vagrant@homestead:~$ sudo ls /var/spool/cron/crontabs vagrant vagrant@homestead:~$

And now only 1 message in my phone.. :+1: :D

Muchas Gracias! Eres un Genio!! <3

gowthambopathyraj commented 5 years ago

Same problem schedule cron job,

my kernal.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Log;

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\SecondTable::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->call('App\Http\Controllers\Sale\SaleController@sync')->everyMinute();
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

manually run a command-> php artisan schedule:run
working good!

but, cronjob run server not working correctly, my cron job code,

* * * * * php /laravel project folder/artisan schedule:run >> /dev/null 2>&1

not working, please help him.......

deepusodhi76 commented 2 years ago

Hello @billmn I have also this type of issue.

when we send single message its work properly. When we go for send messages to multiple no (1000 sms) and users get at a time 7 to 8 sms to users. i don't know understand can you please help me.

enderandpeter commented 1 year ago

They call me Spencer "2 cron jobs" Williams...