This repository has been archived by the owner on May 5, 2023. It is now read-only.
Add ServiceRuntime sample #152
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
df45877
Merge remote-tracking branch 'upstream/master' into dev
markcowl 81eea33
Merge branch 'dev' of https://github.com/WindowsAzure/azure-sdk-for-n…
markcowl 47054b3
Add service runtime sample:
markcowl 9467c2b
Addressed review comments
markcowl File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} | |||
}); | |||
}); | |||
} | |||
} |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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" | |||
} | |||
} |
8 changes: 8 additions & 0 deletions
8
examples/helloruntime/HelloWorker/public/stylesheets/style.css
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
body { | |||
padding: 50px; | |||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; | |||
} | |||
|
|||
a { | |||
color: #00B7FF; | |||
} |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
|
|||
/* | |||
* GET home page. | |||
*/ | |||
|
|||
exports.index = function(req, res){ | |||
res.render('index', { title: 'Express' }) | |||
}; |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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"); | |||
}); |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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]) { | |||
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']); | |||
} | |||
}); | |||
} |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
h1= title | |||
p Welcome to #{title} |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,6 @@ | |||
!!! | |||
html | |||
head | |||
title= title | |||
link(rel='stylesheet', href='/stylesheets/style.css') | |||
body!= body |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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> |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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> |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.