| @@ -69,13 +69,30 @@ | ||
| 69 | 69 | /** |
| 70 | 70 | * Starts the sync process. |
| 71 | 71 | * |
| 72 | 72 | */ |
| 73 | - public function start( $action ) { | |
| 73 | + public function start( $action, $options = [] ) { | |
| 74 | 74 | $action_class = $this->get_class_by_action($action); |
| 75 | 75 | |
| 76 | 76 | if($action_class) { |
| 77 | - $action_class->start(); | |
| 77 | + // BGRunner's lock is per-type — nothing else stops two different actions from running at once. | |
| 78 | + $blocking_action = $this->find_running_action($action); | |
| 79 | + if ($blocking_action) { | |
| 80 | + return [ | |
| 81 | + 'success' => false, | |
| 82 | + /* translators: %s: the action that is currently running */ | |
| 83 | + 'message' => sprintf(__('Another sync (%s) is currently running. Stop or finish it before starting a new one.', 'media-cloud-sync'), $blocking_action), | |
| 84 | + ]; | |
| 85 | + } | |
| 86 | + | |
| 87 | + $start_result = $action_class->start( $options ); | |
| 88 | + // A job's start() can signal a precondition failure (e.g. no destination | |
| 89 | + // configured) by returning ['success' => false, ...] instead of the usual | |
| 90 | + // void/true — short-circuit and pass that straight through instead of masking | |
| 91 | + // it with a status object that always reads as started. | |
| 92 | + if (is_array($start_result) && isset($start_result['success']) && $start_result['success'] === false) { | |
| 93 | + return $start_result; | |
| 94 | + } | |
| 78 | 95 | return $action_class->get_status(); |
| 79 | 96 | } else { |
| 80 | 97 | /* translators: %s: action name */ |
| 81 | 98 | throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action)); |
| @@ -81,8 +98,27 @@ | ||
| 81 | 98 | throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action)); |
| 82 | 99 | } |
| 83 | 100 | } |
| 84 | 101 | |
| 102 | + // Action string of another registered action currently running/paused, or null. | |
| 103 | + private function find_running_action( $except_action ) { | |
| 104 | + $runner = BGRunner::instance(); | |
| 105 | + foreach ($this->syncClasses as $class) { | |
| 106 | + $class_with_namespace = __NAMESPACE__.'\\'. trim($class); | |
| 107 | + if(!class_exists($class_with_namespace)) continue; | |
| 108 | + | |
| 109 | + $instance = $class_with_namespace::instance(); | |
| 110 | + $other_action = $instance->get_action(); | |
| 111 | + if ($other_action === $except_action) continue; | |
| 112 | + | |
| 113 | + $status = $runner->status($other_action); | |
| 114 | + if (in_array($status['status'] ?? 'stopped', ['running', 'paused'], true)) { | |
| 115 | + return $other_action; | |
| 116 | + } | |
| 117 | + } | |
| 118 | + return null; | |
| 119 | + } | |
| 120 | + | |
| 85 | 121 | /** |
| 86 | 122 | * Pauses the sync process. |
| 87 | 123 | * |
| 88 | 124 | */ |
| @@ -115,13 +151,13 @@ | ||
| 115 | 151 | |
| 116 | 152 | |
| 117 | 153 | /** |
| 118 | 154 | * Stops the sync process. |
| 119 | - * | |
| 155 | + * | |
| 120 | 156 | */ |
| 121 | 157 | public function stop( $action ) { |
| 122 | 158 | $action_class = $this->get_class_by_action($action); |
| 123 | - | |
| 159 | + | |
| 124 | 160 | if($action_class) { |
| 125 | 161 | $action_class->stop(); |
| 126 | 162 | return $action_class->get_status(); |
| 127 | 163 | } else { |
| @@ -126,8 +162,44 @@ | ||
| 126 | 162 | return $action_class->get_status(); |
| 127 | 163 | } else { |
| 128 | 164 | throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action)); |
| 129 | 165 | } |
| 166 | + } | |
| 167 | + | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Sets the sync method ('ajax'|'cron'|'mixed') preference for the given action's type. | |
| 171 | + */ | |
| 172 | + public function set_sync_method( $action, $method ) { | |
| 173 | + $action_class = $this->get_class_by_action($action); | |
| 174 | + | |
| 175 | + if($action_class) { | |
| 176 | + return $action_class->set_sync_method($method); | |
| 177 | + } else { | |
| 178 | + throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action)); | |
| 179 | + } | |
| 180 | + } | |
| 181 | + | |
| 182 | + | |
| 183 | + /** | |
| 184 | + * Ticks the sync process — processes a small batch synchronously (ajax/mixed sync mode). | |
| 185 | + * Gracefully no-ops (returns current status unchanged) for an action whose class hasn't | |
| 186 | + * been updated with a tick() method yet — it's still safely progressing via cron, this | |
| 187 | + * just means ajax ticking isn't wired up for it yet, not a failure. | |
| 188 | + * @since 1.3.13 | |
| 189 | + */ | |
| 190 | + public function tick( $action, $batch = 5 ) { | |
| 191 | + $action_class = $this->get_class_by_action($action); | |
| 192 | + | |
| 193 | + if(!$action_class) { | |
| 194 | + throw new \Exception(sprintf(__('Invalid action: %s', 'media-cloud-sync'), $action)); | |
| 195 | + } | |
| 196 | + | |
| 197 | + if(method_exists($action_class, 'tick')) { | |
| 198 | + return $action_class->tick($batch); | |
| 199 | + } | |
| 200 | + | |
| 201 | + return $action_class->get_status(); | |
| 130 | 202 | } |
| 131 | 203 | |
| 132 | 204 | |
| 133 | 205 | /** |