2

I am developing a web portal using Laravel-5.8. Also, I am using guzzlehttp/guzzle-6.3 to consume an external api and save it in my local database.

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=portal
DB_USERNAME=testing
DB_PASSWORD=password

app\Console\Commands\travelupdate.php

class travelsupdate extends Command {

    protected $signature = 'command:travelsupdate';

    protected $description = 'travelsupdate';

    public function __construct() {
        parent::__construct();
    }
    public function handle() 
    {
    try{
         $client = new Client();

        $res = $client->request('GET','https://api.abcdef.net/travels/v4/sample');
        $trips = json_decode($res->getBody());

        foreach($trips as $trip) {
            Trip::updateOrCreate([
                'trip_id' => $trip['trip_id']  
            ],
            [
                'trip_number' => $trip['trip_no'],
                'truck_no' => $trip['t_no'], 
                'truck_reg_no' => $trip['reg_no'],
                'trailer_no' => $trip['trailer_no'],
                'contract_no' => $trip['contract_no'],
                'contract' => $trip['contract_name'],
                'driver_id' => $trip['driver_id'],
                'driver_name' => $trip['driver_name'],
                'loading_date' => date_format($trip['loading_date'], "Y-m-d"),
                'loading_from' => $trip['loading_from']
            ]);
        } 
        } catch (\Exception $e){
            \Log::error($e->getMessage());
        }             
    }
}

app\Console\Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\User;
use App\Activity;
use Avatar;
use Storage;
use Mail;
use App\Travel;
use App\Audit;
use Carbon\Carbon;
// use \Carbon\Carbon;

class Kernel extends ConsoleKernel
{
    protected $commands = [
        'App\Console\Commands\travelsupdate',
    ];

    protected function schedule(Schedule $schedule)
    {
         $schedule->command('command:travelsupdate')
            ->hourly();
    }

    protected function commands()
    {
        // $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

What I want to achieve is that consume the external api using Guzzle GET request. Then save it into the local database. If data already exists (using trip_id), it updates. I set the cron job to every one hour.

I observe that nothing is happening, and no data is being saved to the local database.

When I checked the log, I found this error:

[2019-11-04 10:15:18] local.ERROR: file_put_contents(C:\xampp\htdocs\portal\storage\framework/cache/data/b8/55/b85503ed5c2bc216ecbc86c82005e92d6a01c97f): failed to open stream: Permission denied {"userId":3,"exception":"[object] (ErrorException(code: 0): file_put_contents(C:\\xampp\\htdocs\\portal\\storage\\framework/cache/data/b8/55/b85503ed5c2bc216ecbc86c82005e92d6a01c97f): failed to open stream: Permission denied at C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Filesystem\\Filesystem.php:122)
[stacktrace]
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'file_put_conten...', 'C:\\\\xampp\\\\htdocs...', 122, Array)
#1 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Filesystem\\Filesystem.php(122): file_put_contents('C:\\\\xampp\\\\htdocs...', '1572862577i:157...', 2)
#2 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Cache\\FileStore.php(65): Illuminate\\Filesystem\\Filesystem->put('C:\\\\xampp\\\\htdocs...', '1572862577i:157...', true)
#3 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Cache\\Repository.php(211): Illuminate\\Cache\\FileStore->put('77de68daecd823b...', 1572862577, 60)
#4 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Cache\\Repository.php(316): Illuminate\\Cache\\Repository->put('77de68daecd823b...', 1572862577, 60)
#5 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Cache\\RateLimiter.php(60): Illuminate\\Cache\\Repository->add('77de68daecd823b...', 1572862577, 60)
#6 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\ThrottleRequests.php(57): Illuminate\\Cache\\RateLimiter->hit('77de68daecd823b...', 60)
#7 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Routing\\Middleware\\ThrottleRequests->handle(Object(Illuminate\\Http\\Request), Object(Closure), 60, '1')
#8 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\Middleware\\Authenticate.php(43): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#9 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Auth\\Middleware\\Authenticate->handle(Object(Illuminate\\Http\\Request), Object(Closure), 'api')
#10 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#11 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(682): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#12 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(657): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#13 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(623): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#14 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(612): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#15 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#16 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(130): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#17 C:\\xampp\\htdocs\\portal\\app\\Http\\Middleware\\CORS.php(18): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#18 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): App\\Http\\Middleware\\CORS->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#19 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#20 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#21 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#22 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#23 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#24 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#25 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php(62): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#26 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#27 C:\\xampp\\htdocs\\portal\\vendor\\fideloper\\proxy\\src\\TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#28 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(171): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#29 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(105): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#30 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#31 C:\\xampp\\htdocs\\portal\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#32 C:\\xampp\\htdocs\\portal\\index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#33 {main}
"} 
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

From the code above in index.php, this is line 55 in index.php

$request = Illuminate\Http\Request::capture()

How do I resolve it?

ayobamilaye
  • 429
  • 2
  • 10
  • 25
  • 1
    That looks like part of the stack trace, and not the error itself. Was there more than that in the log? – Brian Thompson Nov 04 '19 at 16:50
  • `index.php` shouldn't even be involved since you are running a console command which doesn't use the Http Kernel – lagbox Nov 04 '19 at 16:53
  • I have added the whole log file to my post. What could be the problem and how do I resolve it and make the cron job schedule to work? – ayobamilaye Nov 05 '19 at 07:52
  • @BrianThompson - I have added the whole log file to my post. What could be the problem and how do I resolve it and make the cron job schedule to work? – ayobamilaye Nov 05 '19 at 11:31

1 Answers1

0

It looks like your storage directory is not writable. This could be caused by a few things.

You could first check the permission level of the directory. I'm not very familiar with xampp and how it works, but on linux you'd want it set to something like 775. There's a pretty through explanation of permissions set up in this answer.

The next piece would be to determine what user is being used while running the cron. If your app is running fine outside of the cron functions, this may be the issue. For example, if you set up your cron as the user1, but the folder belongs to user2, you will not be able to write to it because the folder does not belong to user1.

Brian Thompson
  • 13,263
  • 4
  • 23
  • 43