Search code examples
phplaravelgoogle-app-enginecronlaravel-artisan

Laravel Task Scheduling on Google App Engine


Is there any way to execute php artisan commands on App Engine? I need to set up Laravel's Task Scheduling which requires the following cron job to be set up:

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

but as I looked at cron.yaml documentation, there is no method to execute php file, there seems to be support for only HTTP call to URL.

Can anyway help me with this please?

UPDATE 1 Please do not suggest to call Artisan::handle($command) from within the controller. I need exactly PHP-CLI version of artisan


Solution

  • I guess there were no suggestions, I've done this by writing this in web routes

    Route::get('scheduler', ArtisanController@handle)->middleware('app-engine-cron');
    

    this in ArtisanController

    class ArtisanController extends Controller
    {
        public function handle()
        {
            shell_exec('php '.base_path('artisan').' schedule:run > /dev/null 2>/dev/null &');
        }
    }
    

    and this middleware

    class AppEngineCronMiddleware
    {
    
        public function handle($request, Closure $next)
        {
            if (!$request->hasHeader('X-Appengine-Cron')) {
                return response()->json(trans('auth.unauthorized'), 401);
            }
    
            return $next($request);
        }
    }
    

    And finally cron.yaml like this

    cron:
    - description: "Laravel Scheduler"
      url: /scheduler
      schedule: every 1 mins
      target: default
    

    Anyone having this issue, can try this, it is working for me as expected. Anyways other suggestions are very welcomed. Especially from Google