Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduler! #44

Merged
merged 9 commits into from Aug 6, 2015
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "0.10.34"
- "0.10.36"
before_install:
- npm install -g bower
- bower install
@@ -14,3 +14,4 @@ cache:
- node_modules
services:
- redis
sudo: false
@@ -6,6 +6,5 @@ logger = require './src/logger'
logger.debug "Loaded config file"
logger.debug JSON.stringify(config, null, 2)

new Server(config.server.host, config.server.port, { api_namespace: '/api/v1' }).run(->
new Server(config.server.host, config.server.port, { api_namespace: '/api/v1' }).run ->
logger.info "Express server started on #{config.server.host}:#{config.server.port}"
)
@@ -0,0 +1,43 @@
#!/usr/bin/env ./node_modules/.bin/coffee

# Scheduler binary checks time and sets lights via HTTP API calls
# if COLORPICKER_ON time, turns off lights. if COLORPICKER_OFF time, turns on lights.
# @note intended to be run by cron (hourly)

request = require 'request'
config = require 'config'
moment = require 'moment'
logger = require '../src/logger'

# ugly munging to build fully qualified prod/dev API URL
domain = ->
domain = config.server.domain
if process.env.NODE_ENV != 'production'
domain = "#{domain}:#{config.server.port}"
else
domain = "#{process.env.USERNAME}:#{process.env.PASSWORD}@#{domain}"

# request to turn off LEDs
turn_off = ->
off_url = "http://#{domain()}/api/v1/manager/off"
request off_url, (error, response, body) ->
logger.info 'response: ', response.body
if response.statusCode == 200
logger.info 'turned LEDs off'
process.exit

# request to turn on LEDs
random = ->
random_url = "http://#{domain()}/api/v1/manager/random"
request random_url, (error, response, body) ->
logger.info 'response: ', response.body
if response.statusCode == 200
logger.info 'turned LEDs on'
process.exit

# scheduler business logic
do run = ->
now = moment()
logger.info "now.hour(): #{now.hour()}, COLORPICKER_ON: #{process.env.COLORPICKER_ON}"
turn_off() if now.hour() == parseInt(process.env.COLORPICKER_OFF)
turn_on() if now.hour() == parseInt(process.env.COLORPICKER_ON)
@@ -1,6 +1,7 @@
module.exports = {
"server": {
"host": "127.0.0.1",
"domain": "localhost",
"port": 1337
},

@@ -3,6 +3,7 @@ var rtg = require("url").parse(process.env.REDISTOGO_URL);
module.exports = {
"server": {
"host": "0.0.0.0",
"domain": "colorpicker.herokuapp.com",
"port": process.env.PORT
},

@@ -1,6 +1,7 @@
module.exports = {
"server": {
"host": "127.0.0.1",
"domain": "localhost",
"port": 1338
},

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.