When subscribing to an event subscriber it is likely you will need to display messages to the migration system. The correct migrate message service is dependent on the calling code not a generic service though so this needs to be provided with the event so the subscriber can correctly display messages. Think of a subscriber receiving an event in drush vs a batch.

Comments

neclimdul created an issue. See original summary.

Status: Needs review » Needs work

The last submitted patch, migrate_event_messages.patch, failed testing.

neclimdul’s picture

StatusFileSize
new6.34 KB
new1.04 KB
neclimdul’s picture

Status: Needs work » Needs review
benjy’s picture

It's likely many migrate events are going to need to be able to log. I wonder if we should just add a logMessage() method to a base migrate event class rather than exposing the service?

hussainweb’s picture

StatusFileSize
new7.75 KB

I added a common event base class for all migrate events and put the message interface on that directly. I also added a logMessage() method on this base class as described in #5. I lost the interdiff I created but it was around the size of the patch itself, so, I hope it's okay.

mikeryan’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests
  1. +++ b/core/modules/migrate/src/Event.php
    @@ -0,0 +1,52 @@
    +namespace Drupal\migrate;
    

    Should be in the Drupal\migrate\Event namespace.

  2. +++ b/core/modules/migrate/src/Event.php
    @@ -0,0 +1,52 @@
    +class Event extends SymfonyEvent {
    

    I think it would be clearer to name this MigrationEvent.

  3. +++ b/core/modules/migrate/src/Event.php
    @@ -0,0 +1,52 @@
    +  public function logMessage($message, $type = 'status') {
    

    I'm not sure it's really worthwhile to have a convenience function for this - $event->logMessage() isn't exactly a huge improvement over $event->getMessageService()->display(). And I think having a different function name (logMessage vs. display) obscures what it's doing - a migration developer familiar with the API will immediately recognize what display() is doing, and will hesitate over using a different name to do the exactly same thing just because the context is an event listener.

    Besides which, we're not logging here (which implies persisting the message somewhere such as the message table), we are displaying the message in a front end.

  4. +++ b/core/modules/migrate/src/Event/MigrateImportEvent.php
    @@ -27,9 +28,12 @@ class MigrateImportEvent extends Event {
    +  public function __construct(MigrationInterface $migration, MigrateMessageInterface $message) {
    

    Best practice is to extend the parent class signature (the arguments being passed through should be in the order of the base constructor). Applies to all the events.

  5. +++ b/core/modules/migrate/src/Event/MigrateImportEvent.php
    @@ -27,9 +28,12 @@ class MigrateImportEvent extends Event {
    +    $this->message = $message;
    

    Should call the parent constructor. Applies to all the events.

  6. +++ b/core/modules/migrate/src/Event/MigrateMapSaveEvent.php
    @@ -7,8 +7,8 @@
    +use Drupal\migrate\Event;
    ...
    -use Symfony\Component\EventDispatcher\Event;
    

    The map events are quietly changed to use the new base class, but the message service is not being passed, so a consumer of the event trying to use the message service will blow up. The problem, of course, is these events are dispatched from the Sql id map plugin, which does not have access to the MigrateExecutable or its message service. So, if the map events are to use the message service, it will need to be passed to ID map plugins. If not, then they should continue to extend the Symfony event.

hussainweb’s picture

#7:

1. Actually, it should be in Drupal\migrate namespace itself so that FQCN is \Drupal\migrate\Event.
2. I am mixed on that. I think there is merit in it being \Drupal\migrate\Event rather than \Drupal\migrate\MigrateEvent. Also, consider that this base class would not be used in itself, only extended (we could mark it abstract).
3. I agree with you here. I just added it to see if it was worthwhile. It doesn't look like it.
4. and 5. Agreed.
6. I couldn't find where those events were being invoked. I will look for them again to make sure.

@mikeryan: I'd like to hear your thoughts on the above, especially points 1 and 2.

neclimdul’s picture

1) I think Mike is right. Drupal\migrate\Event also references 2 things in the patch which isn't illegal but is confusing.
2) EventBase. I'm a fan of letting the namespace clarify the rest and not duplicating.
3) yeah, we can get rid of that.
4) sure
5) yeah, fixed.
6) I didn't do these because they required some more refactoring. Moved them back to symfony events for now. so we can unblock #2545632: [PP1] Move memory reclamation out of migrate executable

core/modules/migrate/src/Plugin/migrate/id_map/Sql.php:      $this->eventDispatcher->dispatch(MigrateEvents::MAP_SAVE, new MigrateMapSaveEvent($this, $keys + $fields));
core/modules/migrate/src/Plugin/migrate/id_map/Sql.php:      $this->eventDispatcher->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id_values));
core/modules/migrate/src/Plugin/migrate/id_map/Sql.php:      $this->eventDispatcher->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id));
core/modules/migrate/src/Plugin/migrate/id_map/Sql.php:        $this->eventDispatcher->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id));
core/modules/migrate/src/Plugin/migrate/id_map/Sql.php:        $this->eventDispatcher->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id));

Still needs tests. Will take a look.

neclimdul’s picture

Status: Needs work » Needs review

Not a lot to test but here it is. Tossed in the other methods on the events just because its quick but am willing to remove the tests.

neclimdul’s picture

Status: Needs review » Needs work

The last submitted patch, 11: add_message_service_to-2546004-10.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 11: add_message_service_to-2546004-10.patch, failed testing.

neclimdul’s picture

Status: Needs work » Needs review
StatusFileSize
new1.95 KB
new13.72 KB

phpunit was perfectly happy not failing on this namespace mistake which is why I couldn't figure out why it was failing.

benjy’s picture

$event->logMessage() isn't exactly a huge improvement over $event->getMessageService()->display()

The point is that the consuming code is decoupled from the message service.

a migration developer familiar with the API will immediately recognize what display() is doing

I actually think we should rename to log() or logMessage() everywhere, they much better describe what we're doing than display(). Given the message service is swappable, it could be logged to syslog, or it could be logged to the screen. display() only works in the latter.

+++ b/core/modules/migrate/src/Event/MigratePreRowSaveEvent.php
@@ -33,10 +33,14 @@ class MigratePreRowSaveEvent extends Event {
+  public function __construct(MigrateMessageInterface $message, MigrationInterface $migration, Row $row) {

As I mentioned on the other issue, i'd rather the order of the params were the same as MigrateExecutable

neclimdul’s picture

1) so... log something method? should maybe be follow up to just cleanup the message system?
2) That was set in response to #7.4

benjy’s picture

Yes, i think a logMessage() method on the event and then a follow-up to rename that it the message system.

Shame about the constructor args, IMO the base class was wrong, we had MigrateExecutable as precedent for the order of those two args, and consistency is important for me. I'd vote for re-ordering those params in the base event class as well to match up with MigrateExecutable.

neclimdul’s picture

StatusFileSize
new14.35 KB
new14.35 KB

Miss-understanding with benjy. Discussed and we decided to push migration down to the base class and match executable signature.

Also convert getMessage() to logMessage() shortcut method.

neclimdul’s picture

StatusFileSize
new18.69 KB

its way to late... here's the interdiff i meant to click on.

webchick’s picture

Issue tags: +blocker

This is blocking #2545632: [PP1] Move memory reclamation out of migrate executable so tagging. However, that is also a normal task clean-up issue so I don't think we need any priority changes here.

quietone’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
+++ b/core/modules/migrate/tests/src/Unit/Event/MigratePostRowSaveEventTest.php
@@ -0,0 +1,76 @@
+    $event = new MigratePostRowSaveEvent($message_service, $migration, $row, [1,2,3]);

Shouldn't [1,2,3] be [1, 2, 3]? This appears twice in each Unit test.

The only comment that I don't see agreement on is mikeryan's second point in comment #7 about

I think it would be clearer to name this MigrationEvent.

I noticed the rollback events are not tested. Does that need to be done?

neclimdul’s picture

Status: Needs work » Needs review
StatusFileSize
new821 bytes
new14.37 KB

Lets see what drupalci things about this.

dawehner’s picture

+++ b/core/modules/migrate/src/Event/EventBase.php
@@ -0,0 +1,65 @@
+class EventBase extends SymfonyEvent {

This this supposed to be an abstract base class?

neclimdul’s picture

It looks like maybe... 3 months is hard to remember...

benjy’s picture

+++ b/core/modules/migrate/src/Event/EventBase.php
@@ -0,0 +1,65 @@
+use Symfony\Component\EventDispatcher\Event as SymfonyEvent;
...
+class EventBase extends SymfonyEvent {

Why do we need the as SymfonyEvent here? and also I think @dawehners feedback is valid in #24 and then this looks good to me.

neclimdul’s picture

So looking deeper, I'm going to say I probably didn't make it abstract because its not abstract and Symfony's Event class isn't abstract either. I'm not sure there's really a point to making it abstract and all it would do is make the test implement an non-abstract implementation.

The reason for extending Symfony's even class is because it contains all the methods expected by dispatchers. There is no interface but you could call it the defacto interface. Also, this is the code we're changing, we where already using it.

+++ b/core/modules/migrate/src/Event/MigratePreRowSaveEvent.php
@@ -8,13 +8,13 @@
-use Symfony\Component\EventDispatcher\Event;
 
 /**
  * Wraps a pre-save event for event listeners.
  */
-class MigratePreRowSaveEvent extends Event {
+class MigratePreRowSaveEvent extends EventBase {
benjy’s picture

I think @neclimdul is right, I was getting confused with the abstract final class that provides the event names, not the event class itself.

tvb’s picture

Issue tags: -Needs reroll

I could apply the patch, so no reroll is needed.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mikeryan’s picture

Version: 8.1.x-dev » 8.2.x-dev
Issue tags: +neworleans2016, +Migrate BC break

Status: Needs review » Needs work

The last submitted patch, 23: add_message_service_to-2546004-23.patch, failed testing.

mikeryan’s picture

Issue tags: -Needs tests +Needs reroll
quietone’s picture

Status: Needs work » Needs review
StatusFileSize
new14.13 KB

Reroll.

Status: Needs review » Needs work

The last submitted patch, 34: add_message_service_to-2546004-34.patch, failed testing.

quietone’s picture

Not sure why a comment test is failing, and this passes locally so retesting.

quietone’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll

Thats better. Back to needs review.

neclimdul’s picture

StatusFileSize
new21.83 KB
new21.83 KB

One little fix to remove @file dockblock. Still applies and works.

neclimdul’s picture

StatusFileSize
new866 bytes
new14.07 KB

man I've had a hard time with the issue queue today...
better patches. interdiff vs #34.

The last submitted patch, 38: add_message_service_to-2546004-38.patch, failed testing.

phenaproxima’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/migrate/src/Event/EventBase.php
    @@ -0,0 +1,60 @@
    +   * Migration entity.
    

    Migrations are no longer entities. :) This should also be fixed in the other events while we're at it.

  2. +++ b/core/modules/migrate/src/Event/EventBase.php
    @@ -0,0 +1,60 @@
    +   *   Migration entity.
    

    Ditto.

  3. +++ b/core/modules/migrate/src/Event/EventBase.php
    @@ -0,0 +1,60 @@
    +   * Gets the migration entity.
    

    Ditto.

  4. +++ b/core/modules/migrate/src/Event/EventBase.php
    @@ -0,0 +1,60 @@
    +   * @param string $type
    

    Should we perhaps define constants in MigrateMessageInterface to set the levels?

  5. +++ b/core/modules/migrate/src/Event/MigratePostRowSaveEvent.php
    @@ -11,17 +12,24 @@
    +   * @var array|bool
    

    Needs a description. And why would bool ever be allowed here?

  6. +++ b/core/modules/migrate/tests/src/Unit/Event/EventBaseTest.php
    @@ -0,0 +1,44 @@
    +  public function testLogMessage() {
    

    This method has no assertions in it.

  7. +++ b/core/modules/migrate/tests/src/Unit/Event/MigrateImportEventTest.php
    @@ -0,0 +1,43 @@
    +  public function testLogMessage() {
    

    Neither does this one.

neclimdul’s picture

Thanks!

4) Yeah it should really follow the PSR-3 logger interface which would define those. I was trying not to leak the scope of this outside of injecting the service.

5)

+++ b/core/modules/migrate/src/Event/MigratePostRowSaveEvent.php
@@ -11,17 +12,24 @@
    * @param array|bool $destination_id_values
    *   Values represent the destination ID.

Got me, that behavior was already defined in the constructor. I just hit ctrl-enter because the property wasn't defined I think. Suggestions on comment welcome.

6 and 7)

+++ b/core/modules/migrate/tests/src/Unit/Event/EventBaseTest.php
@@ -0,0 +1,44 @@
+    $message_service->display('status message', 'status')->shouldBeCalled();
+    $message_service->display('warning message', 'warning')->shouldBeCalled();

+++ b/core/modules/migrate/tests/src/Unit/Event/MigrateImportEventTest.php
@@ -0,0 +1,43 @@
+    $message_service->display('status message', 'status')->shouldBeCalled();
+    $message_service->display('warning message', 'warning')->shouldBeCalled();

These are fine actually, the mocks contain the assertions.

phenaproxima’s picture

Re #5, how about something like "The row's destination ID"?

And regarding #6 and #7, that's fine as long as there is a comment explaining that.

neclimdul’s picture

Status: Needs work » Needs review
StatusFileSize
new3.79 KB
new14.24 KB
phenaproxima’s picture

I have nits. After these are fixed, this is RTBC from me if it passes Drupal CI.

  1. +++ b/core/modules/migrate/src/Event/EventBase.php
    @@ -0,0 +1,60 @@
    +   * Related migration.
    

    Can this just be "The migration"?

  2. +++ b/core/modules/migrate/src/Event/EventBase.php
    @@ -0,0 +1,60 @@
    +   * @var \Drupal\migrate\Plugin\\MigrationInterface
    

    Nit: extra slash before MigrationInterface

  3. +++ b/core/modules/migrate/src/Event/EventBase.php
    @@ -0,0 +1,60 @@
    +   * The current message service.
    

    Let's remove the word "current".

  4. +++ b/core/modules/migrate/src/Event/EventBase.php
    @@ -0,0 +1,60 @@
    +  /**
    +   * Constructs a migrate event object.
    

    Nit: s/migrate/Migrate

  5. +++ b/core/modules/migrate/src/Event/EventBase.php
    @@ -0,0 +1,60 @@
    +   *   The related migration being run.
    

    I don't think we should say "related", it has strange connotations.

  6. +++ b/core/modules/migrate/src/Event/EventBase.php
    @@ -0,0 +1,60 @@
    +   *   The current migrate message service.
    

    s/migrate/Migrate

  7. +++ b/core/modules/migrate/src/Event/MigratePostRowSaveEvent.php
    @@ -11,17 +12,26 @@
    +   * @var array|bool
    

    Remove |bool type hint.

  8. +++ b/core/modules/migrate/src/Event/MigratePostRowSaveEvent.php
    @@ -11,17 +12,26 @@
        * @param array|bool $destination_id_values
    

    Let's remove |bool here, I don't think this value will ever be boolean.

  9. +++ b/core/modules/migrate/src/Event/MigratePostRowSaveEvent.php
    @@ -11,17 +12,26 @@
    +  public function __construct(MigrationInterface $migration, MigrateMessageInterface $message, Row $row, $destination_id_values) {
    

    Can $destination_id_values be typehinted as an array?

neclimdul’s picture

StatusFileSize
new1.27 KB
new14.22 KB

As discussed in IRC, 7-9, no can do. that's all tied to the return value of ::import(). Additionally unrelated to this patch.

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

I assume this will pass Drupal CI. Thanks for putting up with my persnicketiness. Happily RTBC!

alexpott’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed

Committed e5908d6 and pushed to 8.2.x. Thanks!

+++ b/core/modules/migrate/src/Event/EventBase.php
@@ -0,0 +1,60 @@
+  /**
+   * Displays a Migrate message.
+   *
+   * @param string $message
+   *   The message to display.
+   * @param string $type
+   *   The type of message, for example: status or warning.
+   */
+  public function logMessage($message, $type = 'status') {

+++ b/core/modules/migrate/tests/src/Unit/Event/EventBaseTest.php
@@ -0,0 +1,44 @@
+

Well the documentation still has the "display" thing... I think we should change "display" to "log" here. For the reasons given earlier.

diff --git a/core/modules/migrate/src/Event/EventBase.php b/core/modules/migrate/src/Event/EventBase.php
index 7f158d8..b228474 100644
--- a/core/modules/migrate/src/Event/EventBase.php
+++ b/core/modules/migrate/src/Event/EventBase.php
@@ -46,10 +46,10 @@ public function getMigration() {
   }
 
   /**
-   * Displays a Migrate message.
+   * Logs a message using the Migrate message service.
    *
    * @param string $message
-   *   The message to display.
+   *   The message to log.
    * @param string $type
    *   The type of message, for example: status or warning.
    */

Made these changes on commit because I don't think this is controversial at all.

diff --git a/core/modules/migrate/tests/src/Unit/Event/MigratePostRowSaveEventTest.php b/core/modules/migrate/tests/src/Unit/Event/MigratePostRowSaveEventTest.php
index 97d6b66..f42cb06 100644
--- a/core/modules/migrate/tests/src/Unit/Event/MigratePostRowSaveEventTest.php
+++ b/core/modules/migrate/tests/src/Unit/Event/MigratePostRowSaveEventTest.php
@@ -20,8 +20,8 @@ public function testGetDestinationIdValues() {
     $migration = $this->prophesize('\Drupal\migrate\Plugin\MigrationInterface')->reveal();
     $message_service = $this->prophesize('\Drupal\migrate\MigrateMessageInterface')->reveal();
     $row = $this->prophesize('\Drupal\migrate\Row')->reveal();
-    $event = new MigratePostRowSaveEvent($migration, $message_service, $row, [1,2,3]);
-    $this->assertSame([1,2,3], $event->getDestinationIdValues());
+    $event = new MigratePostRowSaveEvent($migration, $message_service, $row, [1, 2, 3]);
+    $this->assertSame([1, 2, 3], $event->getDestinationIdValues());
   }

   /**
@@ -34,7 +34,7 @@ public function testGetRow() {
     $migration = $this->prophesize('\Drupal\migrate\Plugin\MigrationInterface')->reveal();
     $message_service = $this->prophesize('\Drupal\migrate\MigrateMessageInterface');
     $row = $this->prophesize('\Drupal\migrate\Row')->reveal();
-    $event = new MigratePostRowSaveEvent($migration, $message_service->reveal(), $row, [1,2,3]);
+    $event = new MigratePostRowSaveEvent($migration, $message_service->reveal(), $row, [1, 2, 3]);
     $this->assertSame($row, $event->getRow());
   }

Coding standards fixed on commit too.

  • alexpott committed e5908d6 on 8.2.x
    Issue #2546004 by neclimdul, quietone, hussainweb, benjy, phenaproxima,...

  • alexpott committed e5908d6 on 8.3.x
    Issue #2546004 by neclimdul, quietone, hussainweb, benjy, phenaproxima,...

  • alexpott committed e5908d6 on 8.3.x
    Issue #2546004 by neclimdul, quietone, hussainweb, benjy, phenaproxima,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.