Class: Towel::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/towel/session.rb

Overview

A session keeps track of an invocation throughout its lifespan. It is meant to be a helper to framework-specific implementations by providing a more convenient API over the raw gRPC calls.

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Session

Returns a new instance of Session.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/towel/session.rb', line 6

def initialize(config = nil)
    @config = config || Towel::Configuration.read
    @stub = create_stub
    @project = [
      "organizations",
      @config["collector"]["organization"],
      "projects",
      @config["collector"]["project"]
    ].join "/"
    @invocation = nil
    @groups = {}
    @group_id = -1
    @results = {}
    @result_id = -1
    @logs = {}
end

Instance Method Details

#cancel_invocationObject

Mark an invocation as cancelled. Do not call this and ‘#finish` on a given Session.



145
146
147
148
149
150
# File 'lib/towel/session.rb', line 145

def cancel_invocation
  request = Towel::V1alpha::CancelInvocationRequest.new
  request.name = @invocation.name

  @stub.cancel_invocation(request)
end

#create_invocationObject

Creates a new invocation. Returns the invocation URL at which users may view results. This must be called before other methods.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/towel/session.rb', line 25

def create_invocation
    invocation = Towel::V1alpha::Invocation.new
    invocation.project = @project
    @config["labels"].each {|k, v| invocation.labels[k] = v }
    invocation.start_time = Time.now.utc

    # Add environment-specific information
    invocation.platform = Towel::Environment.platform
    invocation.hostname = Towel::Environment.hostname
    invocation.user = Towel::Environment.user
    invocation.working_dir = Towel::Environment.working_dir
    invocation.argv.concat(Towel::Environment.argv)
    Towel::Environment.env_vars.each do |name, value|
      invocation.env_vars[name] = value
    end
    invocation.revision = Towel::Environment.revision

    request = Towel::V1alpha::CreateInvocationRequest.new
    request.invocation = invocation

    @invocation = @stub.create_invocation(request)

    @invocation.url
end

#create_log(name) ⇒ Object

Create a new log with a given name. This can be used as-is or wrapped in a LogIO wrapper and used in place of a IO stream.

Raises:



124
125
126
127
128
# File 'lib/towel/session.rb', line 124

def create_log(name)
  raise Error, "Log '#{name}' already exists" if @logs.key?(name)
  resource_name = [@invocation.name, "logs", name].join "/"
  @logs[name] = Log.new(resource_name, @stub)
end

#create_result(group, name, display_name: nil) ⇒ Object

Creates a result under a specified group and mark it as running. Returns the resource name of the result, which can be used to set a Log context.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/towel/session.rb', line 52

def create_result(group, name, display_name: nil)
  group_id = find_or_create_group(group)

  result = Towel::V1alpha::Result.new

  if @results.key?([group_id, name])
    raise Error, "Result already has been created for '#{name}'"
  else
    result_id = @result_id += 1
    @results[[group_id, name]] = result_id
  end

  result.name = [
    @invocation.name,
    "groups",
    group_id,
    "results",
    result_id
  ].join "/"
  result.display_name = display_name || name
  result.start_time = Time.now.utc
  result.state = Towel::V1alpha::ResultState::RUNNING

  request = Towel::V1alpha::CreateResultRequest.new
  request.result = result

  @stub.create_result(request)

  ["groups", group_id, "results", result_id].join "/"
end

#finish_invocationObject

Mark an invocation as finished (ran to termination without being cancelled). Do not call this and ‘#cancel` on a given Session. All outstanding logs will be flushed and closed as part of this.



133
134
135
136
137
138
139
140
141
# File 'lib/towel/session.rb', line 133

def finish_invocation
  @logs.each { |name, log| log.close }

  request = Towel::V1alpha::FinishInvocationRequest.new
  request.name = @invocation.name
  request.end_time = Time.now.utc

  @stub.finish_invocation(request)
end

#update_result(group, name, state:, duration: nil, description: nil) ⇒ Object

Updates a result with its outcome.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/towel/session.rb', line 84

def update_result(group, name, state:, duration: nil, description: nil)
  group_id = find_group(group)
  unless @results.key?([group_id, name])
    raise Error, "Cannot find result '#{name}'"
  end
  result_id = @results[[group_id, name]]

  result = Towel::V1alpha::Result.new
  update_mask = Google::Protobuf::FieldMask.new

  result.name = [
    @invocation.name,
    "groups",
    group_id,
    "results",
    result_id
  ].join "/"

  update_mask.paths << "state"
  result.state = state

  if !duration.nil?
    update_mask.paths << "duration"
    result.duration = duration
  end

  if !description.nil?
    update_mask.paths << "description"
    result.description = description
  end

  request = Towel::V1alpha::UpdateResultRequest.new
  request.result = result
  request.update_mask = update_mask

  @stub.update_result(request)
end