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

MongoStrategy. Сlosing connection. Why? #41

Open
cystbear opened this issue Mar 23, 2016 · 0 comments
Open

MongoStrategy. Сlosing connection. Why? #41

cystbear opened this issue Mar 23, 2016 · 0 comments

Comments

@cystbear
Copy link

Hi, I have found next in MongoStrategy

 If you use this strategy you MUST close your Mongo connection on the spork.pre_fork event.
     $mongo = new Mongo();
     $manager->addListener(Events::PRE_FORK, array($mongo, 'close'));

I can not understand why I need to do this?

Also my code looks like, I mean that I open connection out of logic closure that runs inside fork.

$mongo  = new \MongoClient();
$db     = $mongo->selectDB(DB_NAME);
$coll   = $db->selectCollection(COLL_NAME);
$cursor = $coll->find([/*some query here*/]);

$dispatcher = new SporkDispatcher\EventDispatcher();
$dispatcher->addListener(SporkDispatcher\Events::PRE_FORK, function() use($mongo) {
    $mongo->close();
});
$manager  = new Spork\ProcessManager($dispatcher);
$strategy = new Spork\Batch\Strategy\MongoStrategy(5);

$manager->process($cursor, function($document) {
    $document; // some work here
}, $strategy);

There are few more questions:

  • if I close connection BEFORE any forking operation, what will be with cursor?
  • will cursor close as well as connection?
  • will forked process open new connection inside it-self, or what?

So, looks like @kriswallsmith will be helpful here.

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

1 participant