10
10
use Algolia \AlgoliaSearch \Exception \ReplicaLimitExceededException ;
11
11
use Algolia \AlgoliaSearch \Exceptions \BadRequestException ;
12
12
use Algolia \AlgoliaSearch \Helper \Entity \ProductHelper ;
13
+ use Algolia \AlgoliaSearch \Registry \ReplicaState ;
13
14
use Algolia \AlgoliaSearch \Service \StoreNameFetcher ;
14
- use Magento \Framework \App \State ;
15
+ use Magento \Framework \App \State as AppState ;
15
16
use Magento \Framework \Console \Cli ;
16
17
use Magento \Store \Model \StoreManagerInterface ;
17
18
use Symfony \Component \Console \Input \InputInterface ;
@@ -28,12 +29,13 @@ public function __construct(
28
29
protected ProductHelper $ productHelper ,
29
30
protected ReplicaManagerInterface $ replicaManager ,
30
31
protected StoreManagerInterface $ storeManager ,
31
- State $ state ,
32
+ protected ReplicaState $ replicaState ,
33
+ AppState $ appState ,
32
34
StoreNameFetcher $ storeNameFetcher ,
33
35
?string $ name = null
34
36
)
35
37
{
36
- parent ::__construct ($ state , $ storeNameFetcher , $ name );
38
+ parent ::__construct ($ appState , $ storeNameFetcher , $ name );
37
39
}
38
40
39
41
protected function getReplicaCommandName (): string
@@ -66,6 +68,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
66
68
$ output ->writeln ($ this ->decorateOperationAnnouncementMessage ('Rebuilding replicas for {{target}} ' , $ storeIds ));
67
69
68
70
$ this ->deleteReplicas ($ storeIds );
71
+ $ this ->forceState ($ storeIds );
72
+
69
73
try {
70
74
$ this ->syncReplicas ($ storeIds );
71
75
} catch (ReplicaLimitExceededException $ e ) {
@@ -83,4 +87,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
83
87
return Cli::RETURN_SUCCESS ;
84
88
}
85
89
90
+ /**
91
+ * Force the replica change state to always sync the replica configuration
92
+ * Also serves to avoid latency from Algolia API when reading replica configuration for comparison with local Magento config
93
+ * @param int[] $storeIds
94
+ * @return void
95
+ */
96
+ protected function forceState (array $ storeIds ): void
97
+ {
98
+ if (!count ($ storeIds )) {
99
+ $ storeIds = array_keys ($ this ->storeManager ->getStores ());
100
+ }
101
+ foreach ($ storeIds as $ storeId ) {
102
+ $ this ->replicaState ->setChangeState (ReplicaState::REPLICA_STATE_CHANGED , $ storeId );
103
+ }
104
+ }
105
+
86
106
}
0 commit comments