Last active
October 27, 2017 13:26
-
-
Save jthomas/d7c0bb846985e930924658ba8c0b950c to your computer and use it in GitHub Desktop.
Apache OpenWhisk Advanced Alarm Schedule Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var openwhisk = require('openwhisk'); | |
var request = require('request-promise'); | |
function getNextSunrise(lat, lng, when) { | |
const options = { | |
uri: 'https://api.sunrise-sunset.org/json', | |
qs: { lat: lat, lng: lng, when: when }, | |
json: true | |
} | |
return request(options) | |
.then(result => result.results.sunrise) | |
} | |
function calculateSchedule(sunrise) { | |
console.log('Next sunrise:', sunrise) | |
const sections = sunrise.split(':') | |
const hour = sections[0], minute = sections[1] | |
return `${minute} ${hour} * * *` | |
} | |
function scheduleSunriseEvent (sunrise) { | |
const ow = openwhisk(); | |
const params = {cron: sunrise, maxTriggers: 1} | |
return ow.feeds.delete({name: '/whisk.system/alarms/alarm', trigger: 'sunrise'}).then(() => { | |
console.log('trigger feed deleted.') | |
return ow.feeds.create({name: '/whisk.system/alarms/alarm', trigger: 'sunrise', params: params}) | |
}).then(result => { | |
console.log('trigger feed created.') | |
}) | |
.catch(err => { | |
console.error('failed to create/delete trigger', err) | |
console.log("ERROR", err.error.response.result) | |
}) | |
} | |
function main(params) { | |
console.log('GOOD MORNING!') | |
return getNextSunrise(params.lat, params.lng, 'tomorrow') | |
.then(calculateSchedule) | |
.then(scheduleSunriseEvent) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment