-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspace-replication-status-check-and-log-5.patch
162 lines (152 loc) · 6.15 KB
/
workspace-replication-status-check-and-log-5.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
diff --git a/src/Plugin/QueueWorker/WorkspaceReplication.php b/src/Plugin/QueueWorker/WorkspaceReplication.php
index 92e5147..83ceba5 100644
--- a/src/Plugin/QueueWorker/WorkspaceReplication.php
+++ b/src/Plugin/QueueWorker/WorkspaceReplication.php
@@ -3,6 +3,8 @@
namespace Drupal\workspace\Plugin\QueueWorker;
use Drupal\Component\Datetime\Time;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\Queue\RequeueException;
@@ -10,6 +12,7 @@ use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\State\StateInterface;
use Drupal\replication\Entity\ReplicationLogInterface;
use Drupal\user\Entity\User;
+use Drupal\workspace\Entity\Replication;
use Drupal\workspace\ReplicatorManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -25,25 +28,47 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkspaceReplication extends QueueWorkerBase implements ContainerFactoryPluginInterface {
/**
+ * The replicator manager.
+ *
* @var \Drupal\workspace\ReplicatorManager
*/
protected $replicatorManager;
/**
+ * Time service.
+ *
* @var \Drupal\Component\Datetime\Time
*/
protected $time;
/**
+ * The service for safe account switching.
+ *
* @var \Drupal\Core\Session\AccountSwitcherInterface
*/
protected $accountSwitcher;
/**
+ * State system service.
+ *
* @var \Drupal\Core\State\StateInterface
*/
private $state;
+ /**
+ * The logger.
+ *
+ * @var \Drupal\Core\Logger\LoggerChannelInterface
+ */
+ protected $logger;
+
+ /**
+ * The entity type manager.
+ *
+ * @var \Drupal\Core\Entity\EntityTypeManagerInterface
+ */
+ protected $entityTypeManager;
+
/**
* {@inheritdoc}
*/
@@ -55,51 +80,73 @@ class WorkspaceReplication extends QueueWorkerBase implements ContainerFactoryPl
$container->get('workspace.replicator_manager'),
$container->get('datetime.time'),
$container->get('account_switcher'),
- $container->get('state')
+ $container->get('state'),
+ $container->get('logger.factory'),
+ $container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
- public function __construct(array $configuration, $plugin_id, $plugin_definition, ReplicatorManager $replicator_manager, Time $time, AccountSwitcherInterface $account_switcher, StateInterface $state) {
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, ReplicatorManager $replicator_manager, Time $time, AccountSwitcherInterface $account_switcher, StateInterface $state, LoggerChannelFactoryInterface $logger, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->replicatorManager = $replicator_manager;
$this->time = $time;
$this->accountSwitcher = $account_switcher;
$this->state = $state;
+ $this->logger = $logger->get('workspace');
+ $this->entityTypeManager = $entity_type_manager;
}
/**
- * @param mixed $data
+ * {@inheritdoc}
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function processItem($data) {
if ($this->state->get('workspace.last_replication_failed', FALSE)) {
// Requeue if replication blocked.
- throw new RequeueException('Replication blocked now!');
+ throw new RequeueException('Replication is blocked!');
}
- $account = User::load(1);
- $this->accountSwitcher->switchTo($account);
- /**
- * @var \Drupal\workspace\Entity\Replication $replication
- */
+
+ /** @var \Drupal\workspace\Entity\Replication $replication */
$replication = $data['replication'];
- $replication->setReplicationStatusReplicating();
- $replication->save();
- $response = $this->replicatorManager->doReplication($data['source'], $data['target'], $data['task']);
- if (($response instanceof ReplicationLogInterface) && ($response->get('ok')->value == TRUE)) {
- $replication->setReplicationStatusReplicated();
- $replication->set('replicated', $this->time->getRequestTime());
+
+ if ($replication_new = $this->entityTypeManager->getStorage('replication')->load($replication->id())) {
+ $replication = $replication_new;
+ }
+
+ $this->logger->info('Replication status is "@replication_status".', ['@replication_status' => $replication->get('replication_status')->value]);
+ if ($replication->get('replication_status')->value == Replication::QUEUED) {
+ $account = User::load(1);
+ $this->accountSwitcher->switchTo($account);
+
+ $replication->setReplicationStatusReplicating();
$replication->save();
+ $this->logger->info('Replication "@replication" has started.', ['@replication' => $replication->label()]);
+
+ $response = $this->replicatorManager->doReplication($data['source'], $data['target'], $data['task']);
+ if (($response instanceof ReplicationLogInterface) && ($response->get('ok')->value == TRUE)) {
+ $replication->setReplicationStatusReplicated();
+ $replication->set('replicated', $this->time->getRequestTime());
+ $replication->save();
+ $this->logger->info('Replication "@replication" has finished successfully.', ['@replication' => $replication->label()]);
+ }
+ else {
+ $replication->setReplicationStatusFailed();
+ $replication->save();
+ $this->state->set('workspace.last_replication_failed', TRUE);
+ $this->logger->info('Replication "@replication" has failed.', ['@replication' => $replication->label()]);
+ }
+
+ $this->accountSwitcher->switchBack();
}
else {
- $replication->setReplicationStatusFailed();
- $replication->save();
- $this->state->set('workspace.last_replication_failed', TRUE);
+ // Requeue if replication is in progress.
+ $this->logger->info('Replication "@replication" is already in progress.', ['@replication' => $replication->label()]);
+ throw new RequeueException('Replication is already in progress!');
}
- $this->accountSwitcher->switchBack();
}
}