Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added settings for auto-initialize. Fixes #144 #150

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Akka.Persistence.Azure/Journal/AzureTableStorageJournal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,26 @@ private async Task<CloudTable> InitCloudStorage(
var tableClient = _storageAccount.CreateCloudTableClient();
var tableRef = tableClient.GetTableReference(_settings.TableName);
var op = new OperationContext();

using (var cts = new CancellationTokenSource(_settings.ConnectTimeout))
{
if (!_settings.AutoInitialize)
{
var exists = await tableRef.ExistsAsync(null, null, cts.Token);

if (!exists)
{
remainingTries = 0;

throw new Exception(
$"Table {_settings.TableName} doesn't exist. Either create it or turn auto-initialize on");
}

_log.Info("Successfully connected to existing table", _settings.TableName);

return tableRef;
}

if (await tableRef.CreateIfNotExistsAsync(new TableRequestOptions(), op, cts.Token))
_log.Info("Created Azure Cloud Table", _settings.TableName);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public AzureTableStorageJournalSettings(
TimeSpan connectTimeout,
TimeSpan requestTimeout,
bool verboseLogging,
bool development)
bool development,
bool autoInitialize)
{
if(string.IsNullOrWhiteSpace(tableName))
throw new ConfigurationException("[AzureTableStorageJournal] Table name is null or empty.");
Expand All @@ -43,6 +44,7 @@ public AzureTableStorageJournalSettings(
RequestTimeout = requestTimeout;
VerboseLogging = verboseLogging;
Development = development;
AutoInitialize = autoInitialize;
}

/// <summary>
Expand Down Expand Up @@ -71,6 +73,8 @@ public AzureTableStorageJournalSettings(
public bool VerboseLogging { get; }

public bool Development { get; }

public bool AutoInitialize { get; }

/// <summary>
/// Creates an <see cref="AzureTableStorageJournalSettings" /> instance using the
Expand All @@ -86,14 +90,16 @@ public static AzureTableStorageJournalSettings Create(Config config)
var requestTimeout = config.GetTimeSpan("request-timeout", TimeSpan.FromSeconds(3));
var verbose = config.GetBoolean("verbose-logging", false);
var development = config.GetBoolean("development", false);
var autoInitialize = config.GetBoolean("auto-initialize", true);

return new AzureTableStorageJournalSettings(
connectionString,
tableName,
connectTimeout,
requestTimeout,
verbose,
development);
development,
autoInitialize);
}
}
}
17 changes: 17 additions & 0 deletions src/Akka.Persistence.Azure/Snapshot/AzureBlobSnapshotStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ private async Task<CloudBlobContainer> InitCloudStorage(int remainingTries)

using (var cts = new CancellationTokenSource(_settings.ConnectTimeout))
{
if (!_settings.AutoInitialize)
{
var exists = await containerRef.ExistsAsync(null, null, cts.Token);

if (!exists)
{
remainingTries = 0;

throw new Exception(
$"Container {_settings.ContainerName} doesn't exist. Either create it or turn auto-initialize on");
}

_log.Info("Successfully connected to existing container", _settings.ContainerName);

return containerRef;
}

if (await containerRef.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Container,
new BlobRequestOptions(), op, cts.Token))
_log.Info("Created Azure Blob Container", _settings.ContainerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Akka.Persistence.Azure.Snapshot
public sealed class AzureBlobSnapshotStoreSettings
{
public AzureBlobSnapshotStoreSettings(string connectionString, string containerName,
TimeSpan connectTimeout, TimeSpan requestTimeout, bool verboseLogging, bool development)
TimeSpan connectTimeout, TimeSpan requestTimeout, bool verboseLogging, bool development, bool autoInitialize)
{
if (string.IsNullOrWhiteSpace(containerName))
throw new ConfigurationException("[AzureBlobSnapshotStore] Container name is null or empty.");
Expand All @@ -29,6 +29,7 @@ public AzureBlobSnapshotStoreSettings(string connectionString, string containerN
ConnectTimeout = connectTimeout;
VerboseLogging = verboseLogging;
Development = development;
AutoInitialize = autoInitialize;
}

/// <summary>
Expand Down Expand Up @@ -58,6 +59,8 @@ public AzureBlobSnapshotStoreSettings(string connectionString, string containerN

public bool Development { get; }

public bool AutoInitialize { get; }

/// <summary>
/// Creates an <see cref="AzureBlobSnapshotStoreSettings" /> instance using the
/// `akka.persistence.snapshot-store.azure-blob-store` HOCON configuration section.
Expand All @@ -72,14 +75,16 @@ public static AzureBlobSnapshotStoreSettings Create(Config config)
var requestTimeout = config.GetTimeSpan("request-timeout", TimeSpan.FromSeconds(3));
var verbose = config.GetBoolean("verbose-logging", false);
var development = config.GetBoolean("development", false);
var autoInitialize = config.GetBoolean("auto-initialize", true);

return new AzureBlobSnapshotStoreSettings(
connectionString,
containerName,
connectTimeout,
requestTimeout,
verbose,
development);
development,
autoInitialize);
}
}
}
6 changes: 6 additions & 0 deletions src/Akka.Persistence.Azure/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ akka.persistence {
# Support for Azure Storage Emulator for local development.
# Will ignore connection string settings if turned on.
development = off

# Creates the required table if set
auto-initialize = on
}
}

Expand Down Expand Up @@ -86,6 +89,9 @@ akka.persistence {
# Support for Azure Storage Emulator for local development.
# Will ignore connection string settings if turned on.
development = off

# Creates the required container if set
auto-initialize = on
}
}
}