2020namespace Drupal \apigee_edge \Job ;
2121
2222use Drupal \apigee_edge \Entity \Developer ;
23+ use Drupal \apigee_edge \Entity \DeveloperInterface ;
24+ use Drupal \Core \KeyValueStore \KeyValueStoreInterface ;
2325use Drupal \user \Entity \User ;
26+ use Drupal \user \UserInterface ;
2427
2528/**
2629 * A job that synchronizes Apigee Edge developers and Drupal users.
@@ -58,6 +61,11 @@ class DeveloperSync extends EdgeJob {
5861 */
5962 protected $ filter = NULL ;
6063
64+ /**
65+ * KV store tracks last update attempts for each user/developer.
66+ */
67+ protected KeyValueStoreInterface $ lastUpdateTracker ;
68+
6169 /**
6270 * DeveloperSync constructor.
6371 *
@@ -67,6 +75,7 @@ class DeveloperSync extends EdgeJob {
6775 public function __construct (?string $ filter ) {
6876 parent ::__construct ();
6977 $ this ->filter = $ filter ;
78+ $ this ->lastUpdateTracker = \Drupal::service ('apigee_edge.dev_sync.last_update_tracker ' );
7079 }
7180
7281 /**
@@ -136,6 +145,18 @@ protected function executeRequest() {
136145 public function execute (): bool {
137146 parent ::execute ();
138147
148+ $ update_drupal_user = function (UserInterface $ user ) {
149+ $ update_user_job = new UserUpdate ($ user ->getEmail ());
150+ $ update_user_job ->setTag ($ this ->getTag ());
151+ $ this ->scheduleJob ($ update_user_job );
152+ };
153+
154+ $ update_developer = function (DeveloperInterface $ developer ) {
155+ $ update_developer_job = new DeveloperUpdate ($ developer ->getEmail ());
156+ $ update_developer_job ->setTag ($ this ->getTag ());
157+ $ this ->scheduleJob ($ update_developer_job );
158+ };
159+
139160 // Update Apigee Edge developers and Drupal users if needed.
140161 $ identical_entities = array_intersect_key ($ this ->edgeDevelopers , $ this ->drupalUsers );
141162 foreach ($ identical_entities as $ clean_email => $ entity ) {
@@ -144,20 +165,31 @@ public function execute(): bool {
144165 /** @var \Drupal\user\UserInterface $user */
145166 $ user = $ this ->drupalUsers [$ clean_email ];
146167
147- $ last_modified_delta = $ developer ->getLastModifiedAt ()->getTimestamp () - $ user ->getChangedTime ();
148- // Update Drupal user because the Apigee Edge developer is the most
149- // recent.
150- if ($ last_modified_delta > 0 ) {
151- $ update_user_job = new UserUpdate ($ user ->getEmail ());
152- $ update_user_job ->setTag ($ this ->getTag ());
153- $ this ->scheduleJob ($ update_user_job );
168+ $ last_synced = $ this ->lastUpdateTracker ->get ($ developer ->getEmail (), 0 );
169+
170+ if ($ last_synced === 0 ) {
171+ $ last_modified_delta = $ developer ->getLastModifiedAt ()->getTimestamp () - $ user ->getChangedTime ();
172+ if ($ last_modified_delta === 0 ) {
173+ $ this ->lastUpdateTracker ->set ($ developer ->getEmail (), \Drupal::time ()->getCurrentTime ());
174+ continue ;
175+ }
176+
177+ // Update Drupal user because the Apigee Edge developer is the most
178+ // recent.
179+ if ($ last_modified_delta > 0 ) {
180+ $ update_drupal_user ($ user );
181+ }
182+ // Update Apigee Edge developer because the Drupal user is the most
183+ // recent.
184+ else {
185+ $ update_developer ($ developer );
186+ }
187+ }
188+ elseif ($ last_synced < $ developer ->getLastModifiedAt ()->getTimestamp ()) {
189+ $ update_drupal_user ($ user );
154190 }
155- // Update Apigee Edge developer because the Drupal user is the most
156- // recent.
157- elseif ($ last_modified_delta < 0 ) {
158- $ update_developer_job = new DeveloperUpdate ($ developer ->getEmail ());
159- $ update_developer_job ->setTag ($ this ->getTag ());
160- $ this ->scheduleJob ($ update_developer_job );
191+ elseif ($ last_synced < $ user ->getChangedTime ()) {
192+ $ update_developer ($ developer );
161193 }
162194 }
163195
0 commit comments