Skip to content

Add new symfony dispatcher events - #29651

Merged
PVince81 merged 2 commits into
masterfrom
add-symfony-events-part1
Dec 20, 2017
Merged

PVince81 merged 2 commits into
masterfrom
add-symfony-events-part1

Conversation

@sharidas

Copy link
Copy Markdown
Contributor

Add new symfony dispatcher events

Signed-off-by: Sujith H [email protected]

Description

Add new symfony dispatcher events

Related Issue

#29174

Motivation and Context

Adding more symfony dispatcher events.

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@sharidas sharidas added this to the development milestone Nov 22, 2017
@sharidas
sharidas force-pushed the add-symfony-events-part1 branch 3 times, most recently from d6332c7 to 6964cf6 Compare November 22, 2017 17:09
@codecov

codecov Bot commented Nov 22, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #29651 into master will increase coverage by 0.03%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #29651      +/-   ##
============================================
+ Coverage     58.17%   58.21%   +0.03%     
- Complexity    18457    18506      +49     
============================================
  Files          1092     1093       +1     
  Lines         63617    63690      +73     
============================================
+ Hits          37012    37079      +67     
- Misses        26605    26611       +6
Impacted Files Coverage Δ Complexity Δ
drone/src/lib/private/User/Manager.php 75.72% <0%> (-0.31%) 60% <0%> (ø)
drone/src/lib/public/Events/EventEmitterTrait.php 100% <0%> (ø) 7% <0%> (?)
drone/src/lib/private/User/Session.php 62.17% <0%> (+0.03%) 124% <0%> (ø) ⬇️
drone/src/lib/private/Server.php 82.71% <0%> (+0.07%) 128% <0%> (ø) ⬇️
drone/src/lib/private/User/User.php 82.92% <0%> (+0.1%) 63% <0%> (ø) ⬇️
drone/src/lib/private/Files/View.php 84.78% <0%> (+0.17%) 376% <0%> (+6%) ⬆️
drone/src/apps/dav/lib/Connector/Sabre/File.php 84.47% <0%> (+1.14%) 139% <0%> (+36%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f32796b...b15dcc7. Read the comment docs.

@sharidas
sharidas force-pushed the add-symfony-events-part1 branch 7 times, most recently from 7b07a15 to 295496a Compare November 23, 2017 11:32
@sharidas sharidas self-assigned this Nov 23, 2017
@sharidas sharidas changed the title [WIP] Add new symfony dispatcher events Add new symfony dispatcher events Nov 23, 2017

@PVince81 PVince81 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See my comments above.

I wonder whether it's possible for event dispatcher to first ask whether there is any listener before retrieving potentially expensive operation.

Comment thread lib/private/Files/View.php Outdated
'path' => $this->getHookPath($path),
'fileinfo' => $this->getFileInfo($path)
]);
$this->eventDispatcher->dispatch('\OC\Filesystem::create', $event);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you check whether we could trigger the dispatcher directly inside of basicOperation ?
I'd like to avoid having an extra call to getFileInfo for the events because it will cause more DB calls even if no listener is created. Please check whether basicOperation already has the file info object available somewhere for reuse.

also we used to have pre and post hooks, would be good to also have those as dispatcher event. Goal is that at some time in the future these could replace the old hooks, so we need to be close to the original functionality.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately I couldn't find call to getFileInfo at basicOperation or FileInfo object. This was bothering me too as each call becomes expensive, whether there are listeners or not :(

Sure I will add post and pre events.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmmm that's sad then. As an alternative we could rather pass in the file id only and let the listener get the file info by themselves. The question is how to get it in an efficient manner.

The public API states that one should use \OC::$server->getUserFolder()->getById($fileId) but that's a rather expensive call. The other alternative would be to inject the View and the $path and let the subscriber resolve it. However the View is not supposed to be public API...

bringing in @jvillafanez for further ideas

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about using custom events instead of the default one? The custom event could have the View object inside (or any other needed dependency) and provide custom methods to access to the object depending on what we want to expose.
(Info in https://symfony.com/doc/current/components/event_dispatcher.html#creating-an-event-class)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@DeepDiver1975 @PVince81 So with the current state of the patch, if we add a custom event as per @jvillafanez , then we can send View object and $path1 and/or $path2. And in the trait, for before and after events we can use the data from this custom event. And the subscriber of the event would resolve it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

View is considered private API. If we were to pass something it would need to be the current folder from getUserFolder()->get($path) which is likely expensive to resolve. Note that said folder is from the \OCP\Files\Node interface which itself implements/embeds the FileInfo.

Comment thread lib/private/Files/View.php Outdated
}

if ($result) {
$this->eventDispatcher->dispatch('\OC\Filesystem::delete_completed', $event);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is there a pre-delete event ?

maybe let's settle on names, howabout beforeDelete, afterDelete ? beforeCreate, afterCreate ?

@DeepDiver1975 thoughts ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

agreed - sounds great

Comment thread lib/private/Files/View.php Outdated
return $this->removeMount($mount, $absolutePath);
}
if ($this->is_dir($path)) {
$event->setArgument('path', $path);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if we have file info we likely don't need the path as it's already contained inside the file info object

Comment thread lib/private/Files/View.php Outdated
try {
$event->setArgument('fileinfo', $this->getFileInfo($path));
} catch (\Exception $e) {
$this->logger->warning(__METHOD__ . " failed to retrieve file info for $path");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if we don't log the exception we'll never know what really happened there... we have $this->logger->logException()

Comment thread lib/private/Files/View.php Outdated

private $userManager;

private $eventDispatcher;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PHPDoc


private $eventDispatcher;

private $logger;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PHPDoc

Comment thread lib/private/Files/View.php Outdated
}

if ($result) {
$this->eventDispatcher->dispatch('\OC\Filesystem::delete_completed', $event);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

agreed - sounds great

Comment thread lib/private/Files/View.php Outdated
} catch (\Exception $e) {
$this->logger->warning(__METHOD__ . " failed to retrieve file info for $path");
}
$this->eventDispatcher->dispatch('\OC\Filesystem::read_completed', $event);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is not read_completed but beforeRead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wanted to make the call after basicOperation. So after the call to basicOperation its read_completed. I will update the PR accordingly.

Comment thread lib/private/Server.php Outdated
'uid' => $user->getUID(),
'password' => $password
]);
\OC::$server->getEventDispatcher()->dispatch('\OC\User\Manager::createUser', $event);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@PVince81 I'd expect here to have OCP namespace .....

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok, so change the format again ? we already settled on this OC namespace + event id for many other events.

Anyway, I'm ok switching convention now as I prefer to refer to documented stuff.

I'd expect here to have OCP namespace

This would imply that there are also private symfony events. Not sure if we want private events any more ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we stick with class+event I'd expect this to be OCP namespaces since events are part of the public api.

but maybe its enough to have "user.beforeCreate" and "user.afterCreate"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@DeepDiver1975 @PVince81 how about "ocp_iusermanager.beforeCreate" for https://github.com/owncloud/core/blob/master/lib/private/User/Manager.php#L323 ( as an example )?
Instead of using namespace with slashes "/" use "_" and all letters remain in small case as per document.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IMHO, we can add all the events name as a constant of the relevant class. For example in the \OCP\User class we can add

const EventCreate = 'ocp_user.create';
const EventDelete = 'ocp_user.delete';

In this way we can access it via \OCP\User::EventCreate or directly event name string. It also makes easier to find the class events for developers.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"iusermanager" here doesn't make sense. Whoever is listening to the event shouldn't need to know where the event come from. The only thing a listener is interested in is the type of event and what it applies to.

So I think I prefer "user.beforeCreate" and "user.afterCreate". This makes it clear that it's about a user being created or deleted. What component of ownCloud does it (user manager or whatever) doesn't matter. And I don't like ocp when used this way.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Regarding the constants, I'm not even sure whether we should have the constants directly in the \OCP\User object. Should we have a more global location for all user and group management related event constants ? Depending on specific classes feels too close to the old emitter pattern.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

let's use user.beforeCreate and user.afterCreate here

Comment thread lib/private/User/User.php Outdated
if ($result) {
if ($this->emitter) {
$this->emitter->emit('\OC\User', 'postSetPassword', [$this, $password, $recoveryPassword]);
$event = new GenericEvent(null, ['user' => $this]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we fully kill the emitter concept as we are working on this as of now? All emitter interfaces never hit the public api - we can kill it right away @PVince81 thoughts

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All emitter interfaces never hit the public api

Really ? I remember that @patrickjahns's example app was using the emitter events from the public files Node API. I'm sure other apps do so to.

Once we have symfony events for everything, we can first deprecate all hooks and emitter events and give app devs time to migrate. Only then we can kill these completely.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

emitters are private - we can kill them right away
hooks is a different story - yes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, so emitters that are triggering events from a private object (\OC namespace) could be replaced with using symfony events. But this is out of scope for this PR.

The purpose of this PR is to add new events without causing any regressions.
Killing/replacing anything will require careful regression testing and potentially adding new tests where tests are missing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Raised here #29672

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

from an implementation point of view - this can help to streamline the code base and avoid duplications:

private emittingCall(\Closure $fn, $class, $eventName) {
    $this->eventDispatcher->dispatch("$class::before$eventName");
    $result = $fn();
    $this->eventDispatcher->dispatch("$class::after$eventName");
    return $result;
}

// within the implementation of e.g. create user

private function createUser() {
    return emittingCall( function() use($userManager) {
        return $userManager->createUser();
    }, '\OCP\IUserManager', 'create');
}

@sharidas
sharidas force-pushed the add-symfony-events-part1 branch 7 times, most recently from 5c1a3c6 to 41896c5 Compare November 27, 2017 13:36
@sharidas
sharidas force-pushed the add-symfony-events-part1 branch 8 times, most recently from ec4f246 to 615a5c8 Compare December 14, 2017 09:03
try {
$this->doPut('/testingfile.txt');
} catch (\Exception $e) {
$this->assertInstanceOf(Exception::class, $e);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is there no specific exception for this ?

Also use @expectedException in the PHPDoc for PHPUnit to check for this exception.
The problem with this code is that if the exception is not thrown at all the test still passes, so would need an additional check, or have the assert outside... Best is to use @expectedException instead in this situation with the proper exception class name. There is also a tag for the exception message if that makes sense.

@sharidas sharidas Dec 14, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Indeed it does have exception \Sabre\DAV\Exception. There was a failure observed when I tried with @expectedException. Yesterday I spent sometime with the same. The error of exception failure was observed only in Jenkins. Where as when I used @expectedException in local machine it never caused a problem. Yes I used @expectExceptionMessage for message in the same version of the PR which had @expectedException. Will try to see again how to resolve it.

]);
if ($run) {
$event->setArgument('run', $run);
$this->eventDispatcher->dispatch('file.beforeCreate', $event);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

here you also forgot to check the value of $run. Please recheck all occurrences for this logic.

]);
if ($run) {
$event->setArgument('run', $run);
$this->eventDispatcher->dispatch('file.beforeUpdate', $event);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

re-read the last value of $run from the argument.

Now I find it strange that the value doesn't seem to be used below and is just passed again...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

so I suggest to just re-read the value from the event argument to keep the old logic working as it was before...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I did verified with file operations like create, modify/update, delete, rename operations in web ui. At least in that context no damage happened. The problem might appear, when the event listener alters $run value. But as of now the alteration happens only in tests so, I guess we are safe for now. @PVince81 Let me know if this change looks ok. If you really feel that just re-read value is sufficient, then I can do that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please just re-read the value, for consistency with the other code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@PVince81 Ok, I have updated the PR, by just re-reading the value.

$calledAfterEvent = [];
\OC::$server->getEventDispatcher()->addListener("$className.after$eventName", function ($event) use (&$calledAfterEvent, &$className, &$eventName) {
$calledAfterEvent[] = "$className.after$eventName";
array_push($calledAfterEvent, $event);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why not just use $calledAfterEvent[] = $event to push to array ? changing the code style in the middle looks weird.

same comment applies to all other array_push locations

@PVince81 PVince81 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks better, getting close. Please check my latest comments.

@sharidas
sharidas force-pushed the add-symfony-events-part1 branch 7 times, most recently from 1d6baa9 to 2e496c1 Compare December 15, 2017 06:58

@PVince81 PVince81 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

@PVince81

Copy link
Copy Markdown
Contributor

restarted drone build

Add new symfony dispatcher events

Signed-off-by: Sujith H <[email protected]>
Add events to check run value at emitPreHooks

Signed-off-by: Sujith H <[email protected]>
@PVince81

Copy link
Copy Markdown
Contributor

@sharidas please backport

@sharidas

Copy link
Copy Markdown
Contributor Author

Backport to stable10: #29939

@lock

lock Bot commented Aug 1, 2019

Copy link
Copy Markdown

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants