Skip to content

Instantly share code, notes, and snippets.

💻
serverless all the things.

James Thomas jthomas

💻
serverless all the things.
Block or report user

Report or block jthomas

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jthomas
jthomas / action.js
Last active May 14, 2019
Example showing how to handle intermittant action failures for large number of invocations using Redis
View action.js
"use strict";
// What percentage of invocations should randomly fail?
const ERROR_RATE = 0.25
// Random delay to results being returned (0 -> 10 seconds)
const DELAY_MS = Math.random() * 10000
function should_fail () {
return Math.random() < ERROR_RATE
@jthomas
jthomas / action.js
Last active May 13, 2019
Example showing how to handle intermittant action failures for large number of invocations
View action.js
"use strict";
const ERROR_RATE = 0.25
function should_fail () {
return Math.random() < ERROR_RATE
}
function main(params) {
if (!params.a || !params.b) throw new Error('Missing input parameters (a or b).')
View workers.js
'use strict';
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
const min = 2
function generatePrimes(start, range) {
const primes = []
let isPrime = true;
let end = start + range;
for (let i = start; i < end; i++) {
View primes.js
'use strict';
const min = 2
function main(params) {
const { start, end } = params
console.log(params)
const primes = []
let isPrime = true;
for (let i = start; i < end; i++) {
@jthomas
jthomas / primes-with-workers.js
Last active May 11, 2019
Calculating prime numbers on serverless platforms using Node.js Worker Threads and IBM Cloud Functions (Apache OpenWhisk)
View primes-with-workers.js
'use strict';
const { Worker } = require('worker_threads');
const os = require('os')
const threadCount = os.cpus().length
const compute_primes = async (start, range) => {
return new Promise((resolve, reject) => {
let primes = []
console.log(`adding worker (${start} => ${start + range})`)
@jthomas
jthomas / index.js
Created Aug 10, 2018
Serverless Machine Learning With TensorFlow.js and IBM Cloud Functions (Apache OpenWhisk)
View index.js
const tf = require('@tensorflow/tfjs')
const mobilenet = require('@tensorflow-models/mobilenet');
require('@tensorflow/tfjs-node')
const jpeg = require('jpeg-js');
const NUMBER_OF_CHANNELS = 3
const MODEL_PATH = 'mobilenet/model.json'
let mn_model
@jthomas
jthomas / package.json
Last active Jun 3, 2019
Using TensorFlow.js with MobileNet models for image classification on Node.js
View package.json
{
"name": "tf-js",
"version": "1.0.0",
"main": "script.js",
"license": "MIT",
"dependencies": {
"@tensorflow-models/mobilenet": "^0.2.2",
"@tensorflow/tfjs": "^0.12.3",
"@tensorflow/tfjs-node": "^0.1.9",
"jpeg-js": "^0.3.4"
@jthomas
jthomas / action.json
Last active May 8, 2018
IBM Cloud Monitoring service (Grafana) dashboards for monitoring IBM Cloud Functions (OpenWhisk) application metrics
View action.json
{
"annotations": {
"list": []
},
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"hideControls": false,
"id": 2309,
"links": [],
@jthomas
jthomas / counter.js
Created Dec 11, 2017
OpenWhisk Action - Storing State Without a Database
View counter.js
const openwhisk = require('openwhisk');
const main = async evt => {
const count = (evt.count || 0) + 1
// use client library to retrieve current function configuration
const ow = openwhisk()
const action = await ow.actions.get('counter')
// update default parameters with new value
@jthomas
jthomas / wake_up.js
Last active Oct 27, 2017
Apache OpenWhisk Advanced Alarm Schedule Events
View wake_up.js
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
}
You can’t perform that action at this time.