Class: Zillabyte::Command::Apps

Inherits:
Flows show all
Defined in:
lib/zillabyte/cli/apps.rb

Overview

manage custom apps

Constant Summary collapse

MAX_POLL_SECONDS =
60 * 15
POLL_SLEEP =
2.0

Constants inherited from Base

Base::META_COLUMNS

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Flows

#local, #on_exit

Methods inherited from Base

#api, #initialize, namespace

Methods included from Helpers

#app, #ask, #command, #create_git_remote, #display, #error, #extract_app_from_git_config, #extract_app_in_dir, #format_with_bang, #friendly_dir, #get_flow_ui_link, #get_info, #get_rich_info, #git, #handle_downloading_manifest, #has_git?, #longest, #read_multiline, #truncate_message, #version_okay?, #with_tty

Constructor Details

This class inherits a constructor from Zillabyte::Command::Base

Instance Method Details

#authorizeObject

apps:authorize [ID] [SCOPE]

changes permission of the flow

–id ID # The dataset id –public # Makes the flow public (default) –private # Makes the flow private



382
383
384
# File 'lib/zillabyte/cli/apps.rb', line 382

def authorize
  super
end

#cronObject

apps:cron PERIOD

Schedules the app to run on a reocurring basis. Period must be of the form ‘every N (minutes|hours|days|weeks)’

–id ID # The app id. Otherwise, use infer from current directory



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/zillabyte/cli/apps.rb', line 330

def cron

  flow_id = options[:id]
  if flow_id.nil?
    flow_id = read_name_from_conf(options)
  end
  period = options[:period] || shift_argument
  
  error("no id given", type) if flow_id.nil?
  error("no period given", type) if period.nil? 
  
  options = {
    :period => period
  }
  
  res = api.request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{CGI.escape(flow_id)}/cron",
    :body     => options.to_json 
  ).body
  
  if res["error"]
    display res['error_message']
  else
    display "App schedule to run on #{res['next_run']}"
  end
  
end

#cyclesObject

apps:cycles ID [OPTIONS]

Operations on the app’s cycles. With no options, the command lists the apps cycles.

-n, –next # Request the app to move to the next cycle -f, –forever # Automatically start new cycles when previous cycles ends –output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/zillabyte/cli/apps.rb', line 203

def cycles
  app_id = options[:id] || shift_argument
  type = options[:output_type]

  trigger_next = options[:next] || false
  trigger_forever = options[:forever] || false

  if app_id.nil?
    app_id = read_name_from_conf(options)
  end

  if trigger_next
    # Trigger the next app
    response = api.apps.create_cycle(app_id, options)
  elsif trigger_forever
    response = api.apps.run_forever(app_id, options)
  else
    require 'date'
    # List the apps
    response = api.apps.list_cycles(app_id, options)
    # TODO List the sequence number for this app.
    display "Most recent cycles of the app:" if type.nil?
    headings = ["Cycle_id", "State", "Start", "End"]
    rows = response["cycles"] 
    rows = rows.map do |row|
      start_time = DateTime.parse(row["start"]).strftime("%m/%d/%Y %I:%M%p")
      end_time = row["end"].nil? ? "---" : DateTime.parse(row["end"]).strftime("%m/%d/%Y %I:%M%p")
      [row["cycle_id"], row["state"], start_time, end_time ] #TODO Pretty print time
    end

    display TableOutputBuilder.build_table(headings, rows, type)
    display "Total number of cycles executed: #{response['total']}" if type.nil?
    return
  end

  if response["job_id"]
    options[:job_id] = response["job_id"]
    app_id = response["app_id"]

    start = Time.now.utc
    display "Next cycle request sent. If your app was RETIRED this may take a few minutes. Please wait or check 'zillabyte logs' for progress." if type.nil?

    while(Time.now.utc < start + MAX_POLL_SECONDS) do

      # Poll
      res = self.api.apps.cycles_poll(app_id, options)

      case res['status']
      when 'completed'
        if res['return']
          if type == "json"
            return "{}"
          else
            display res['return']
          end
        else
          throw "something is wrong: #{res}"
        end
        # success! continue below
        break
      when 'running'
        sleep(POLL_SLEEP)
     #   display ".", false
      else
        throw "unknown status: #{res}"
      end

    end
  elsif response["error"]
    error(response["error"], type)
  else
    error("remote server error (a380)", type) 
  end
end

#deleteObject

apps:delete ID

Deletes an app. If the app is running, this command will kill it.

-f, –force # Don’t ask for confirmation –output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN



102
103
104
# File 'lib/zillabyte/cli/apps.rb', line 102

def delete
  super
end

#detailsObject

apps:details ID

List details for an app, including percentage complete, if the source is an existing dataset.

–output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN



55
56
57
# File 'lib/zillabyte/cli/apps.rb', line 55

def details
  super 
end

#errorsObject

apps:errors ID

Show recent errors generated by the app.

–output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN



177
178
179
# File 'lib/zillabyte/cli/apps.rb', line 177

def errors
  super
end

#indexObject

apps

List custom apps.

–output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN



17
18
19
# File 'lib/zillabyte/cli/apps.rb', line 17

def index
  self.list
end

#infoObject

apps:info [DIR]

Outputs the info for the app in the dir.

–pretty # Pretty prints the info output –dir DIR # App directory –output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN



409
410
411
# File 'lib/zillabyte/cli/apps.rb', line 409

def info
  super
end

#initObject

apps:init [NAME]

Initializes a new app.

–lang LANG # Specify which language to use (ruby, python) [default: ‘ruby’] –dir DIR # Target directory of the app

Examples:

$ zillabyte apps:init contact_extractor –lang python



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/zillabyte/cli/apps.rb', line 131

def init
  
  name = options[:name] || shift_argument
  error("name required") if name.nil? 
  
  lang = options[:lang] || options[:language] || "ruby"
  dir = options[:dir] || Dir.pwd
  remote = options[:remote] || "zillabyte"
  
  dir = File.expand_path(dir) unless dir.nil?
  type = options[:output_type]
  
  languages = ["ruby","python", "js"]

  error("Unsupported language #{lang}. Zillabyte currently supports #{languages.join(', ')}.", type) if not languages.include? lang

  display "initializing empty #{lang} app in #{friendly_dir(dir)}" if type.nil?
  erb_binding = binding
  require("fileutils")
  FileUtils.mkdir_p dir

  require("erb")
  Dir[File.join(File.expand_path("../templates/apps/#{lang}", __FILE__), "*")].each do |source_file|
    
    next if File.directory?(source_file)
    erb = ERB.new(File.read(source_file))
    erb.filename = source_file
    
    dest_file = File.join(dir, File.basename(source_file).gsub(/\.erb$/, ""))
    File.open(dest_file, 'w') {|file| file.write(erb.result(erb_binding))}
    
  end
  
  # Add the git remote... 
  # add_git_remote(name, remote)
  
end

#killObject

apps:kill ID

Kills the given app.

–config CONFIG_FILE # Use the given config file –output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN



369
370
371
# File 'lib/zillabyte/cli/apps.rb', line 369

def kill
  super
end

#listObject

apps

List custom apps.

–output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zillabyte/cli/apps.rb', line 29

def list
  type = options[:output_type]
  
  headings = ["id", "name", "state", "cycles"]
  rows = api.app.list.map do |row|
    if headings.size == 0
      headings = row.keys 
      headings.delete("rel_dir")
    end
    
    v = row.values_at *headings
    v
  end

  display "apps:\n" if type.nil?
  display TableOutputBuilder.build_table(headings, rows, type)
  display "Total number of apps: " + rows.length.to_s if type.nil?
end

#live_runObject

apps:live_run [OPERATION_NAME] [PIPE_NAME] [DIR]

Runs a local app with live data.

–config CONFIG_FILE # Use the given config file –dir DIR # App directory –output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN

HIDDEN:



396
397
398
# File 'lib/zillabyte/cli/apps.rb', line 396

def live_run
  super
end

#logsObject

apps:logs ID [OPERATION_NAME]

Streams logs for the app from our cluster.

–operation OPERATION_NAME # Specify the operation to show logs for -v, –verbose LEVEL # Sets the verbosity (error, info, debug) [default: info] –output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN



189
190
191
# File 'lib/zillabyte/cli/apps.rb', line 189

def logs
  super
end

#pauseObject

apps:pause [ID]

Pause an app



299
300
301
# File 'lib/zillabyte/cli/apps.rb', line 299

def pause
  super
end

#prepObject

apps:prep [DIR]

Performs any necessary initialization for the app.

–dir DIR # App directory –output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN –mode MODE # Specify development or production mode for dependencies #HIDDEN



115
116
117
# File 'lib/zillabyte/cli/apps.rb', line 115

def prep
  super
end

#pullObject

apps:pull ID DIR

Pulls an app source to a directory.

–force # Pulls even if the directory exists –dir DIR # App directory –output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN

Examples:

$ zillabyte apps:pull 27 .



90
91
92
# File 'lib/zillabyte/cli/apps.rb', line 90

def pull
  super
end

#pushObject

apps:push [DIR]

Uploads an app.

–config CONFIG_FILE # Use the given config file –dir DIR # App directory –output_type OUTPUT_TYPE # Specify an output type i.e. json #HIDDEN –public # Make the flow public

Examples:

$ zillabyte apps:push .



73
74
75
# File 'lib/zillabyte/cli/apps.rb', line 73

def push
  super
end

#resumeObject

apps:resume [ID]

Resume a paused app



308
309
310
# File 'lib/zillabyte/cli/apps.rb', line 308

def resume
  super
end

#scaleObject

apps:scale ID OPERATION_NAME PARALLELISM

Change the parallelism of of app or operation



316
317
318
# File 'lib/zillabyte/cli/apps.rb', line 316

def scale
  super
end

#testObject

apps:test

Tests a local app with sample data.

–config CONFIG_FILE # Use the given config file –output OUTPUT_FILE # Writes sink output to a file –cycles CYCLES # Number of cycles to emit [default: 1] –dir DIR # App directory -i, –interactive # Interactively control input and read output



289
290
291
# File 'lib/zillabyte/cli/apps.rb', line 289

def test
  super
end