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

PHPORM-81 implement mongodb driver for batch #2904

Open
wants to merge 11 commits into
base: 4.4
Choose a base branch
from

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Apr 23, 2024

Fix PHPORM-81

Checklist

  • Add tests and ensure they pass
  • Add an entry to the CHANGELOG.md file
  • Update documentation for new features

@GromNaN GromNaN changed the title PHPORM-81 implement mmongodb driver for batch PHPORM-81 implement mongodb driver for batch Apr 25, 2024
@GromNaN GromNaN force-pushed the PHPORM-81 branch 2 times, most recently from 82ff224 to 7264181 Compare May 2, 2024 15:12
@GromNaN GromNaN marked this pull request as ready for review May 2, 2024 17:53
@GromNaN GromNaN requested review from a team as code owners May 2, 2024 17:53
@GromNaN GromNaN requested a review from jmikola May 2, 2024 17:53
$_SERVER['__progress.count']++;
})
->then(function (Batch $batch) {
$_SERVER['__then.batch'] = $batch;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use $this because the closure is serialized.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would $this be used here even if it was supported?

Offhand, is laravel/serializable-closure used internally for serialization? AFAIK, the stock serialize() function doesn't support closures on its own.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the $_SERVER['something'] could have been replaced by $this->data['something']. But that's not allowed by the package you mentioned.

@GromNaN GromNaN changed the base branch from 4.3 to 4.4 May 3, 2024 13:05
Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about assuming that any string passed will be a valid ObjectId. A possible workaround would be to try and create an ObjectId instance, but leave the value as-is if it isn't valid. That way, the operation will either succeed (if such a record exists) or be a no-op (which would be the assumed behaviour for any batch that doesn't exist). As is, passing a string that isn't ObjectId-like would result in an exception, which IMO is not desirable.

public function get($limit = 50, $before = null): array
{
if (is_string($before)) {
$before = new ObjectId($before);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beware that this may throw an exception if the string is not a well-formed ObjectId. Not sure if that's something we want to consider, but I figured I'd point it out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the store method, the _id is not set, so only ObjectId can be inserted.

Copy link
Member

@alcaeus alcaeus May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to point this out as theoretically users could call the method with any string. No objection to assuming valid ObjectIDs are passed and leaving this as-is.

src/Bus/MongoBatchRepository.php Show resolved Hide resolved
Comment on lines +57 to +60
if (! isset($config['collection']) && isset($config['table'])) {
trigger_error('Since mongodb/laravel-mongodb 4.4: Using "table" option for the queue is deprecated. Use "collection" instead.', E_USER_DEPRECATED);
$config['collection'] = $config['table'];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgive me for any ignorant questions.

docs/queues.txt Outdated Show resolved Hide resolved
docs/queues.txt Outdated Show resolved Hide resolved

// Extending DatabaseBatchRepository is necessary so methods pruneUnfinished and pruneCancelled
// are called by PruneBatchesCommand
class MongoBatchRepository extends DatabaseBatchRepository implements PrunableBatchRepository
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason for inconsistent use of "Mongo" and "MongoDB" as class prefixes? This isn't the first time I've noticed it, and I understand that there may be historical inconsistencies in the project, but this PR introduces two new classes with inconsistent names.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also named MongoLock and MongoStore with the same unsaid naming rule, following the naming examples of MongoConnector, MongoQueue ...

src/Bus/MongoBatchRepository.php Outdated Show resolved Hide resolved
src/Bus/MongoBatchRepository.php Outdated Show resolved Hide resolved
src/Bus/MongoBatchRepository.php Show resolved Hide resolved
src/Bus/MongoBatchRepository.php Outdated Show resolved Hide resolved
@@ -51,6 +54,15 @@ protected function registerFailedJobServices()
*/
protected function mongoFailedJobProvider(array $config): MongoFailedJobProvider
{
return new MongoFailedJobProvider($this->app['db'], $config['database'], $config['table']);
if (! isset($config['collection']) && isset($config['table'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be an outright exception to specify both table and collection? As-is, I assume table would just be ignored.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, table will be ignored if collection is set. Allowing both make migration easier as you can specify both during transition.

$_SERVER['__progress.count']++;
})
->then(function (Batch $batch) {
$_SERVER['__then.batch'] = $batch;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would $this be used here even if it was supported?

Offhand, is laravel/serializable-closure used internally for serialization? AFAIK, the stock serialize() function doesn't support closures on its own.

@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
## [4.4.0] - unreleased

* Support collection name prefix by @GromNaN in [#2930](https://github.com/mongodb/laravel-mongodb/pull/2930)
* Add `mongodb` driver for Batching by @GromNaN in [#2904](https://github.com/mongodb/laravel-mongodb/pull/2904)
* Rename queue option `table` to `collection`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you also need to note deprecating expire for retry_after?

#[Override]
public function transaction(Closure $callback): mixed
{
return $this->connection->transaction(fn () => $callback());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any significance to wrapping $callback in an additional closure?

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

Successfully merging this pull request may close these issues.

None yet

3 participants