Class: FaaStRuby::Local::FunctionProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/faastruby/local/processors/function.rb

Instance Attribute Summary

Attributes inherited from Processor

#queue, #thread

Instance Method Summary collapse

Methods inherited from Processor

#add_ignore, #add_thread, #get_thread, #initialize, #kill_thread, #present_in_ignore_list?, #remove_ignore, #remove_thread_record, #run, #start, #start_thread

Methods included from Logger

#debug, puts, #puts

Constructor Details

This class inherits a constructor from FaaStRuby::Local::Processor

Instance Method Details

#added(event) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/faastruby/local/processors/function.rb', line 30

def added(event)
  debug "added(#{event.inspect})"
  # This should trigger
  # - Initialize function
  # - Deploy
  if event.function_created?
    debug "added: a handler file was added"
    return new_function(event)
  end
  init_gemfile(event) if event.file_is_a_gemfile?
  unless event.file_is_a_function_config? || event.file_is_a_gemfile? || event.file_is_a_gemfile_lock?
    debug "added: a file was added"
    deploy(event)
  end
end

#bundle_install(event) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/faastruby/local/processors/function.rb', line 152

def bundle_install(event)
  run(event.dirname, 'bundle install') do
    puts "Running: cd #{event.relative_path_dirname} && bundle install"
    if system("cd #{event.dirname} && bundle install")
      STDOUT.puts '---'.yellow
      puts "Gems from Gemfile '#{event.relative_path}' were installed in your local machine."
    else
      STDOUT.puts '---'.red
      STDOUT.puts "#{Time.now} | Error installing gems in your local machine for Gemfile '#{event.relative_path}'.".red
    end
  end
end

#compile_function(event) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/faastruby/local/processors/function.rb', line 78

def compile_function(event)
  debug "compile_function(#{event.inspect})"
  # This should run if:
  # - Modified any file
  # - Removed any file but handler
  # - Language is cristal
  function = Local::Function.that_has_file(event.full_path, event.type)
  if function.is_a?(Local::CrystalFunction)
    run(function.name, 'compile') do
      debug "+ IGNORE #{function.absolute_folder}"
      add_ignore(function.absolute_folder)
      function.compile
      debug "- IGNORE #{function.absolute_folder}"
      remove_ignore(function.absolute_folder)
    end
  end
end

#deploy(event) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/faastruby/local/processors/function.rb', line 127

def deploy(event)
  return false unless SYNC_ENABLED
  # This should run when sync is enabled and:
  # - added any file but handler
  # - modified any file
  # - removed any file but handler
  function = Local::Function.that_has_file(event.full_path, event.type)
  run(function.name, 'deploy') do
    # debug "+ IGNORE #{function.absolute_folder}"
    # add_ignore(function.absolute_folder)
    function.deploy
    # debug "- IGNORE #{function.absolute_folder}"
    # remove_ignore(function.absolute_folder)
  end
end

#function_object_for_handler(filename) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/faastruby/local/processors/function.rb', line 117

def function_object_for_handler(filename)
  debug "function_object_for_handler(#{filename.inspect})"
  case filename
  when 'handler.rb'
    Local::RubyFunction
  when 'handler.cr'
    Local::CrystalFunction
  end
end

#init_gemfile(event) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/faastruby/local/processors/function.rb', line 165

def init_gemfile(event)
  unless File.size(event.full_path) > 0
    run(event.full_path, "init_gemfile") do
      puts "Initializing Gemfile '#{event.relative_path}'"
      add_ignore(event.full_path)
      sleep 0.2
      File.write(event.full_path, Local::RubyFunction.default_gemfile)
      sleep 0.5
      remove_ignore(event.full_path)
    end
  end
end

#is_a_crystal_function?(directory) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/faastruby/local/processors/function.rb', line 25

def is_a_crystal_function?(directory)
  debug "is_a_crystal_function?(#{directory.inspect})"
  File.file?("#{directory}/handler.cr") || File.file?("#{directory}/src/handler.cr")
end

#is_crystal_handler_binary?(filename) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/faastruby/local/processors/function.rb', line 20

def is_crystal_handler_binary?(filename)
  debug "is_crystal_handler_binary?(#{filename.inspect})"
  filename == 'handler' || filename == 'handler.dwarf'
end

#modified(event) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/faastruby/local/processors/function.rb', line 46

def modified(event)
  debug "modified(#{event.inspect})"
  # This should trigger
  # - Compile
  # - Deploy
  bundle_install(event) if event.file_is_a_gemfile?
  compile_function(event)
  deploy(event)
end

#new_function(event) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/faastruby/local/processors/function.rb', line 96

def new_function(event)
  debug "new_function(#{event.inspect})"
  # This should run if:
  # - Handler file is added to a folder
  object = function_object_for_handler(event.filename)
  function = object.new(
    absolute_folder: event.dirname,
    name: event.relative_path_dirname,
  )
  run(function.name, 'new_function') do
    debug "+ IGNORE #{event.dirname}"
    add_ignore(event.dirname)
    function.initialize_new_function
    # Needs improvement. We need to wait a bit so it won't try
    # to deploy or compile newly added functions
    sleep 1.5
    debug "- IGNORE #{event.dirname}"
    remove_ignore(event.dirname)
  end
end

#remove_from_workspace(event) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/faastruby/local/processors/function.rb', line 143

def remove_from_workspace(event)
  debug "remove_from_workspace(#{event.inspect})"
  return false unless SYNC_ENABLED
  # This should run when sync is enabled and:
  # - removed handler
  function = Local::Function.that_has_file(event.full_path, event.type)
  run(function.name, 'remove_from_workspace') {function.remove_from_workspace}
end

#removed(event) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/faastruby/local/processors/function.rb', line 56

def removed(event)
  debug "removed(#{event.inspect})"
  # This should trigger
  # - Compile
  # - Deploy
  # - Remove from workspace
  if event.file_is_a_function_config?
    debug "removed: the file is a function_config"
    function_name = event.relative_path_dirname
    puts "Function '#{function_name}' was removed."
    return remove_from_workspace(event)
  end
  if !event.file_is_a_handler?
    debug "removed: the file is NOT a function config"
    compile_function(event)
    deploy(event)
    return true
  end
rescue FaaStRuby::Local::MissingConfigurationFileError
  nil
end

#should_ignore?(event) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/faastruby/local/processors/function.rb', line 5

def should_ignore?(event)
  debug "should_ignore?(#{event.inspect})"
  return true if event.file_is_a_gemfile_lock?
  if present_in_ignore_list?(event.dirname) || present_in_ignore_list?(event.full_path)
    debug "SKIP #{event}"
    return true
  end
  if is_a_crystal_function?(event.dirname) && is_crystal_handler_binary?(event.filename)
    debug "ignoring #{event.filename}"
    return true
  end
  return true if event.filename == 'Gemfile.lock'
  return false
end