Class: Zenaton::Client
- Inherits:
-
Object
- Object
- Zenaton::Client
- Includes:
- Singleton
- Defined in:
- lib/zenaton/client.rb
Overview
Zenaton Client
Constant Summary collapse
- ZENATON_API_URL =
Zenaton api url
'https://api.zenaton.com/v1'
- ZENATON_GATEWAY_URL =
Gateway url
'https://gateway.zenaton.com/api'
- ZENATON_WORKER_URL =
Default worker url
'http://localhost'
- DEFAULT_WORKER_PORT =
Default worker port
4001
- WORKER_API_VERSION =
Default worker api version
'v_newton'
- MAX_ID_SIZE =
Limit on length of custom ids
256
- APP_ENV =
Parameter name for the application environment
'app_env'
- APP_ID =
Parameter name for the application ID
'app_id'
- API_TOKEN =
Parameter name for the API token
'api_token'
- ATTR_INTENT_ID =
Internal parameter for retries
'intent_id'
- ATTR_ID =
Parameter name for custom ids
'custom_id'
- ATTR_NAME =
Parameter name for workflow names
'name'
- ATTR_CANONICAL =
Parameter name for version name
'canonical_name'
- ATTR_DATA =
Parameter name for json payload
'data'
- ATTR_PROG =
Parameter name for the language
'programming_language'
- ATTR_MODE =
Parameter name for the worker update mode
'mode'
- ATTR_MAX_PROCESSING_TIME =
Parameter name for task maximum processing time
'maxProcessingTime'
- PROG =
The current programming language
'Ruby'
- EVENT_INPUT =
Parameter name for event input
'event_input'
- EVENT_NAME =
Parameter name for event name
'event_name'
- EVENT_DATA =
Parameter name for event data
'event_data'
- WORKFLOW_KILL =
Worker update mode to stop a worker
'kill'
- WORKFLOW_PAUSE =
Worker udpate mode to pause a worker
'pause'
- WORKFLOW_RUN =
Worker update mode to resume a worker
'run'
Instance Attribute Summary collapse
-
#api_token ⇒ Object
writeonly
Sets the attribute api_token.
-
#app_env ⇒ Object
writeonly
Sets the attribute app_env.
-
#app_id ⇒ Object
writeonly
Sets the attribute app_id.
Class Method Summary collapse
-
.init(app_id, api_token, app_env) ⇒ Zenaton::Client
Class method that sets the three tokens needed to interact with the API.
Instance Method Summary collapse
-
#create_task_schedule_input(task, cron) ⇒ Object
rubocop:enable Metrics/MethodLength.
-
#create_workflow_schedule_input(flow, cron) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#find_workflow(workflow_name, custom_id) ⇒ Zenaton::Interfaces::Workflow?
Finds a workflow.
- #gateway_headers ⇒ Object
- #gateway_url ⇒ Object
-
#kill_workflow(workflow_name, custom_id) ⇒ NilClass
Stops a workflow.
-
#pause_workflow(workflow_name, custom_id) ⇒ NilClass
Pauses a workflow.
-
#resume_workflow(workflow_name, custom_id) ⇒ NilClass
Resumes a workflow.
-
#send_event(workflow_name, custom_id, event) ⇒ NilClass
Sends an event to a workflow.
- #start_scheduled_task(task, cron) ⇒ Object
- #start_scheduled_workflow(flow, cron) ⇒ Object
-
#start_task(task) ⇒ Object
Start a single task.
-
#start_workflow(flow) ⇒ Object
Start the specified workflow.
-
#website_url(resource = '', params = {}) ⇒ String
Gets the url for zenaton api.
-
#worker_url(resource = '', params = {}) ⇒ String
Gets the url for the workers.
Instance Attribute Details
#api_token=(value) ⇒ Object (writeonly)
Sets the attribute api_token
48 49 50 |
# File 'lib/zenaton/client.rb', line 48 def api_token=(value) @api_token = value end |
#app_env=(value) ⇒ Object (writeonly)
Sets the attribute app_env
48 49 50 |
# File 'lib/zenaton/client.rb', line 48 def app_env=(value) @app_env = value end |
#app_id=(value) ⇒ Object (writeonly)
Sets the attribute app_id
48 49 50 |
# File 'lib/zenaton/client.rb', line 48 def app_id=(value) @app_id = value end |
Class Method Details
.init(app_id, api_token, app_env) ⇒ Zenaton::Client
Class method that sets the three tokens needed to interact with the API
55 56 57 58 59 60 61 |
# File 'lib/zenaton/client.rb', line 55 def self.init(app_id, api_token, app_env) instance.tap do |client| client.app_id = app_id client.api_token = api_token client.app_env = app_env end end |
Instance Method Details
#create_task_schedule_input(task, cron) ⇒ Object
rubocop:enable Metrics/MethodLength
179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/zenaton/client.rb', line 179 def create_task_schedule_input(task, cron) { 'createTaskScheduleInput' => { 'intentId' => SecureRandom.uuid, 'environmentName' => @app_env, 'cron' => cron, 'taskName' => class_name(task), 'programmingLanguage' => PROG.upcase, 'properties' => @serializer.encode(@properties.from(task)) } } end |
#create_workflow_schedule_input(flow, cron) ⇒ Object
rubocop:disable Metrics/MethodLength
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/zenaton/client.rb', line 163 def create_workflow_schedule_input(flow, cron) { 'createWorkflowScheduleInput' => { 'intentId' => SecureRandom.uuid, 'environmentName' => @app_env, 'cron' => cron, 'customId' => parse_custom_id_from(flow), 'workflowName' => class_name(flow), 'canonicalName' => canonical_name(flow) || class_name(flow), 'programmingLanguage' => PROG.upcase, 'properties' => @serializer.encode(@properties.from(flow)) } } end |
#find_workflow(workflow_name, custom_id) ⇒ Zenaton::Interfaces::Workflow?
Finds a workflow
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/zenaton/client.rb', line 220 def find_workflow(workflow_name, custom_id) params = { ATTR_ID => custom_id, ATTR_NAME => workflow_name } params[ATTR_PROG] = PROG data = @http.get(instance_website_url(params))['data'] data && @properties.object_from( data['name'], @serializer.decode(data['properties']) ) rescue Zenaton::InternalError => exception return nil if exception. =~ /No workflow instance found/ raise exception end |
#gateway_headers ⇒ Object
91 92 93 94 95 96 |
# File 'lib/zenaton/client.rb', line 91 def gateway_headers { 'app-id' => @app_id, 'api-token' => @api_token } end |
#gateway_url ⇒ Object
87 88 89 |
# File 'lib/zenaton/client.rb', line 87 def gateway_url ENV['ZENATON_GATEWAY_URL'] || ZENATON_GATEWAY_URL end |
#kill_workflow(workflow_name, custom_id) ⇒ NilClass
Stops a workflow
196 197 198 |
# File 'lib/zenaton/client.rb', line 196 def kill_workflow(workflow_name, custom_id) update_instance(workflow_name, custom_id, WORKFLOW_KILL) end |
#pause_workflow(workflow_name, custom_id) ⇒ NilClass
Pauses a workflow
204 205 206 |
# File 'lib/zenaton/client.rb', line 204 def pause_workflow(workflow_name, custom_id) update_instance(workflow_name, custom_id, WORKFLOW_PAUSE) end |
#resume_workflow(workflow_name, custom_id) ⇒ NilClass
Resumes a workflow
212 213 214 |
# File 'lib/zenaton/client.rb', line 212 def resume_workflow(workflow_name, custom_id) update_instance(workflow_name, custom_id, WORKFLOW_RUN) end |
#send_event(workflow_name, custom_id, event) ⇒ NilClass
Sends an event to a workflow
238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/zenaton/client.rb', line 238 def send_event(workflow_name, custom_id, event) body = { ATTR_INTENT_ID => SecureRandom.uuid, ATTR_PROG => PROG, ATTR_NAME => workflow_name, ATTR_ID => custom_id, EVENT_NAME => event.class.name, EVENT_INPUT => @serializer.encode(@properties.from(event)), EVENT_DATA => @serializer.encode(event) } @http.post(send_event_url, body) end |
#start_scheduled_task(task, cron) ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/zenaton/client.rb', line 142 def start_scheduled_task(task, cron) res = @graphql.request( gateway_url, Services::GraphQL::CREATE_TASK_SCHEDULE, create_task_schedule_input(task, cron), gateway_headers ) res && res['createTaskSchedule'] end |
#start_scheduled_workflow(flow, cron) ⇒ Object
152 153 154 155 156 157 158 159 160 |
# File 'lib/zenaton/client.rb', line 152 def start_scheduled_workflow(flow, cron) res = @graphql.request( gateway_url, Services::GraphQL::CREATE_WORKFLOW_SCHEDULE, create_workflow_schedule_input(flow, cron), gateway_headers ) res && res['createWorkflowSchedule'] end |
#start_task(task) ⇒ Object
Start a single task
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/zenaton/client.rb', line 116 def start_task(task) max_processing_time = task.try(:max_processing_time) @http.post( worker_url('tasks'), ATTR_INTENT_ID => SecureRandom.uuid, ATTR_PROG => PROG, ATTR_NAME => class_name(task), ATTR_DATA => @serializer.encode(@properties.from(task)), ATTR_MAX_PROCESSING_TIME => max_processing_time ) end |
#start_workflow(flow) ⇒ Object
Start the specified workflow
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/zenaton/client.rb', line 130 def start_workflow(flow) @http.post( instance_worker_url, ATTR_INTENT_ID => SecureRandom.uuid, ATTR_PROG => PROG, ATTR_CANONICAL => canonical_name(flow), ATTR_NAME => class_name(flow), ATTR_DATA => @serializer.encode(@properties.from(flow)), ATTR_ID => parse_custom_id_from(flow) ) end |
#website_url(resource = '', params = {}) ⇒ String
Gets the url for zenaton api
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/zenaton/client.rb', line 102 def website_url(resource = '', params = {}) api_url = ENV['ZENATON_API_URL'] || ZENATON_API_URL url = "#{api_url}/#{resource}" if params.is_a?(Hash) params[API_TOKEN] = @api_token append_params_to_url(url, params) else add_app_env("#{url}?#{API_TOKEN}=#{@api_token}&", params) end end |
#worker_url(resource = '', params = {}) ⇒ String
Gets the url for the workers
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/zenaton/client.rb', line 75 def worker_url(resource = '', params = {}) base_url = ENV['ZENATON_WORKER_URL'] || ZENATON_WORKER_URL port = ENV['ZENATON_WORKER_PORT'] || DEFAULT_WORKER_PORT url = "#{base_url}:#{port}/api/#{WORKER_API_VERSION}/#{resource}" if params.is_a?(Hash) append_params_to_url(url, params) else add_app_env("#{url}?", params) end end |