Repository navigation
Add new symfony dispatcher events - #29651
Conversation
d6332c7 to
6964cf6
Compare
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
7b07a15 to
295496a
Compare
PVince81
left a comment
There was a problem hiding this comment.
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.
| 'path' => $this->getHookPath($path), | ||
| 'fileinfo' => $this->getFileInfo($path) | ||
| ]); | ||
| $this->eventDispatcher->dispatch('\OC\Filesystem::create', $event); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| if ($result) { | ||
| $this->eventDispatcher->dispatch('\OC\Filesystem::delete_completed', $event); |
There was a problem hiding this comment.
is there a pre-delete event ?
maybe let's settle on names, howabout beforeDelete, afterDelete ? beforeCreate, afterCreate ?
@DeepDiver1975 thoughts ?
There was a problem hiding this comment.
agreed - sounds great
| return $this->removeMount($mount, $absolutePath); | ||
| } | ||
| if ($this->is_dir($path)) { | ||
| $event->setArgument('path', $path); |
There was a problem hiding this comment.
if we have file info we likely don't need the path as it's already contained inside the file info object
| try { | ||
| $event->setArgument('fileinfo', $this->getFileInfo($path)); | ||
| } catch (\Exception $e) { | ||
| $this->logger->warning(__METHOD__ . " failed to retrieve file info for $path"); |
There was a problem hiding this comment.
if we don't log the exception we'll never know what really happened there... we have $this->logger->logException()
|
|
||
| private $userManager; | ||
|
|
||
| private $eventDispatcher; |
|
|
||
| private $eventDispatcher; | ||
|
|
||
| private $logger; |
| } | ||
|
|
||
| if ($result) { | ||
| $this->eventDispatcher->dispatch('\OC\Filesystem::delete_completed', $event); |
There was a problem hiding this comment.
agreed - sounds great
| } catch (\Exception $e) { | ||
| $this->logger->warning(__METHOD__ . " failed to retrieve file info for $path"); | ||
| } | ||
| $this->eventDispatcher->dispatch('\OC\Filesystem::read_completed', $event); |
There was a problem hiding this comment.
this is not read_completed but beforeRead
There was a problem hiding this comment.
I wanted to make the call after basicOperation. So after the call to basicOperation its read_completed. I will update the PR accordingly.
| 'uid' => $user->getUID(), | ||
| 'password' => $password | ||
| ]); | ||
| \OC::$server->getEventDispatcher()->dispatch('\OC\User\Manager::createUser', $event); |
There was a problem hiding this comment.
@PVince81 I'd expect here to have OCP namespace .....
There was a problem hiding this comment.
but we might want to stick to the symfony event naming convention? see https://symfony.com/doc/current/components/event_dispatcher.html#naming-conventions
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
"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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
let's use user.beforeCreate and user.afterCreate here
| if ($result) { | ||
| if ($this->emitter) { | ||
| $this->emitter->emit('\OC\User', 'postSetPassword', [$this, $password, $recoveryPassword]); | ||
| $event = new GenericEvent(null, ['user' => $this]); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
emitters are private - we can kill them right away
hooks is a different story - yes
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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');
}5c1a3c6 to
41896c5
Compare
ec4f246 to
615a5c8
Compare
| try { | ||
| $this->doPut('/testingfile.txt'); | ||
| } catch (\Exception $e) { | ||
| $this->assertInstanceOf(Exception::class, $e); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
so I suggest to just re-read the value from the event argument to keep the old logic working as it was before...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Please just re-read the value, for consistency with the other code.
There was a problem hiding this comment.
@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); |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Looks better, getting close. Please check my latest comments.
1d6baa9 to
2e496c1
Compare
|
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]>
|
@sharidas please backport |
|
Backport to stable10: #29939 |
|
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. |
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
Checklist: