Module: Dakwak

Defined in:
lib/dakwak/log_manager.rb,
lib/dakwak.rb,
lib/dakwak/logger.rb,
lib/dakwak/utility.rb,
lib/dakwak/configurable.rb,
lib/dakwak/configurator.rb,
lib/dakwak/mongo_adapter.rb,
lib/dakwak/analytics/sheet.rb,
lib/dakwak/messaging/channel.rb,
lib/dakwak/messaging/message.rb,
lib/dakwak/messaging/station.rb,
lib/dakwak/messaging/communicator.rb,
lib/dakwak/messaging/private_channel.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: Analytics, Logger, Silencable, Utility Classes: Channel, Communicator, Configurable, Configurator, LogLevel, LogManager, Message, MongoAdapter, PrivateChannel, Station

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#log_managerObject (readonly)

:nodoc:



16
17
18
# File 'lib/dakwak.rb', line 16

def log_manager
  @log_manager
end

#loggerObject (readonly)

:nodoc:



16
17
18
# File 'lib/dakwak.rb', line 16

def logger
  @logger
end

#stationObject (readonly)

:nodoc:



16
17
18
# File 'lib/dakwak.rb', line 16

def station
  @station
end

Class Method Details

.__die_gracefullyObject



116
117
118
119
120
# File 'lib/dakwak.rb', line 116

def __die_gracefully
  logger.log_notice "dakwak: installing traps for SIGINT and SIGTERM"
  Signal.trap("INT") { Dakwak.cleanup }
  Signal.trap("TERM") { Dakwak.cleanup }
end

.__startObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dakwak.rb', line 84

def __start
  if defined?(PhusionPassenger)
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
      @@my_own_reactor = true
      # for passenger, we need to avoid orphaned threads
      if forked && EM.reactor_running?
        Dakwak.cleanup
      end
      Thread.new { EM.run do yield end }
      # __die_gracefully
      sleep(@@sleep_ms * 2)
    end
  else
    if EventMachine.reactor_running?
      puts "EM reactor seems to be running, will hook into it"
      @@my_own_reactor = false
      yield
    else
      @@my_own_reactor = true
      puts "EM reactor doesn't seem to be running, will fire it up"
      if defined?(Thin)
        return yield
      end

      EventMachine.run do
        yield
      end

    end
  end # no phusion passenger
end

.add_cleanup_handler(handler) ⇒ Object



173
174
175
176
177
# File 'lib/dakwak.rb', line 173

def add_cleanup_handler(handler)
  raise RuntimeError "cleanup handler must be a Proc or a lambda" unless handler.respond_to?(:call)

  @@cleanup_handlers << handler
end

.app_fqnObject

The fully-qualified application name that is unique across the network. Format:

name-version@hostname
# an example
[email protected]


194
195
196
# File 'lib/dakwak.rb', line 194

def app_fqn()
  @@app_fqn
end

.app_nameObject

The registered application name



200
201
202
# File 'lib/dakwak.rb', line 200

def app_name
  @@app_name
end

.cleanup(&callback) ⇒ Object

Call this when you’re done using dakwak.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/dakwak.rb', line 143

def cleanup(&callback)
  logger.log_info "dakwak: dakwak cleaning up..."

  cleaner = Proc.new {
    @@cleanup_handlers.each { |handler| handler.call }

    callback.call if callback

    logger.log_info "dakwak: dakwak cleaned up"

    @@logger = nil
    @@log_manager = nil
  }

  if light_init? then
    cleaner.call
  else
    EventMachine.next_tick do
      @@station.shutdown {
        EventMachine.stop if @@my_own_reactor

        cleaner.call

        @@worker.join if @@worker
      }
    # sleep(@@sleep_ms)
    end
  end
end

.debugging?Boolean Also known as: development?

:category: Utility

Returns:

  • (Boolean)


214
215
216
# File 'lib/dakwak.rb', line 214

def debugging?
  ENV["DAKWAK_DEBUG"] or app_fqn.include?("cornholio")
end

.environmentObject

The runtime environment, if the environment variable DAKWAK_DEBUG is defined, then the environment is set to :development, otherwise it is :production.



208
209
210
# File 'lib/dakwak.rb', line 208

def environment
  Dakwak.debugging? ? :development : :production
end

.init(info, foreground = true, install_trap = true, &callback) ⇒ Object

Initializes the logging system and other sub-systems. This must be called before attempting to use anything!

Params:

info

{ name: String, version: [ MAJOR, MINOR, PATCH, BUILD ] }

foreground

Whether dakwak should be run in the foreground or in a detached thread. If passing false, the init and teardown routines will force a sleep of 250 milliseconds.

install_trap

When true, SIGINT will be trapped to call ::cleanup and stop the EventMachine, terminating gracefully. If you need to trap SIGINT and define custom behaviour, make sure to call ::cleanup in your signal handler.

Note: this parameter is ignored when foreground = false



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dakwak.rb', line 46

def init(info, foreground = true, install_trap = true, &callback)

  @@sleep_ms = 100 / 1000.0

  booter = lambda {
    @@worker = nil
    @@cleanup_handlers = []
    @@app_name = info[:name]
    @@app_fqn = format_name(info)

    @@log_manager = LogManager.new
    @@logger = Object.new
    @@logger.extend(Logger)
    @@logger.logging_context("")

    @@station = Station.new

    callback.call if callback

    logger.log_info "dakwak: running in debug mode" if Dakwak.debugging?
    logger.log_info "dakwak: dakwak initialized"

    if install_trap
      __die_gracefully
    end
  }

  if foreground then
    __start do booter.call end          
  else
    @@worker = Thread.new {
      __start do booter.call end
    }
    sleep(@@sleep_ms)
  end

end

.init?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/dakwak.rb', line 122

def init?
  !@@log_manager.nil?
end

.light_init(info) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dakwak.rb', line 126

def light_init(info)
  @@app_name = info[:name]
  @@app_fqn = format_name(info)

  @@log_manager = LogManager.new
  @@logger = Object.new
  @@logger.extend(Logger)
  @@logger.logging_context("")

  @@cleanup_handlers = []
end

.light_init?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/dakwak.rb', line 138

def light_init?
  !EventMachine.reactor_running?
end

.log_managerObject

Used by Logger instances



230
231
232
# File 'lib/dakwak.rb', line 230

def log_manager()
  @@log_manager
end

.loggerObject

An anonymous Logger that objects can use when they won’t/can’t include an instance of Dakwak::Logger themseles



181
182
183
# File 'lib/dakwak.rb', line 181

def logger()
  @@logger
end

.production?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/dakwak.rb', line 221

def production?
  !debugging?
end

.stationObject

Use Dakwak::Station.instance instead



235
236
237
# File 'lib/dakwak.rb', line 235

def station
  @@station
end