Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database connection in Queue Component #103

Open
drjoju opened this issue Jun 13, 2019 · 2 comments
Open

Database connection in Queue Component #103

drjoju opened this issue Jun 13, 2019 · 2 comments

Comments

@drjoju
Copy link

drjoju commented Jun 13, 2019

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
Copy link
Contributor

atefBB commented Feb 26, 2020

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

@Gummibeer Gummibeer mentioned this issue Jan 19, 2021
@chamanrastogi
Copy link

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants