 memwatch   = require 'memwatch'
 v8profiler = require 'v8-profiler'
 
+state              = off
+bucket             = 'koding-nodejs-runtime-heapsnapshots'
 snapshotInterval   = null
 latestHeapSnapshot = {}
-
-state      = off
-bucket     = 'koding-nodejs-runtime-heapsnapshots'
-AWS_KEY    = null
-AWS_SECRET = null
 
 handleSnapshots = (info) ->
 

  # take an initial snapshot
  latestHeapSnapshot = { content: v8profiler.takeSnapshot(), date: Date.now() }

  # then take a heap snapshot every second
  snapshotInterval = setInterval ->
    latestHeapSnapshot = { content: v8profiler.takeSnapshot(), date: Date.now() }
  , 1000

  memwatch.on 'leak', handleSnapshots
  console.log "Started watching possible memory leakage for the process with id #{process.pid}"



setup = (options = {}) ->

  console.log "To start/stop watching memory profiling send SIGUSR2 to PID #{process.pid}"

  { AWS_KEY, AWS_SECRET } = options
