Skip to content

Loading…

Add ServiceRuntime sample #152

Merged
merged 4 commits into from

2 participants

@markcowl
Microsoft Azure member

Sample shows:

(1) Retrieving local config
(2) Getting endpoint details for the local instance
(3) Writing to the DiagnosticStore LocalResource

@andrerod andrerod commented on an outdated diff
examples/helloruntime/HelloWorker/logger.js
((9 lines not shown))
+* 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 = "DiagnisticLog.txt";

Diagnostic ?

@markcowl Microsoft Azure member

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@andrerod andrerod commented on an outdated diff
examples/helloruntime/HelloWorker/util.js
((14 lines not shown))
+*/
+
+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

Wrong spacing betweetn the type and the parameter name.

@markcowl Microsoft Azure member

Fixed, I believe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@andrerod andrerod commented on the diff
examples/helloruntime/HelloWorker/util.js
((21 lines not shown))
+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

header comments should end with '.'.

@markcowl Microsoft Azure member

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@andrerod andrerod commented on the diff
examples/helloruntime/HelloWorker/util.js
((32 lines not shown))
+ 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]) {

wrong indentantion between if and else

@markcowl Microsoft Azure member

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@andrerod andrerod commented on the diff
examples/helloruntime/HelloWorker/util.js
((52 lines not shown))
+ }
+ }
+ });
+}
+
+/**
+* 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]);
+ }

we inline the else's with the closing braces. E.g. '} else {'

@markcowl Microsoft Azure member

Fixed globally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@andrerod

Added a few comments mark. Sorry i missed this PR the other day. Could you go through the feedback ? Thank you.

@markcowl
Microsoft Azure member
@markcowl
Microsoft Azure member

Addressed review comments, please comment or merge.

@andrerod

Looks good.

@andrerod andrerod merged commit 53994cc into Azure:dev
@andrerod andrerod added a commit that referenced this pull request
@andrerod andrerod #152: Fixing a bunch of JSON outputs. 8b96f3d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 17, 2012
  1. @markcowl
Commits on Mar 30, 2012
  1. @markcowl
Commits on Apr 11, 2012
  1. @markcowl

    Add service runtime sample:

    markcowl committed
Commits on Apr 16, 2012
  1. @markcowl

    Addressed review comments

    markcowl committed
View
91 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);
+ }
+ });
+ });
+ }
+}
View
10 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"
+ }
+}
View
8 examples/helloruntime/HelloWorker/public/stylesheets/style.css
@@ -0,0 +1,8 @@
+body {
+ padding: 50px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+
+a {
+ color: #00B7FF;
+}
View
8 examples/helloruntime/HelloWorker/routes/index.js
@@ -0,0 +1,8 @@
+
+/*
+ * GET home page.
+ */
+
+exports.index = function(req, res){
+ res.render('index', { title: 'Express' })
+};
View
58 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");
+});
View
87 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.

header comments should end with '.'.

@markcowl Microsoft Azure member

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+*
+* @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]) {

wrong indentantion between if and else

@markcowl Microsoft Azure member

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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 {

we inline the else's with the closing braces. E.g. '} else {'

@markcowl Microsoft Azure member

Fixed globally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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']);
+ }
+ });
+}
View
2 examples/helloruntime/HelloWorker/views/index.jade
@@ -0,0 +1,2 @@
+h1= title
+p Welcome to #{title}
View
6 examples/helloruntime/HelloWorker/views/layout.jade
@@ -0,0 +1,6 @@
+!!!
+html
+ head
+ title= title
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ body!= body
View
8 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>
View
8 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>
View
21 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>
Something went wrong with that request. Please try again.