Class: FaaStRuby::Local::CrystalFunction

Inherits:
Function show all
Includes:
Logger
Defined in:
lib/faastruby/local/functions/crystal.rb

Instance Attribute Summary

Attributes inherited from Function

#context, #created_at, #errors, #name, #updated_at, #workspace

Instance Method Summary collapse

Methods included from Logger

#debug, puts, #puts

Methods inherited from Function

#destroy, #run, #update

Methods inherited from BaseObject

#assign_attributes, #attributes=, #call_api, #initialize, #mass_assign

Constructor Details

This class inherits a constructor from FaaStRuby::BaseObject

Instance Method Details

#compileObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/faastruby/local/functions/crystal.rb', line 5

def compile
  debug "compile"
  handler_path = get_handler_path
  debug "File exists? #{handler_path} - #{File.file?(handler_path)}"
  runtime_path = Pathname.new "#{Gem::Specification.find_by_name("faastruby").gem_dir}/lib/faastruby/local/crystal_runtime.cr"
  h_path = Pathname.new(handler_path)
  handler_path = h_path.relative_path_from runtime_path
  build_cmd = "cd #{@absolute_folder} && crystal build #{runtime_path} -o handler"
  debug "Running #{build_cmd}"
  job_id = SecureRandom.uuid
  puts "Job ID=\"#{job_id}\" started: Compiling function '#{@name}'"
  # return false unless precompile
  env = {'HANDLER_PATH' => handler_path.to_s}
  debug "COMPILE ENV: #{env}"
  output, status = Open3.capture2e(env, build_cmd)
  success = status.exitstatus == 0
  if success
    puts "Job ID=\"#{job_id}\" completed: #{status}"
  else
    puts "Job ID=\"#{job_id}\" failed:"
    String.disable_colorization = true
    STDERR.puts output
    STDOUT.puts '---'
    String.disable_colorization = false
  end
  # puts "Job ID=\"#{job_id}\": #{output}" unless success
  # puts "Job ID=\"#{job_id}\" #{success ? 'completed' : 'failed'}: #{status}"
end

#get_handler_pathObject



34
35
36
37
38
39
40
# File 'lib/faastruby/local/functions/crystal.rb', line 34

def get_handler_path
  if File.file?("#{@absolute_folder}/handler.cr")
    "#{@absolute_folder}/handler"
  else
    "#{@absolute_folder}/src/handler"
  end
end

#write_handlerObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/faastruby/local/functions/crystal.rb', line 51

def write_handler
  debug "write_handler"
  content = "def handler(event)\n  # Write code here\n  \nend"
  file = "#{get_handler_path}.cr"
  if File.size(file) > 0
    puts "New Crystal function '#{@name}' detected."
  else
    File.write(file, content)
    puts "New Crystal function '#{@name}' initialized."
  end
end

#yaml_hashObject



42
43
44
45
46
47
48
49
# File 'lib/faastruby/local/functions/crystal.rb', line 42

def yaml_hash
  debug "yaml_hash"
  hash = {
    'cli_version' => FaaStRuby::VERSION,
    'name' => @name,
    'runtime' => DEFAULT_CRYSTAL_RUNTIME
  }
end