Class: IParty::RakeTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/iparty/rake_task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :iparty) {|_self| ... } ⇒ RakeTask

Returns a new instance of RakeTask.

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/iparty/rake_task.rb', line 10

def initialize(name = :iparty)
  super()

  @name = name
  @verbose = true

  yield self if block_given?
  define_update
  define_fetch
  define_status
  define_config
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/iparty/rake_task.rb', line 8

def name
  @name
end

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/iparty/rake_task.rb', line 8

def verbose
  @verbose
end

Instance Method Details

#define_configObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/iparty/rake_task.rb', line 100

def define_config
  namespace(name) do
    desc "Shows effective IParty config (including license_key, optional format json/inspect)"
    task :config, [:format] do |task, args|
      Rake.application.lookup("environment")&.invoke

      case args[:format]
      when "json"
        require "json"
        puts JSON.pretty_generate(IParty.config.to_h)
      when "inspect"
        puts IParty.config.inspect
      else
        IParty.config.each_pair do |key, value|
          puts "#{key.to_s.rjust(16)}: #{value.inspect}"
        end
      end
    end
  end
end

#define_fetchObject

iparty:fetch iparty:fetch iparty:fetch



50
51
52
53
54
55
56
57
58
59
# File 'lib/iparty/rake_task.rb', line 50

def define_fetch
  namespace(name) do
    desc "Fetches missing or expired geoip mmdb-files (optional numeric max_age)"
    task :fetch, [:max_age] do |task, args|
      Rake.application.lookup("environment")&.invoke

      IParty::MaxMind.fetch_db_files!(parse_duration(args[:max_age]), verbose: @verbose)
    end
  end
end

#define_statusObject

iparty:status



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/iparty/rake_task.rb', line 62

def define_status
  namespace(name) do
    desc "Show status of geoip mmdb-files (optional numeric max_age)"
    task :status, [:max_age] do |task, args|
      Rake.application.lookup("environment")&.invoke

      max_age = parse_duration(args[:max_age])

      success = IParty.config.editions.map do |edition|
        file = IParty.config.directory.join("#{edition}.mmdb")
        reason = IParty::MaxMind.fetch_db_file_reason(file, max_age)

        stat_string = if file.exist?
          ctime   = file.ctime
          age     = Time.now - ctime
          days    = (age / 86_400).floor
          hours   = ((age / 3_600) % 24).floor
          minutes = ((age / 60) % 60).floor
          age_string = [
            ("#{days}d" if days > 0),
            ("%02d:%02d" % [hours, minutes] if hours > 0 || minutes > 0),
          ].compact.join(" ")
          age_string = "#{age.floor}s" if age_string.empty?
          "[age: #{age_string}, ctime: #{ctime}]"
        end

        puts [reason&.upcase || "OK", stat_string, file].compact.join(" ")
        !reason
      end.all?

      exit(1) unless success
    end
  end
end

#define_updateObject

iparty:update



36
37
38
39
40
41
42
43
44
45
# File 'lib/iparty/rake_task.rb', line 36

def define_update
  namespace(name) do
    desc "Updates geoip mmdb-files"
    task :update do
      Rake.application.lookup("environment")&.invoke

      IParty::MaxMind.fetch_db_files!(verbose: @verbose)
    end
  end
end

#parse_duration(input, default = :missing) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/iparty/rake_task.rb', line 23

def parse_duration input, default = :missing
  return default unless input.is_a?(String)

  if input.match?(/\A\d+\z/)
    input.to_i
  elsif asm = input.match(/\A(\d+)\.(second|minute|hour|day|week|month|year)s?\z/)
    ActiveSupport::Duration.send(:"#{asm[2]}s", asm[1].to_i)
  else
    default
  end
end