Skip to content

Commit

Permalink
Added Troubleshooter for, well, troubleshooting basic setup issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
jawngee committed Nov 9, 2018
1 parent f84cd2c commit 1f1a26d
Show file tree
Hide file tree
Showing 15 changed files with 638 additions and 24 deletions.
22 changes: 19 additions & 3 deletions classes/Cloud/Storage/Driver/Backblaze/BackblazeStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use ILAB\MediaCloud\Cloud\Storage\StorageException;
use ILAB\MediaCloud\Cloud\Storage\StorageInterface;
use ILAB\MediaCloud\Utilities\EnvironmentOptions;
use ILAB\MediaCloud\Utilities\Logging\ErrorCollector;
use ILAB\MediaCloud\Utilities\Logging\Logger;
use ILAB\MediaCloud\Utilities\NoticeManager;

Expand Down Expand Up @@ -109,15 +110,20 @@ public function supportsDirectUploads() {
return false;
}

public function validateSettings() {
/**
* @param ErrorCollector|null $errorCollector
* @return bool|void
*/
public function validateSettings($errorCollector = null) {
delete_option('ilab-backblaze-settings-error');
$this->settingsError = false;

$this->client = null;
$valid = false;

if($this->enabled()) {
$client = $this->getClient();

$valid = false;
if($client) {
$buckets = $client->listBuckets();
foreach($buckets as $bucket) {
Expand All @@ -127,6 +133,12 @@ public function validateSettings() {
break;
}
}

if (!$valid) {
if ($errorCollector) {
$errorCollector->addError("Unable to find bucket named {$this->bucket}.");
}
}
}

if(!$valid) {
Expand All @@ -135,7 +147,11 @@ public function validateSettings() {
} else {
$this->client = $client;
}
}
} else {
if ($errorCollector) {
$errorCollector->addError("Backblaze settings are missing or incorrect.");
}
}
}

public function enabled() {
Expand Down
37 changes: 31 additions & 6 deletions classes/Cloud/Storage/Driver/Google/GoogleStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use ILAB\MediaCloud\Cloud\Storage\StorageException;
use ILAB\MediaCloud\Cloud\Storage\StorageInterface;
use ILAB\MediaCloud\Utilities\EnvironmentOptions;
use ILAB\MediaCloud\Utilities\Logging\ErrorCollector;
use ILAB\MediaCloud\Utilities\Logging\Logger;
use ILAB\MediaCloud\Utilities\NoticeManager;
use function PHPSTORM_META\type;
Expand Down Expand Up @@ -124,20 +125,29 @@ public function supportsDirectUploads() {
return true;
}

public function validateSettings() {
/**
* @param ErrorCollector|null $errorCollector
* @return bool
* @throws StorageException
*/
public function validateSettings($errorCollector = null) {
delete_option('ilab-google-settings-error');
$this->settingsError = false;

$this->client = null;
$valid = false;
if($this->enabled()) {
$client = $this->getClient();
$client = $this->getClient($errorCollector);

$valid = false;
if($client) {
try {
if($client->bucket($this->bucket)->exists()) {
$valid = true;
} else {
if ($errorCollector) {
$errorCollector->addError("Bucket {$this->bucket} does not exist.");
}

Logger::info("Bucket does not exist.");
}
} catch (ServiceException $ex) {
Expand All @@ -152,7 +162,13 @@ public function validateSettings() {
} else {
$this->client = $client;
}
}
} else {
if ($errorCollector) {
$errorCollector->addError("Google configuration is incorrect or missing.");
}
}

return $valid;
}

public function enabled() {
Expand All @@ -172,11 +188,16 @@ public function enabled() {

//region Client Creation
/**
* @param ErrorCollector|null $errorCollector
* @return StorageClient|null
*/
protected function getClient() {
protected function getClient($errorCollector = null) {
if(!$this->enabled()) {
return null;
if ($errorCollector) {
$errorCollector->addError("Google configuration is incorrect or missing.");
}

return null;
}

$client = null;
Expand All @@ -188,6 +209,10 @@ protected function getClient() {
}

if(!$client) {
if ($errorCollector) {
$errorCollector->addError("Google configuration is incorrect or missing.");
}

Logger::info('Could not create Google storage client.');
}

Expand Down
72 changes: 60 additions & 12 deletions classes/Cloud/Storage/Driver/S3/S3Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ILAB\MediaCloud\Cloud\Storage\StorageInterface;
use ILAB\MediaCloud\Cloud\Storage\StorageManager;
use ILAB\MediaCloud\Utilities\EnvironmentOptions;
use ILAB\MediaCloud\Utilities\Logging\ErrorCollector;
use ILAB\MediaCloud\Utilities\Logging\Logger;
use ILAB\MediaCloud\Utilities\NoticeManager;
use ILAB_Aws\Exception\AwsException;
Expand Down Expand Up @@ -127,7 +128,7 @@ public function __construct() {
}
}

$this->client = $this->getClient();
$this->client = $this->getClient(null);
}
//endregion

Expand Down Expand Up @@ -171,15 +172,19 @@ protected function settingsErrorOptionName() {
return 'ilab-s3-settings-error';
}

public function validateSettings() {
/**
* @param ErrorCollector|null $errorCollector
* @return bool
*/
public function validateSettings($errorCollector = null) {
delete_option($this->settingsErrorOptionName());
$this->settingsError = false;

$valid = false;
$this->client = null;
if($this->enabled()) {
$client = $this->getClient();
$client = $this->getClient($errorCollector);

$valid = false;
if($client) {
if($client->doesBucketExist($this->bucket)) {
$valid = true;
Expand All @@ -198,10 +203,20 @@ public function validateSettings() {
}
}

Logger::info("Bucket does not exist.");
if (!$valid) {
if ($errorCollector) {
$errorCollector->addError("Bucket {$this->bucket} does not exist.");
}

Logger::info("Bucket does not exist.");
}
}
catch(AwsException $ex) {
Logger::error("Error insuring bucket exists.", ['exception' => $ex->getMessage()]);
if ($errorCollector) {
$errorCollector->addError("Error insuring that {$this->bucket} exists. Message: ".$ex->getMessage());
}

Logger::error("Error insuring bucket exists.", ['exception' => $ex->getMessage()]);
}
}
}
Expand All @@ -212,7 +227,13 @@ public function validateSettings() {
} else {
$this->client = $client;
}
}
} else {
if ($errorCollector) {
$errorCollector->addError("Account ID, account secret and/or the bucket are incorrect or missing.");
}
}

return $valid;
}

public function enabled() {
Expand Down Expand Up @@ -307,11 +328,12 @@ protected function getS3MultiRegionClient() {
/**
* Attempts to build the S3Client. This requires a region be defined or determinable.
*
* @param bool $region
* @param bool $region
* @param ErrorCollector|null $errorCollector
*
* @return S3Client|null
*/
protected function getS3Client($region = false) {
protected function getS3Client($region = false, $errorCollector = null) {
if(!$this->enabled()) {
return null;
}
Expand All @@ -320,6 +342,10 @@ protected function getS3Client($region = false) {
if(empty($this->region)) {
$this->region = $this->getBucketRegion();
if(empty($this->region)) {
if ($errorCollector) {
$errorCollector->addError('Could not determine region.');
}

Logger::info("Could not get region from server.");

return null;
Expand All @@ -332,6 +358,10 @@ protected function getS3Client($region = false) {
}

if(empty($region)) {
if ($errorCollector) {
$errorCollector->addError("Could not determine region or the region was not specified.");
}

return null;
}

Expand Down Expand Up @@ -361,15 +391,33 @@ protected function getS3Client($region = false) {
}


protected function getClient() {
/**
* Gets the S3Client
* @param ErrorCollector|null $errorCollector
* @return S3Client|S3MultiRegionClient|null
*/
protected function getClient($errorCollector = null) {
if(!$this->enabled()) {
if ($errorCollector) {
$errorCollector->addError("Account ID, account secret and/or the bucket are incorrect or missing.");
}

return null;
}

$s3 = $this->getS3Client();
$s3 = $this->getS3Client(false, $errorCollector);
if(!$s3) {
Logger::info('Could not create regular client, creating multi-region client instead.');
$s3 = $this->getS3MultiRegionClient();

if ($errorCollector) {
$errorCollector->addError("Could not create regular client, creating multi-region client instead.");
}

$s3 = $this->getS3MultiRegionClient();

if (!$s3 && $errorCollector) {
$errorCollector->addError("Could not create regular client, creating multi-region client instead.");
}
}

return $s3;
Expand Down
5 changes: 4 additions & 1 deletion classes/Cloud/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace ILAB\MediaCloud\Cloud\Storage;

use ILAB\MediaCloud\Utilities\Logging\ErrorCollector;

if (!defined('ABSPATH')) { header('Location: /'); die; }

/**
Expand Down Expand Up @@ -87,9 +89,10 @@ public function enabled();
/**
* Validates settings.
*
* @param ErrorCollector|null $errorCollector
* @return bool
*/
public function validateSettings();
public function validateSettings($errorCollector = null);

/**
* Returns the name of the bucket this storage is using.
Expand Down
20 changes: 19 additions & 1 deletion classes/Tasks/BatchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ILAB\MediaCloud\Tasks;

use function ILAB\MediaCloud\Utilities\json_response;
use ILAB\MediaCloud\Utilities\Logging\ErrorCollector;
use ILAB\MediaCloud\Utilities\Logging\Logger;
use ILAB\MediaCloud\Utilities\NoticeManager;

Expand Down Expand Up @@ -429,7 +430,12 @@ public function dispatchBatchesIfNeeded() {
}
}

protected function testConnectivity() {
/**
* Tests connectivity to for the bulk importer
* @param ErrorCollector|null $errorCollector
* @return array|bool|\WP_Error
*/
public function testConnectivity($errorCollector = null) {
$url = add_query_arg(['action' => 'ilab_batch_test', 'nonce' => wp_create_nonce('ilab_batch_test')], admin_url('admin-ajax.php'));
$args = [
'timeout' => 0.01,
Expand All @@ -443,15 +449,27 @@ protected function testConnectivity() {
$result = wp_remote_post($rawUrl, $args);

if (is_wp_error($result)) {
if ($errorCollector) {
$errorCollector->addError("Could not connect to the server for bulk background processing. The error was: ".$result->get_error_message());
}

Logger::error("Testing connectivity to the site for background processing failed. Error was: ".$result->get_error_message());
return $result;
} else if ($result['response']['code'] != 200) {
if ($errorCollector) {
$errorCollector->addError("Could not connect to the server for bulk background processing. The server returned a {$result['response']['code']} response code.");
}

Logger::error("Testing connectivity to the site for background processing failed. Site returned a {$result['response']['code']} status.", ['body' => $result['body']]);
return $result;
}

$json = json_decode($result['body'], true);
if (empty($json) || !isset($json['test']) || (isset($json['test']) && ($json['test'] != 'worked'))) {
if ($errorCollector) {
$errorCollector->addError("Was able to connect to the server for bulk background processing but the JSON response was incorrect: ".$result['body']);
}

Logger::error("Testing connectivity to the site for background processing failed. Was able to connect to the site but the JSON response was not expected.", ['body' => $result['body']]);
return new \WP_Error(500, "The server response from the connectivity test was not in the expected format.");
}
Expand Down
Loading

0 comments on commit 1f1a26d

Please sign in to comment.