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 /**
@@ -130,6 +139,30 @@ protected function executeRequest() {
130139 $ this ->edgeDevelopers = $ this ->loadDevelopers ();
131140 }
132141
142+ /**
143+ * Schedules the update of a user.
144+ *
145+ * @param \Drupal\user\UserInterface $user
146+ * The user.
147+ */
148+ protected function scheduleUserUpdate (UserInterface $ user ): void {
149+ $ update_user_job = new UserUpdate ($ user ->getEmail ());
150+ $ update_user_job ->setTag ($ this ->getTag ());
151+ $ this ->scheduleJob ($ update_user_job );
152+ }
153+
154+ /**
155+ * Schedules the update of a developer.
156+ *
157+ * @param \Drupal\apigee_edge\Entity\DeveloperInterface $developer
158+ * The developer.
159+ */
160+ protected function scheduleDeveloper (DeveloperInterface $ developer ): void {
161+ $ update_developer_job = new DeveloperUpdate ($ developer ->getEmail ());
162+ $ update_developer_job ->setTag ($ this ->getTag ());
163+ $ this ->scheduleJob ($ update_developer_job );
164+ }
165+
133166 /**
134167 * {@inheritdoc}
135168 */
@@ -144,20 +177,31 @@ public function execute(): bool {
144177 /** @var \Drupal\user\UserInterface $user */
145178 $ user = $ this ->drupalUsers [$ clean_email ];
146179
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 );
180+ $ last_synced = $ this ->lastUpdateTracker ->get ($ developer ->getEmail (), 0 );
181+
182+ if ($ last_synced === 0 ) {
183+ $ last_modified_delta = $ developer ->getLastModifiedAt ()->getTimestamp () - $ user ->getChangedTime ();
184+ if ($ last_modified_delta === 0 ) {
185+ $ this ->lastUpdateTracker ->set ($ developer ->getEmail (), \Drupal::time ()->getCurrentTime ());
186+ continue ;
187+ }
188+
189+ // Update Drupal user because the Apigee Edge developer is the most
190+ // recent.
191+ if ($ last_modified_delta > 0 ) {
192+ $ this ->scheduleUserUpdate ($ user );
193+ }
194+ // Update Apigee Edge developer because the Drupal user is the most
195+ // recent.
196+ else {
197+ $ this ->scheduleDeveloper ($ developer );
198+ }
199+ }
200+ elseif ($ last_synced < $ developer ->getLastModifiedAt ()->getTimestamp ()) {
201+ $ this ->scheduleUserUpdate ($ user );
154202 }
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 );
203+ elseif ($ last_synced < $ user ->getChangedTime ()) {
204+ $ this ->scheduleDeveloper ($ developer );
161205 }
162206 }
163207
0 commit comments