class Google::Cloud::PubSub::Snapshot

# Snapshot

A named resource created from a subscription to retain a stream of messages from a topic. A snapshot is guaranteed to retain:

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
sub = pubsub.subscription "my-sub"

snapshot = sub.create_snapshot "my-snapshot"
snapshot.name #=> "projects/my-project/snapshots/my-snapshot"

Attributes

grpc[RW]

@private The gRPC Google::Cloud::PubSub::V1::Snapshot object.

service[RW]

@private The Service object.

Public Class Methods

from_grpc(grpc, service) click to toggle source

@private New Snapshot from a Google::Cloud::PubSub::V1::Snapshot object.

# File lib/google/cloud/pubsub/snapshot.rb, line 178
def self.from_grpc grpc, service
  new.tap do |f|
    f.grpc = grpc
    f.service = service
  end
end
new() click to toggle source

@private Create an empty {Snapshot} object.

# File lib/google/cloud/pubsub/snapshot.rb, line 56
def initialize
  @service = nil
  @grpc = Google::Cloud::PubSub::V1::Snapshot.new
end
timestamp_from_grpc(grpc_timestamp) click to toggle source

@private Get a Time object from a Google::Protobuf::Timestamp object.

# File lib/google/cloud/pubsub/snapshot.rb, line 187
def self.timestamp_from_grpc grpc_timestamp
  return nil if grpc_timestamp.nil?
  Time.at grpc_timestamp.seconds, Rational(grpc_timestamp.nanos, 1000)
end

Public Instance Methods

delete() click to toggle source

Removes an existing snapshot. All messages retained in the snapshot are immediately dropped. After a snapshot is deleted, a new one may be created with the same name, but the new one has no association with the old snapshot or its subscription, unless the same subscription is specified.

@return [Boolean] Returns `true` if the snapshot was deleted.

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

pubsub.snapshots.each do |snapshot|
  snapshot.delete
end
# File lib/google/cloud/pubsub/snapshot.rb, line 169
def delete
  ensure_service!
  service.delete_snapshot name
  true
end
expiration_time() click to toggle source

The snapshot is guaranteed to exist up until this time. A newly-created snapshot expires no later than 7 days from the time of its creation. Its exact lifetime is determined at creation by the existing backlog in the source subscription. Specifically, the lifetime of the snapshot is 7 days - (age of oldest unacked message in the subscription). For example, consider a subscription whose oldest unacked message is 3 days old. If a snapshot is created from this subscription, the snapshot – which will always capture this 3-day-old backlog as long as the snapshot exists – will expire in 4 days.

@return [Time] The time until which the snapshot is guaranteed to

exist.

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
sub = pubsub.subscription "my-sub"

snapshot = sub.create_snapshot "my-snapshot"
snapshot.topic.name #=> "projects/my-project/topics/my-topic"
snapshot.expiration_time
# File lib/google/cloud/pubsub/snapshot.rb, line 112
def expiration_time
  self.class.timestamp_from_grpc @grpc.expire_time
end
labels() click to toggle source

A hash of user-provided labels associated with this snapshot. Labels can be used to organize and group snapshots.See [Creating and Managing Labels](cloud.google.com/pubsub/docs/labels).

The returned hash is frozen and changes are not allowed. Use {#labels=} to update the labels for this snapshot.

@return [Hash] The frozen labels hash.

# File lib/google/cloud/pubsub/snapshot.rb, line 126
def labels
  @grpc.labels.to_h.freeze
end
labels=(new_labels) click to toggle source

Sets the hash of user-provided labels associated with this snapshot. Labels can be used to organize and group snapshots. Label keys and values can be no longer than 63 characters, can only contain lowercase letters, numeric characters, underscores and dashes. International characters are allowed. Label values are optional. Label keys must start with a letter and each label in the list must have a different key. See [Creating and Managing Labels](cloud.google.com/pubsub/docs/labels).

@param [Hash] new_labels The new labels hash.

# File lib/google/cloud/pubsub/snapshot.rb, line 142
def labels= new_labels
  raise ArgumentError, "Value must be a Hash" if new_labels.nil?
  labels_map = Google::Protobuf::Map.new :string, :string
  Hash(new_labels).each { |k, v| labels_map[String(k)] = String(v) }
  update_grpc = @grpc.dup
  update_grpc.labels = labels_map
  @grpc = service.update_snapshot update_grpc, :labels
end
name() click to toggle source

The name of the snapshot.

@return [String] A fully-qualified snapshot name in the form

`projects/{project_id}/snapshots/{snapshot_id}`.
# File lib/google/cloud/pubsub/snapshot.rb, line 66
def name
  @grpc.name
end
topic() click to toggle source

The {Topic} from which this snapshot is retaining messages.

@return [Topic]

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new
sub = pubsub.subscription "my-sub"

snapshot = sub.create_snapshot "my-snapshot"
snapshot.topic.name #=> "projects/my-project/topics/my-topic"
# File lib/google/cloud/pubsub/snapshot.rb, line 84
def topic
  Topic.from_name @grpc.topic, service
end

Protected Instance Methods

ensure_service!() click to toggle source

@private Raise an error unless an active connection to the service is available.

# File lib/google/cloud/pubsub/snapshot.rb, line 197
def ensure_service!
  raise "Must have active connection to service" unless service
end