Class: Jets::Builders::CodeSize

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/jets/builders/code_size.rb

Constant Summary collapse

LAMBDA_SIZE_LIMIT =

Total lambda limit is 250MB

250

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check!Object



6
7
8
# File 'lib/jets/builders/code_size.rb', line 6

def self.check!
  new.check
end

Instance Method Details

#checkObject



10
11
12
13
14
15
16
# File 'lib/jets/builders/code_size.rb', line 10

def check
  return if within_lambda_limit?
  say "Over the Lambda size limit of #{LAMBDA_SIZE_LIMIT}MB".colorize(:red)
  say "Please reduce the size of your code."
  display_sizes
  exit 1
end

#compute_size(path) ⇒ Object



42
43
44
45
46
# File 'lib/jets/builders/code_size.rb', line 42

def compute_size(path)
  # -k option is required for macosx but not for linux
  out = `du -ks #{path}`
  out.split(' ').first.to_i # bytes
end

#display_sizesObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jets/builders/code_size.rb', line 28

def display_sizes
  code_size = compute_size("#{stage_area}/code")
  opt_size = compute_size("#{stage_area}/opt")
  total_size = opt_size + code_size
  overlimit = (LAMBDA_SIZE_LIMIT * 1024 - total_size) * -1
  say "Sizes:"
  say "Code: #{megabytes(code_size)} - #{stage_area}/code"
  say "Gem Layer: #{megabytes(opt_size)} - #{stage_area}/opt"
  say "Total Package: #{megabytes(total_size)}"
  say "Over limit by: #{megabytes(overlimit)}"
  say "Sometimes blowing away the /tmp/jets cache will reduce the size: rm -rf /tmp/jets"
  # sh "du -kcsh #{stage_area}/*" unless ENV['TEST'] # uncomment to debug
end

#megabytes(bytes) ⇒ Object



48
49
50
51
# File 'lib/jets/builders/code_size.rb', line 48

def megabytes(bytes)
  n = bytes / 1024.0
  sprintf('%.1f', n) + 'MB'
end

#say(message) ⇒ Object



53
54
55
# File 'lib/jets/builders/code_size.rb', line 53

def say(message)
  puts message unless ENV['TEST']
end

#total_sizeObject



22
23
24
25
26
# File 'lib/jets/builders/code_size.rb', line 22

def total_size
  code_size = compute_size("#{stage_area}/code")
  opt_size = compute_size("#{stage_area}/opt")
  opt_size + code_size # total_size
end

#within_lambda_limit?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/jets/builders/code_size.rb', line 18

def within_lambda_limit?
  total_size < LAMBDA_SIZE_LIMIT * 1024 # 120MB
end