Skip to content

Commit

Permalink
server: add custom timeouts to actions (#5762)
Browse files Browse the repository at this point in the history
  • Loading branch information
codingkarthik authored and rikinsk committed Dec 16, 2020
1 parent 545c7b9 commit 4fa581d
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Create a synchronous action with name ``create_user``:
}
],
"output_type":"User",
"handler":"https://action.my_app.com/create-user"
"handler":"https://action.my_app.com/create-user",
"timeout":60
},
"comment": "Custom action to create user"
}
Expand Down Expand Up @@ -122,6 +123,11 @@ ActionDefinition
- false
- [ ``mutation`` | ``query`` ]
- The type of the action (default: ``mutation``)
* - timeout
- false
- Integer
- Number of seconds to wait for response before timing out. Default: 30


.. _InputArgument:

Expand Down
4 changes: 3 additions & 1 deletion server/src-lib/Hasura/RQL/DDL/Metadata/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,8 @@ replaceMetadataToOrdJSON ( ReplaceMetadata
<> catMaybes [maybeAnyToMaybeOrdPair "description" AO.toOrdered descM]

actionDefinitionToOrdJSON :: ActionDefinitionInput -> AO.Value
actionDefinitionToOrdJSON (ActionDefinition args outputType actionType headers frwrdClientHdrs handler) =
actionDefinitionToOrdJSON (ActionDefinition args outputType actionType
headers frwrdClientHdrs timeout handler) =
let typeAndKind = case actionType of
ActionQuery -> [("type", AO.toOrdered ("query" :: String))]
ActionMutation kind -> [ ("type", AO.toOrdered ("mutation" :: String))
Expand All @@ -551,6 +552,7 @@ replaceMetadataToOrdJSON ( ReplaceMetadata
<> catMaybes [ listToMaybeOrdPair "headers" AO.toOrdered headers
, listToMaybeOrdPair "arguments" argDefinitionToOrdJSON args]
<> typeAndKind
<> (bool [("timeout",AO.toOrdered timeout)] mempty $ timeout == defaultActionTimeoutSecs)

permToOrdJSON :: ActionPermissionMetadata -> AO.Value
permToOrdJSON (ActionPermissionMetadata role permComment) =
Expand Down
5 changes: 5 additions & 0 deletions server/tests-py/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ def do_POST(self):
resp, status = self.create_user()
self._send_response(status, resp)

elif req_path == "/create-user-timeout":
time.sleep(2)
resp, status = self.create_user()
self._send_response(status, resp)

elif req_path == "/create-users":
resp, status = self.create_users()
self._send_response(status, resp)
Expand Down
44 changes: 44 additions & 0 deletions server/tests-py/queries/actions/timeout/schema_setup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
type: bulk
args:
- type: run_sql
args:
sql: |
CREATE TABLE "user"(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL,
is_admin BOOLEAN NOT NULL DEFAULT false
);
- type: track_table
args:
name: user
schema: public

- type: set_custom_types
args:
objects:
- name: UserId
fields:
- name: id
type: Int!
relationships:
- name: user
type: object
remote_table: user
field_mapping:
id: id

- type: create_action
args:
name: create_user
definition:
kind: asynchronous
arguments:
- name: email
type: String!
- name: name
type: String!
output_type: UserId
timeout: 2
handler: http://127.0.0.1:5593/create-user-timeout
15 changes: 15 additions & 0 deletions server/tests-py/queries/actions/timeout/schema_teardown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type: bulk
args:
- type: drop_action
args:
name: create_user
clear_data: true
# clear custom types
- type: set_custom_types
args: {}

- type: run_sql
args:
cascade: true
sql: |
DROP TABLE "user";
7 changes: 7 additions & 0 deletions server/tests-py/queries/actions/timeout/values_teardown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: bulk
args:
- type: run_sql
args:
sql: |
DELETE FROM "user";
SELECT setval('user_id_seq', 1, FALSE);
47 changes: 47 additions & 0 deletions server/tests-py/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,50 @@ def test_introspection_query(self, hge_ctx):
code, resp, _ = hge_ctx.anyq(conf['url'], conf['query'], headers)
assert code == 200, resp
assert 'data' in resp, resp

@use_action_fixtures
class TestActionTimeout:

@classmethod
def dir(cls):
return 'queries/actions/timeout'

def test_action_timeout_fail(self, hge_ctx):
graphql_mutation = '''
mutation {
create_user(email: "random-email", name: "Clarke")
}
'''
query = {
'query': graphql_mutation,
'variables': {}
}
status, resp, _ = hge_ctx.anyq('/v1/graphql', query, mk_headers_with_secret(hge_ctx))
assert status == 200, resp
assert 'data' in resp
action_id = resp['data']['create_user']
query_async = '''
query ($action_id: uuid!){
create_user(id: $action_id){
id
errors
}
}
'''
query = {
'query': query_async,
'variables': {
'action_id': action_id
}
}
conf = {
'url': '/v1/graphql',
'headers': {},
'query': query,
'status': 200,
}
# the action takes 2 seconds to complete
time.sleep(4)
response, _ = check_query(hge_ctx, conf)
assert 'errors' in response['data']['create_user']
assert 'ResponseTimeout' == response['data']['create_user']['errors']['internal']['error']['message']

0 comments on commit 4fa581d

Please sign in to comment.