cooperl22 / laravel-db2

laravel-db2 is a simple DB2 service provider for Laravel. It provides DB2 Connection by extending the Illuminate Database component of the laravel framework.
Other
59 stars 65 forks source link

could not find driver {"exception":"[object] (PDOException(code: 0): #38

Closed gbryant200 closed 6 years ago

gbryant200 commented 6 years ago

I've setup a scheduled task to run every 5 minutes to update query results in redis cache.

app\Console\Kernel.php

namespace App\Console;

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

protected function schedule(Schedule $schedule)
    {
        $schedule->job(new QueryLoadsJob())->everyFiveMinutes();
    }

QueryLoadsJob.php

namespace App\Jobs;

use App\Repositories\LoadRepositoryEloquent;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class QueryLoadsJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @param LoadRepositoryEloquent $load_repository_eloquent
     *
     * @return void
     */
    public function handle(LoadRepositoryEloquent $load_repository_eloquent)
    {
        $load_repository_eloquent->getLoads();
    }
}

My LoadsRepositoryEloquent.php

namespace App\Repositories;

use Illuminate\Support\Facades\Cache;
use Redis;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Repositories\LoadRepository;
use App\Models\Load;
use App\Validators\LoadValidator;

class LoadRepositoryEloquent extends BaseRepository implements LoadRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Load::class;
    }

    /**
    * Specify Validator class name
    *
    * @return mixed
    */
    public function validator()
    {

        return LoadValidator::class;
    }

    public function getLoads() {

        $loads = Cache::remember('loads_waiting_cache', 5, function ()
        {
            return $this->model::doors()
                                 ->scheduled()
                                 ->admitted()
                                 ->notRejected()
                                 ->notReturned()
                                 ->stillHere()->get();
        });
            return $loads;

    }
}

my model Loads.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use Carbon\Carbon;
use App\Scopes\gblGateScope;
use App\Scopes\gblLimitScope;
use App\Scopes\gblMillScope;

class Load extends Model implements Transformable
{
    use TransformableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    public $connection = 'QISDB';

    protected $table = 'USERID.SHIPCAR'; 
    protected $primarykey = null;
    public $incrementing = false;
    public $timestamps = false;

    protected static function boot() {
        parent::boot();

        // sets the SHPCAR_MILL to M for all queries
        static::addGlobalScope(new gblMillScope());

        // sets the SHPCAR_GATE_LOC to W for all queries
        static::addGlobalScope(new gblGateScope());

        // sets the limit to 1000 just in case
        static::addGlobalScope(new gblLimitScope());
    }

    /**
     *  Door scope for door numbers to be included in query.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeDoors($query){
        return $query->wherein('USERID.SHIPCAR.SHPCAR_DOOR1',['77', '78', '166', '167', '185A', '191']);
    }

    public function scopeAdmitted($query){
        return $query->where('USERID.SHIPCAR.SHPCAR_LOAD_ADMT_D','<>','9999-01-01');
    }

    public function scopeStillHere($query){
        return $query->where('USERID.SHIPCAR.SHPCAR_COMP_LD_DT','=','9999-01-01');
    }

    public function scopeScheduled($query){
        $startTime = Carbon::now()->subHour(36)->format('Y-m-d');
        $endTime = Carbon::now()->addHour(12)->format('Y-m-d');
        return $query->whereBetween('USERID.SHIPCAR.SHPCAR_SCHD_PU_D',[$startTime, $endTime])
                     ->where('USERID.SHIPCAR.SHPCAR_SCHD_PU_D','<>','9999-01-01');
    }

    public function scopeNotRejected($query){
        return $query->where('USERID.SHIPCAR.SHPCAR_REJECT_DT','=','9999-01-01');
    }

    public function scopeNotRescheduled($query){
        return $query->where('USERID.SHIPCAR.SHPCAR_RESCHD_DT','=','9999-01-01');
    }

    public function scopeNotReturned($query){
        return $query->where('USERID.SHIPCAR.SHPCAR_RETURN_DT','=','9999-01-01');
    }
}

DB2 connection setup

'QISDB' => [
            'driver' => 'db2_ibmi_ibm', // pdo driver
            'username' => 'xxxxx',
            'password' => 'password',
            'database' => 'QISDB',
            'prefix' => '',
            'schema' => 'USERID',
            'options' => [
                PDO::ATTR_CASE => PDO::CASE_LOWER,
                PDO::ATTR_PERSISTENT => false, 
                   ]
        ],

If I call the repository's getLoads() from a controller, the results come back fine. Running the QueryLoadsJob results in a 'could not find driver {"exception":"[object] (PDOException(code: 0): could not find driver at /var/www/ep2/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:68)'

Do I need to inject the model somewhere so the Job can find the database connection? Why would the connection work in the controller and not from the job? Any ideas (or a workaround)? Thanks.

First 10 items in stack trace:

[stacktrace]
#0 /var/www/ep2/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(68): PDO->__construct('ibm:QISDB', 'xxxxx', 'xxxxx', Array)
#1 /var/www/ep2/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(44): Illuminate\\Database\\Connectors\\Connector->createPdoConnection('ibm:QISDB', 'xxxx', 'xxxxxx', Array)
#2 /var/www/ep2/vendor/cooperl/laravel-db2/src/Connectors/DB2Connector.php(24): Illuminate\\Database\\Connectors\\Connector->createConnection('ibm:QISDB', Array, Array)
#3 /var/www/ep2/vendor/cooperl/laravel-db2/src/DB2ServiceProvider.php(81): Cooperl\\Database\\DB2\\Connectors\\DB2Connector->connect(Array)
#4 [internal function]: Cooperl\\Database\\DB2\\DB2ServiceProvider->Cooperl\\Database\\DB2\\{closure}(Array, 'QISDB')
#5 /var/www/ep2/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php(109): call_user_func(Object(Closure), Array, 'QISDB')
#6 /var/www/ep2/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php(74): Illuminate\\Database\\DatabaseManager->makeConnection('QISDB')
#7 /var/www/ep2/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1135): Illuminate\\Database\\DatabaseManager->connection('QISDB')
#8 /var/www/ep2/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1101): Illuminate\\Database\\Eloquent\\Model::resolveConnection('QISDB')
#9 /var/www/ep2/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(931): Illuminate\\Database\\Eloquent\\Model->getConnection()
#10 /var/www/ep2/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(877): Illuminate\\Database\\Eloquent\\Model->newBaseQueryBuilder()
gbryant200 commented 6 years ago

I had to load the pdo_ibm php extension for the cli php.ini file. Worked fine after that.