Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Add ServiceRuntime sample #152

Merged
merged 4 commits into from Apr 17, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 91 additions & 0 deletions examples/helloruntime/HelloWorker/logger.js
@@ -0,0 +1,91 @@
/**
* Copyright 2011 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var azure = require('azure')
, path = require('path')
, util = require('./util.js')
, fs = require('fs');

var RoleEnvironment = azure.RoleEnvironment;

var LogFileKey = "DiagnosticLog";
var DefaultLogFileName = "DiagnosticLog.txt";

exports = module.exports = Logger;

/**
* Construct a logger that writes to the diagnostics store.
*
* @param {string} logFile: The name of the log file to write to.
*/
function Logger() {
this.setUp = false;
}

/**
* Write a message to the log.
*
* @param {string} message: The message to write.
*/
Logger.prototype.write = function(message) {
this._setupLog(function(error) {
this.outStream.write(message);
});
}

/**
* End the log file stream.
*/
Logger.prototype.end = function() {
this.outStream.end();
}

/**
* Get a readable stream of the log file contents.
*
* @return {ReadableStream}: The log file contents.
*/
Logger.prototype.getReadableStream = function() {
return fs.createReadStream(this.logFilePath);
}

/**
* Set up log file streaming based on configuration stored in the service definition.
*
* @param {function(error)} callback: The completion callback.
*/
Logger.prototype._setupLog = function(callback) {
if (!this.setUp) {
util.getConfigurationSetting(LogFileKey, function(error, logFileName) {
if (!logFileName || logFileName.length < 1) {
logFileName = DefaultLogFileName;
}
this.logFileName = logFileName;
util.getDiagnosticPath(function( error, logPath) {
this.setUp = true;
if (error) {
this.logFilePath = "console";
console.log("Error getting diagnostic store, using stdout instead: " + error + "\r\n");
this.outStream = process.stdout;
callback(error);
} else {
this.logFilePath = path.join(logPath, this.logFileName);
this.outStream = fs.createWriteStream(this.logFilePath);
callback(null);
}
});
});
}
}
10 changes: 10 additions & 0 deletions examples/helloruntime/HelloWorker/package.json
@@ -0,0 +1,10 @@
{
"name": "HelloWorker"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"azure": ">=0.5.4"
, "express": "2.5.8"
, "jade": ">= 0.0.1"
}
}
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
8 changes: 8 additions & 0 deletions examples/helloruntime/HelloWorker/routes/index.js
@@ -0,0 +1,8 @@

/*
* GET home page.
*/

exports.index = function(req, res){
res.render('index', { title: 'Express' })
};
58 changes: 58 additions & 0 deletions examples/helloruntime/HelloWorker/server.js
@@ -0,0 +1,58 @@
/**
* Copyright 2011 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Module dependencies.
*/

var express = require('express')
, Logger = require('./logger.js')
, routes = require('./routes')
, utility = require('./util.js');

var app = module.exports = express.createServer();

var Endpoint = "HttpIn";

var log = new Logger();

// Configuration

app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

utility.getListenAddress(Endpoint, function(error, host, port) {
app.listen(port, host);
console.log("Express server listening on hostname %s port %d in %s mode", host, port, app.settings.env);
log.write("Express server listening on hostname " + host +", port " + port + " in " + app.settings.env + "mode");
});
87 changes: 87 additions & 0 deletions examples/helloruntime/HelloWorker/util.js
@@ -0,0 +1,87 @@
/**
* Copyright 2011 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var azure = require('azure');

var RoleEnvironment = azure.RoleEnvironment;

var DiagnosticStoreKey = "DiagnosticStore";
var PathKey = "path";

exports = module.exports;

/**
* Return the path to the diagnostic store.
*
* @param {function(error, path)} callback: The completion callback.
*/
exports.getDiagnosticPath = function(callback) {
exports.getLocalResourcePath(DiagnosticStoreKey, callback);
}

/**
* Returne the path to the named local resource.
*
* @param {string} resourceName: Name of the local storage resource.
* @param {function(error, path)} callback: The completion callback.
*/
exports.getLocalResourcePath = function(resourceName, callback) {
RoleEnvironment.getLocalResources(function( error, resources) {
if (error) {
callback(error, null);
}
else {
if (resources[resourceName] && resources[resourceName][PathKey]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong indentantion between if and else

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

callback(null, resources[resourceName][PathKey]);
} else {
callback("Could not find resource '"+ resourceName + "'\r\n");
}
}
});
}

/**
* Get a specific role configuration setting.
*
* @param {string} key: The name of the configuration setting.
* @param {function(error, setting)} callback: The completion callback.
*/
exports.getConfigurationSetting = function(key, callback) {
RoleEnvironment.getConfigurationSettings( function(error, settings) {
if (!error) {
callback(null, settings[key]);
} else {
callback(error, null);
}

});
}

/**
* Get the hostname and port associated with the given endpoint for this role instance.
*
* @param {string} endpointName: The name of the endpoint in the service definition file.
* @param {function(error, host, port)} callback: The completion callback.
*/
exports.getListenAddress = function(endpointName, callback) {
RoleEnvironment.getCurrentRoleInstance(function( inErr, instance) {
if (inErr || !instance['endpoints'] || ! instance['endpoints'][endpointName]) {
callback("Error, could not get current role instance endpoint '"+ endpointName + "', error: " + inErr + "\r\n", null, null);
} else {
var currentEndpoint = instance['endpoints'][endpointName];
callback(null, currentEndpoint['address'], currentEndpoint['port']);
}
});
}
2 changes: 2 additions & 0 deletions examples/helloruntime/HelloWorker/views/index.jade
@@ -0,0 +1,2 @@
h1= title
p Welcome to #{title}
6 changes: 6 additions & 0 deletions examples/helloruntime/HelloWorker/views/layout.jade
@@ -0,0 +1,6 @@
!!!
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body!= body
8 changes: 8 additions & 0 deletions examples/helloruntime/ServiceConfiguration.Cloud.cscfg
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ServiceConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" serviceName="helloruntime" osFamily="1" osVersion="*" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
<Role name="HelloWorker">
<ConfigurationSettings />
<Instances count="1" />
<Certificates />
</Role>
</ServiceConfiguration>
8 changes: 8 additions & 0 deletions examples/helloruntime/ServiceConfiguration.Local.cscfg
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ServiceConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" serviceName="helloruntime" osFamily="1" osVersion="*" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
<Role name="HelloWorker">
<ConfigurationSettings />
<Instances count="1" />
<Certificates />
</Role>
</ServiceConfiguration>
21 changes: 21 additions & 0 deletions examples/helloruntime/ServiceDefinition.csdef
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<ServiceDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="helloruntime" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WorkerRole name="HelloWorker">
<Endpoints>
<InputEndpoint name="HttpIn" protocol="tcp" port="80" />
</Endpoints>
<Runtime>
<Environment>
<Variable name="PORT">
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='HttpIn']/@port" />
</Variable>
<Variable name="EMULATED">
<RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
</Variable>
</Environment>
<EntryPoint>
<ProgramEntryPoint commandLine="node.exe .\server.js" setReadyOnProcessStart="true" />
</EntryPoint>
</Runtime>
</WorkerRole>
</ServiceDefinition>