mattstauffer / Torch

Examples of using each Illuminate component in non-Laravel applications
MIT License
1.85k stars 210 forks source link

Database connection in Queue Component #103

Open drjoju opened 5 years ago

drjoju commented 5 years ago

Hello,

I'm trying to add a database connection to Queue component. Something like:

$capsule = new Capsule;
$capsule->addConnection([
    'driver' =>'mysql',
    'host' => $dbhost,
    'database' => $dbname,
    'username' => $dbuname,
    'password' => $dbpass,
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => $prefix."_",
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();

$container->instance("capsule", $capsule);

// Sistema de colas
$queue = new Queue();

$queue->addConnection([
    'driver' => 'sync',
]);
$queue->addConnection([
    'driver' => 'database',
    'table' => 'jobs',
    'queue' => 'default',
    'connection' => 'default',
    'host'      => 'localhost',
], 'database');

$manager = $queue->getQueueManager();
$container['queue'] = $manager;
$connection = Capsule::schema()->getConnection();
$resolver = new \Illuminate\Database\ConnectionResolver(['default' => $connection]);
$manager->addConnector('database', function () use ($resolver) {
    return new DatabaseConnector($resolver);
});
$queue->setAsGlobal();

I created the table like Laravel does. I'm trying to do the same as Laravel, that is, WorklistOrderJob :: dispatch ($order); with this code

$orden = Orden::where('idorden', 2015111100000000054)->first();
$queue->connection('database')->push('App\\Jobs\\WorklistOrderJob', $orden);

Where Orden is a Eloquent Model.

My goal is to insert in the database the instance of the model so another application in Laravel consumes the model inserted in the Jobs table. I can see the model in the database but when I call

 php artisan queue:work --once

an error appears "Call to undefined method fire".

When I add fire method to the Job the database entrance is procesed.

public function fire()
    {
        $this->handle();
    }

Why fire thas not exists? Laravel is in version 5.7 and I'm using Torch 5.5. Incompatibilty problem? Is it correct what I am saying?

Any suggestions or comments are welcomed.

Thanks in advance

atefBB commented 4 years ago

Hey @drjoju! I have the same problem and I wonder how did you solve this issue?

chamanrastogi commented 3 years ago

use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Events\Dispatcher; use Illuminate\Container\Container; use Illuminate\Support\Facades\Facade; $capsule = new Capsule;

$capsule->addConnection([ 'driver' => DBDRIVER, 'host' => DB_SERVER, 'database' => DB_NAME, 'username' => DB_USER, 'password' => DB_PASS, 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => PREFIX, ]);

// Set the event dispatcher used by Eloquent models... (optional)

$capsule->setEventDispatcher(new Dispatcher(new Container));

// Make this Capsule instance available globally via static methods... (optional) $capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) $capsule->bootEloquent();

$database = new Capsule(); $db = & $database;


Global $database; $database->table('users')->where('votes', '>', 100)->get(); or you can make model User::find(1)-get();