Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

S3 brain resets brain auto-save interval

- Outsmarted myself using a separate timer for the S3 brain, as this
prevents it from responding to regular .save() calls
- Added environment variable HUBOT_S3_BRAIN_SAVE_INTERVAL, which
defaults to 30 minutes
- Improved docs
  • Loading branch information...
commit 59ddb0bd3869bd767766aa4675eb93acb31e32be 1 parent a40aa26
Ethan J. Brown Iristyle authored
Showing with 20 additions and 9 deletions.
  1. +20 −9 src/scripts/s3-brain.coffee
29 src/scripts/s3-brain.coffee
View
@@ -5,13 +5,20 @@
# "aws2js": "0.7.10"
#
# Configuration:
-# HUBOT_S3_BRAIN_ACCESS_KEY_ID
-# HUBOT_S3_BRAIN_SECRET_ACCESS_KEY
-# HUBOT_S3_BRAIN_BUCKET
+# HUBOT_S3_BRAIN_ACCESS_KEY_ID - AWS Access Key ID with S3 permissions
+# HUBOT_S3_BRAIN_SECRET_ACCESS_KEY - AWS Secret Access Key for ID
+# HUBOT_S3_BRAIN_BUCKET - Bucket to store brain in
+# HUBOT_S3_BRAIN_SAVE_INTERVAL - [Optional] auto-save interval in seconds
+# Defaults to 30 minutes
#
# Commands:
#
# Notes:
+# Take care if using this brain storage with other brain storages. Others may
+# set the auto-save interval to an undesireable value. Since S3 requests have
+# an associated monetary value, this script uses a 30 minute auto-save timer
+# by default to reduce cost.
+#
# It's highly recommended to use an IAM account explicitly for this purpose
# https://console.aws.amazon.com/iam/home?
# A sample S3 policy for a bucket named Hubot-Bucket would be
@@ -43,19 +50,24 @@
util = require 'util'
aws = require 'aws2js'
-# sets up hooks to persist the brain into redis.
module.exports = (robot) ->
loaded = false
key = process.env.HUBOT_S3_BRAIN_ACCESS_KEY_ID
secret = process.env.HUBOT_S3_BRAIN_SECRET_ACCESS_KEY
bucket = process.env.HUBOT_S3_BRAIN_BUCKET
+ # default to 30 minutes (in seconds)
+ save_interval = process.env.HUBOT_S3_BRAIN_SAVE_INTERVAL || 30 * 60
brain_dump_path = "#{bucket}/brain-dump.json"
if !key && !secret && !bucket
throw new Error('S3 brain requires HUBOT_S3_BRAIN_ACCESS_KEY_ID, ' +
'HUBOT_S3_BRAIN_SECRET_ACCESS_KEY and HUBOT_S3_BRAIN_BUCKET configured')
+ save_interval = parseInt(save_interval)
+ if isNaN(save_interval)
+ throw new Error('HUBOT_S3_BRAIN_SAVE_INTERVAL must be an integer')
+
s3 = aws.load('s3', key, secret)
store_brain = (brain_data, callback) ->
@@ -78,10 +90,6 @@ module.exports = (robot) ->
store_current_brain = () ->
store_brain robot.brain.data
- # 30 minute timer based save to limit s3 PUT requests
- time = 30 * 60 * 1000
- intervalId = setInterval store_current_brain, time
-
s3.get brain_dump_path, 'buffer', (err, response) ->
# unfortunately S3 gives us a 403 if we have access denied OR
# the file is simply missing, so no way of knowing if IAM policy is bad
@@ -96,8 +104,11 @@ module.exports = (robot) ->
robot.brain.on 'loaded', () ->
loaded = true
+ robot.brain.resetSaveInterval(save_interval)
+ store_current_brain()
+
+ robot.brain.on 'save', () ->
store_current_brain()
robot.brain.on 'close', ->
- clearInterval intervalId
store_current_brain()
Please sign in to comment.
Something went wrong with that request. Please try again.