Skip to content

Instantly share code, notes, and snippets.

View avnersorek's full-sized avatar

Avner Sorek avnersorek

View GitHub Profile
@avnersorek
avnersorek / 3rd-party-new-process.js
Last active July 22, 2016 20:24
Calling 3rd party in a new process
'use strict';
const cp = require('child_process');
const showdown = require('showdown');
const config = require('my-config');
const requestTimeout = config.get('app:requestTimeoutMs');
const converter = new showdown.Converter();
// in the master process
@avnersorek
avnersorek / 3rd-party-in-process.js
Created July 20, 2016 11:58
Calling 3rd party in-process
'use strict';
const showdown = require('showdown');
const converter = new showdown.Converter();
function markdownToHtml(markdown) {
return converter.makeHtml(markdown);
}
module.exports = {
markdownToHtml
@avnersorek
avnersorek / 000logdna.config
Last active May 11, 2017 04:03
AWS Elastic Beanstalk configuration script for forwarding logs to LogDNA
# Place this file in the .ebextensions folder of your project
files:
"/home/ec2-user/logdna.sh" :
mode: "000777"
owner: root
group: root
content: |
#!/bin/sh
echo "[logdna]
name=LogDNA packages
import {go, chan, take, put} from 'js-csp';
let channel = chan();
go(function* () {
const text = yield take(ch);
console.log('A > RECEIVED:', text);
});
go(function* () {
const text = yield take(ch);
var redisGet = Q.nbind(redisClient.get, redisClient);
var mysqlGet = Q.nbind(mysqlClient.get, mysqlClient);
function GetUser(userId) {
return redisGet(userId)
.then(function(redisResult){
if (redisResult !== null) return redisResult;
else return mysqlGet(userId);
});
}
function GetUser(userId, callback) {
redisClient.get(userId, function(redisError, redisResult){
if (redisError !== null) callback(redisError, null);
else if (redisResult !== null) callback(null, redisResult);
else {
mysqlClient.getUser(userId, function(mysqlError, mysqlResult){
if (mysqlError !== null) callback(mysqlError, null);
else callback(null, mysqlResult);
});
}
public User Get(string userId) {
User cachedUser = redisClient.GetUser(userId);
if (cachedUser !== null) return cachedUser;
else return mySqlClient.GetUser(userId);
}
@avnersorek
avnersorek / doorbell.js
Last active August 29, 2015 14:25
Doorbell.io Node.js server side integration
var crypto = require('crypto');
var DOORBELL_SECRET = '--your secret--';
var DOORBELL_KEY = '--your app key--';
function getOptions() {
var options = {
appKey: DOORBELL_KEY,
timestamp: Math.floor(new Date() / 1000),
token: crypto.randomBytes(16).toString('hex')
@avnersorek
avnersorek / checkOpenPullRequests.js
Last active May 23, 2017 07:52
NodeJS script used to check GitHub for open pull requests - for several / multiple repositories - and send a message to Slack. We have this running every 30 minutes so the bot nags us about pull requests we are not attending to.
"use strict";
var request = require('superagent');
var Q = require('q');
var config = {
github : {
// https://help.github.com/articles/creating-an-access-token-for-command-line-use/
user : GITHUB_USERNAME,
password : GITHUB_TOKEN
},
@avnersorek
avnersorek / supertestForNaught.js
Created May 20, 2014 22:43
This is an example of an express node.js server which can be launched using naught, with an integration test using supertest. Each worker naught runs will test itself on a unique port, and only if passes it will listen in on the main port and join the pool.
var express = require('express');
var supertest = require('supertest');
var mainport = 80;
var app = express();
app.get('/test', function(req, res) { res.send({ 'pid' : process.pid }); } );
function listenToMainport() {
app.listen(mainport, function(){