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, #print, puts, #puts

Constructor Details

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

Instance Method Details

#added(event) ⇒ Object



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

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
  unless event.file_is_a_function_config?
    debug "added: a file was added"
    deploy(event)
  end
end

#compile_function(event) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/faastruby/local/processors/function.rb', line 74

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/faastruby/local/processors/function.rb', line 123

def deploy(event)
  debug "deploy(#{event.inspect})"
  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



113
114
115
116
117
118
119
120
121
# File 'lib/faastruby/local/processors/function.rb', line 113

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

#is_a_crystal_function?(directory) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/faastruby/local/processors/function.rb', line 23

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)


18
19
20
21
# File 'lib/faastruby/local/processors/function.rb', line 18

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

#modified(event) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/faastruby/local/processors/function.rb', line 43

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

#new_function(event) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/faastruby/local/processors/function.rb', line 92

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: File.dirname(event.relative_path),
  )
  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



140
141
142
143
144
145
146
147
# File 'lib/faastruby/local/processors/function.rb', line 140

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/faastruby/local/processors/function.rb', line 52

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
    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
# File 'lib/faastruby/local/processors/function.rb', line 5

def should_ignore?(event)
  debug "should_ignore?(#{event.inspect})"
  if present_in_ignore_list?(event.dirname)
    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 false
end