@@ -14,7 +14,7 @@ def run_statement(statement, comment, bind_params = [])
14
14
request_token = Digest ::MD5 . hexdigest ( [ statement , bind_params . to_json , data_source . id , settings [ "workgroup" ] ] . compact . join ( "/" ) )
15
15
statement_name = "blazer_#{ request_token } "
16
16
begin
17
- client . create_prepared_statement ( {
17
+ create_prepared_statement ( {
18
18
statement_name : statement_name ,
19
19
work_group : settings [ "workgroup" ] ,
20
20
query_statement : statement
@@ -45,15 +45,15 @@ def run_statement(statement, comment, bind_params = [])
45
45
query_options [ :work_group ] = settings [ "workgroup" ]
46
46
end
47
47
48
- resp = client . start_query_execution ( **query_options )
48
+ resp = start_query_execution ( **query_options )
49
49
query_execution_id = resp . query_execution_id
50
50
51
51
timeout = data_source . timeout || 300
52
52
stop_at = Time . now + timeout
53
53
resp = nil
54
54
55
55
begin
56
- resp = client . get_query_results (
56
+ resp = get_query_results (
57
57
query_execution_id : query_execution_id
58
58
)
59
59
rescue Aws ::Athena ::Errors ::InvalidRequestException => e
@@ -119,11 +119,11 @@ def run_statement(statement, comment, bind_params = [])
119
119
end
120
120
121
121
def tables
122
- glue . get_tables ( database_name : database ) . table_list . map ( &:name ) . sort
122
+ get_tables ( database_name : database ) . table_list . map ( &:name ) . sort
123
123
end
124
124
125
125
def schema
126
- glue . get_tables ( database_name : database ) . table_list . map { |t | { table : t . name , columns : t . storage_descriptor . columns . map { |c | { name : c . name , data_type : c . type } } } }
126
+ get_tables ( database_name : database ) . table_list . map { |t | { table : t . name , columns : t . storage_descriptor . columns . map { |c | { name : c . name , data_type : c . type } } } }
127
127
end
128
128
129
129
def preview_statement
@@ -154,11 +154,51 @@ def engine_version
154
154
end
155
155
156
156
def fetch_error ( query_execution_id )
157
- client . get_query_execution (
157
+ get_query_execution (
158
158
query_execution_id : query_execution_id
159
159
) . query_execution . status . state_change_reason
160
160
end
161
161
162
+ def autorefresh_credentials
163
+ yield
164
+ rescue Aws ::Athena ::Errors ::ExpiredTokenException , Aws ::Glue ::Errors ::ExpiredTokenException
165
+ # Clear our cached Athena & Glue clients to force fetching new credentials, and immediately retry
166
+ @client = nil
167
+ @glue = nil
168
+ @client_credentials = nil
169
+ yield
170
+ end
171
+
172
+ def get_tables ( **options )
173
+ autorefresh_credentials do
174
+ glue . get_tables ( **options )
175
+ end
176
+ end
177
+
178
+ def create_prepared_statement ( **options )
179
+ autorefresh_credentials do
180
+ client . create_prepared_statement ( **options )
181
+ end
182
+ end
183
+
184
+ def start_query_execution ( **options )
185
+ autorefresh_credentials do
186
+ client . start_query_execution ( **options )
187
+ end
188
+ end
189
+
190
+ def get_query_results ( **options )
191
+ autorefresh_credentials do
192
+ client . get_query_results ( **options )
193
+ end
194
+ end
195
+
196
+ def get_query_execution ( **options )
197
+ autorefresh_credentials do
198
+ client . get_query_execution ( **options )
199
+ end
200
+ end
201
+
162
202
def client
163
203
@client ||= Aws ::Athena ::Client . new ( **client_options )
164
204
end
@@ -168,12 +208,12 @@ def glue
168
208
end
169
209
170
210
def client_options
171
- @client_options ||= begin
172
- options = { }
173
- options [ :credentials ] = client_credentials if client_credentials
174
- options [ :region ] = settings [ "region" ] if settings [ "region" ]
175
- options
211
+ options = { }
212
+ if credentials = client_credentials
213
+ options [ :credentials ] = credentials
176
214
end
215
+ options [ :region ] = settings [ "region" ] if settings [ "region" ]
216
+ options
177
217
end
178
218
179
219
def client_credentials
0 commit comments