簡體   English   中英

為什么Cron調度程序在laravel中不起作用?

[英]Why does Cron scheduler not working in laravel?

注意: 我正在使用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();
}
}

這是我在命令中的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 '';
    }

 }

我想要如果有什么問題。

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

該類需要擴展Command而不是Controller並實現handle()方法,以及一些其他必需的屬性。

您可以使用php artisan make:command創建適當命令的外殼,然后將其復制粘貼到其中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM