为什么Cron调度程序在laravel中不起作用?

时间:2018-09-12 16:04:53

标签: php laravel laravel-5

注意: 我正在使用laravel 5.0

我已经执行了该通用命令“ * * * * * php / path / to / artisan schedule:run >> / dev / null 2>&1”,如laravel文档中所述,但cron仍无法在我的内核文件中运行:

<?php namespace App\Console;

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\InstagramAutopost',
];

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
    $schedule->command('instagramautopost')
             ->everyFiveMinutes()
             ->withoutOverlapping();
}
}

这是我在Commands中的InstagramAutopost.php文件:     

use App\Export;
use App\Insta;
use Auth;
use Response;
use App\Vehicle;
use View;
use Instagram;
use App\Libraries\UserPreferences;
use Illuminate\Console\Command;
use App\Libraries\Info;
use Validator;
use Illuminate\Support\Facades\URL;

class InstagramAutopost extends Command {

 /**
 * The console command name.
 *
 * @var string
 */
protected $name = 'instagramautopost';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Instagram Autopost';

/**
 * Execute the console command.
 *
 * @return mixed
 */

 public function handle() {

    $vehicles_id = Vehicle::where(['user_id' => Auth::id()])->get();
    $numVehicles = count($vehicles_id);

    for ($i=0; $i < $numVehicles ; $i++) {
        $vExport = Export::where(['v_id' => $vehicles_id[$i]['id']])->first();

        if (!$vExport || ($vehicles_id[$i]['last_modified'] > $vExport['instagram_date'])) {
            $settings = \App\Insta::where(['user_id' => Auth::id()])->first();
            if($settings){
                $vInfo = Vehicle::where(['id' => $vehicles_id[$i]['id']])->first();

                $img = $this->get_main_photo($vInfo['photos'], 400, 300, V12_IMAGES_URL . '/' . Auth::user()->photos_directory . '/' . $vInfo['id'] . "/");
                if($img && getimagesize($img) ){

                    $price = ($vInfo['internet_specials']=='yes' and $vInfo['featured_price']!=0)?$vInfo['featured_price']:$vInfo['price'];
                    $msg = $vInfo['year'].' '.$vInfo['make_name'].' '.$vInfo['model'].' $'.$price;

                    /////// CONFIG ///////
                    $username = $settings->username;
                    $password = $settings->password;

                    $debug = false;
                    $photo = $img ;
                    $info  = new Info(Auth::id(), $vehicles_id[$i]['id']);
                    $caption = $msg.' '.$info->getDomain() . "/inventory/view/" . $vehicles_id[$i]['id'];    

                    $instagrame = new Instagram($username, $password, $debug);

                    //Login
                    try {
                        $instagrame->login();

                    } catch (InstagramException $e) {
                        exit();
                    }

                    //Upload photo
                    try {

                        $instagrame->uploadPhoto($img, $caption);

                        //update exports
                        $exp = Export::where(['v_id' => $vehicles_id[$i]['id']])->first();
                        if(!$exp){
                            $exp = new Export;
                            $exp->v_id = $vehicles_id[$i]['id'];
                        }
                        $exp->instagram = 'yes';
                        $exp->instagram_date = date('Y-m-d H:i:s');
                        $exp->save();


                    } catch (Exception $e) {

                    }   
                }

            }
        }
    }
}

/**
* Get the main photo of a vehicle
*
* @return Response
*/
public function get_main_photo($photos, $width, $height, $path = '') {
    if ($photos != '') {
        $ar_photos = unserialize($photos);
        for ($i = 0; $i < count($ar_photos); $i++) {
            if ($ar_photos[$i]['main'] == 'yes') {
                return (empty($width) ? $path . $ar_photos[$i]['photo'] : $path . str_ireplace('.jpg', '_' . $width . $height . '.jpg', $ar_photos[$i]['photo']));
            }
        }
    }
    return '';
    }

 }

我想要如果有什么问题。

1 个答案:

答案 0 :(得分:2)

您的InstagramAutopost.php(如上所示)不是有效的Command

该类需要扩展Command而不是Controller并实现handle()方法以及其他一些必需的属性。

您可以使用php artisan make:command创建适当命令的外壳,然后将其复制粘贴到其中。