/// <summary> /// Reads the checkpoint information from the file for partition id /// </summary> /// <param name="partitionId">The partition id the checkpoint data is for</param> /// <returns>A checkpoint instance populated from the file</returns> private async Task <CheckpointEntry> ReadCheckpointFromFileAsync(string partitionId) { CheckpointEntry result = null; using (_logger.BeginScope("Read Checkpoint")) { _checkpointReadCounter.Increment(); _logger.LogDebug("Reading checkpoint partition {partitionId}", partitionId); var fileName = GetFileName(partitionId); if (File.Exists(fileName)) { _logger.LogDebug("File {fileName} exists", fileName); try { var json = await File.ReadAllTextAsync(fileName, ContainerLifecycle.CancellationToken).ConfigureAwait(false); if (string.IsNullOrWhiteSpace(json)) { _logger.LogInformation("File {fileName} was empty", fileName); } else { _logger.LogInformation("File {fileName} had the following content: {content}", fileName, json); try { result = CheckpointEntry.FromJson(json); } catch (Exception e) { _logger.LogError(e, "Error deserializing checkpoint contents"); _checkpointErrorCounter.Increment(); } } } catch (Exception e) { _logger.LogError(e, "Error reading checkpoint file {fileName}", fileName); _checkpointErrorCounter.Increment(); } } else { _logger.LogInformation("File {fileName} does not exists", fileName); } } return(result); }
/// <inheritdoc /> public async Task <Checkpoint> CreateCheckpointIfNotExistsAsync(string partitionId) { CheckpointEntry checkpoint; using (_logger.BeginScope("Create checkpoint")) { _checkpointUpdateCounter.Increment(); _logger.LogDebug("Checkpoint partition {partitionId}", partitionId); checkpoint = await ReadCheckpointFromFileAsync(partitionId).ConfigureAwait(false); if (checkpoint == null) { _logger.LogInformation("Checkpoint not found for partition {partitionId}, creating", partitionId); var fileName = GetFileName(partitionId); var newCheckpoint = new CheckpointEntry { PartitionId = partitionId, Owner = _eventProcessorName }; var json = newCheckpoint.ToJson(); _logger.LogTrace("Checkpoint json: ", json); try { await File.WriteAllTextAsync(fileName, json, ContainerLifecycle.CancellationToken).ConfigureAwait(false); _logger.LogDebug("Checkpoint complete for file {fileName}", fileName); checkpoint = newCheckpoint; } catch (Exception e) { _logger?.LogError(e, "Error writing checkpoint file {fileName}", fileName); _checkpointErrorCounter.Increment(); } } else { _logger?.LogTrace("Checkpoint found for partition {partitionId}", partitionId); } } return(checkpoint); }