Aaronontheweb

Hacking .NET and Startups

How to Use the Azure npm Package Locally without the Azure Compute Emulator

February 6, 2012 05:28 by Aaronontheweb in Azure, Node // Tags: , , , // Comments (2)

One thing that is a little dicey about the Windows Azure SDK for Node 1 is that it by default it depends on being run inside of Azure itself or the compute emulator.

The Azure npm package looks for environment variables parsed from web.config and won’t find them if you run your node application via the node [entrypoint].js commandline.

So why would you want to run your Node application outside the Azure emulator if you’re utilizing the Azure npm package? If you’re using Cloud9 to develop and deploy Node applications to Windows Azure, then that’s one reason.

Another is that IIS eats any error messages your Node application throws by default2 and on some occasions errors don’t always get logged to server.js/logs/[n].txt, so occasionally you have to debug by running the stand-alone node server where you get verbose error messages to console.

To work around this, you can set your tableClient / storageClient / queueClient object to target a specific account directly.

//Emulator
var tableClient = azure.createTableService(ServiceClient.DEVSTORE_STORAGE_ACCOUNT, ServiceClient.DEVSTORE_STORAGE_ACCESS_KEY, ServiceClient.DEVSTORE_TABLE_HOST);

//Live account
var tableClient = azure.createTableService(‘aaronnodedemo’,’scary-looking-access-key’,’ [account].table.core.windows.net’);

When you do this you lose the ability to let Azure .config transforms enable you to switch between production and staging easily, but this can be a necessary evil for debugging tricky bugs ;)


1“azure” is the name of the associated npm package

2I’m sure there’s a way to change that behavior in the IISnode configuration settings

If you enjoyed this post, make sure you subscribe to my RSS feed!



Code Camp Talks: Intro to Node.JS and Building Web Apps with Express

February 2, 2012 17:15 by Aaronontheweb in Node, Azure // Tags: , , , // Comments (0)

This past weekend at SoCal Code Camp I gave two presentations back-to-back on Node.JS: “Intro to Node.JS” and “Building Web Apps with Express.”

I don’t have much to add on what I did at the sessions other than to mention just how surprised I was at how enthusiastic people were to see Microsoft involved with the Node effort and how eager everyone was to learn Node. I was thoroughly impressed.

Below are links to my slides and code samples for both talks – enjoy!

Intro to Node.JS

Source code: Github or Cloud9

 

 

Building Web Apps with Express

 

 

Let me know if you have any questions!

If you enjoyed this post, make sure you subscribe to my RSS feed!



How to Automatically Utilize Multicore Servers with Node on Windows Azure

January 17, 2012 11:31 by Aaronontheweb in Azure, Azure, Node, Node // Tags: , , , // Comments (0)

One major advantage of developing Node applications for Windows Azure is the ability to have your Node apps managed directly by IIS via iisnode.

You can read more about the benefits of iisnode here, but I want to call out these two points specifically from the iisnode wiki:

Scalability on multi-core servers. Since node.exe is a single threaded process, it only scales to one CPU core. The iisnode module allows creation of multiple node.exe processes per application and load balances the HTTP traffic between them, therefore enabling full utilization of a server’s CPU capacity without requiring additional infrastructure code from an application developer.

Process management. The iisnode module takes care of lifetime management of node.exe processes making it simple to improve overall reliability. You don’t have to implement infrastructure to start, stop, and monitor the processes.

Scalability on multicore servers with Node typically requires developers to use the Node cluster module and write some code which spawns one node.exe process per available processor and write your own handlers for what to do in the event of a failed process and so forth.

Update: It occurred to me after I initially published this that many developers may not understand the need for multiple node.exe processes. If you read through my “Intro to Node.JS for .NET Developers“ you’ll get a better sense for how Node works under the hood – the gist of it is that Node handles all incoming HTTP requests on a single thread, and thus can only utilize a single core at any given time regardless of the number of available processors. Having multiple Node.exe processes handling HTTP requests allows for multiple event loop threads, all of which can be run in parallel across different processors. That’s why this technique is important for multicore systems.

IIS and iisnode can take care of this for you automatically without you having to write any code to do it (a good thing,) and on Windows Azure you can automate this via startup task (fork the source on Github:)

if "%EMULATED%"=="true" exit /b 0

REM Count the total number of available processors on this system
powershell -c "exit [System.Environment]::ProcessorCount"

REM set the default number of processes for our app pools in IIS equal to the 
number of available processors
%windir%\system32\inetsrv\appcmd set config -section:applicationPools 
-applicationPoolDefaults.processModel.maxProcesses:%ErrorLevel%

This startup task automatically determines the number of available processors on the CPU and tells IIS to set the number of worker processes in your Node application pools to use one process-per-core, thus allowing your Node applications to take advantage of every core on the system.

Adding Startup Tasks to Node Projects

If you want to use this startup task in your Node project, follow these steps:

image

  • The last thing you need to do is just include the startup task in your ServiceDefinition.csdef file, located in the root of your Node Azure Service. The Startup section of the file should look like this:
    <Startup>
      <!-- Included by default; installs everything you need to run Node on Azure -->
      <Task commandLine="setup_web.cmd" executionContext="elevated">
        <Environment>
          <Variable name="EMULATED">
            <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
          </Variable>
        </Environment>
      </Task>
      <Task commandLine="setMaxProcessesToAvailableProcessors.cmd" 
        executionContext="elevated">
        <Environment>
          <Variable name="EMULATED">
            <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
          </Variable>
        </Environment>
      </Task>
    </Startup>
    Once all of this is setup, go ahead and deploy your Node service to Azure and it will be able to take advantage of all of the cores on the VMs. The script will dynamically scale with the size of your role instance, so there is no need to alter it.

To verify that the script worked:

  • RDP into one of your Node + Azure instances;
  • Go to IIS Manager from the Windows Menu;
  • Go to Application Pools;
  • Right click on the Application Pool for your Node application and select Advanced Settings – your application pool will be the one that has Applications = 1 on the far right of the table; and lastly
  • Scroll down to Maximum Worker Processes and check the value – in the screenshot below I’m running on a pair of Medium Azure instances, which have two cores each and thus two processes.

image

image

And voila, you’re done.

Let me know if you have any questions!

If you enjoyed this post, make sure you subscribe to my RSS feed!



Node.JS on Windows Azure Part 1: Setting Up Your Environment

January 9, 2012 19:14 by Aaronontheweb in Azure, Node // Tags: , , , // Comments (1)

Following Microsoft’s announcements regarding first-class Node.JS support on Windows Azure, I thought it would be helpful to walk newbie Node.JS and Windows Azure developers through the process of getting their Node.JS development environment set up, building their first simple Node.JS application, and then learning how to take advantage of popular NPM packages like Express to build actual web applications on Windows Azure.

If you need a primer on how Node.JS works and what Node is useful for, go ahead and check out my post from last week: “Intro to Node.JS for .NET Developers.”

Setting Up Your Node.JS + Windows Azure Environment

First, a big disclaimer: all of the Windows Azure deployment and packaging components require Windows in order to work. You will not be able to deploy your application to Windows Azure without a Windows Vista / 7 box.

That being said, the rest of the setup process is straight forward and all of the tools are free (with the exception of one possible text editor I am going to recommend.)

Step 1 – Install the Node.JS Tools for Windows Azure Tools via Web PI

The first thing you need to do is install the Node.JS tools for Windows Azure via Web Platform Installer – click here to install the tools.

image

This will install:

  • Node.JS for Windows;
  • Node Package Manager (NPM) for Windows; and
  • Windows Azure Powershell for Node.

If you already have a previous version of Node.JS for Windows installed (anything before the latest version, currently v0.6.6), you should uninstall it before running Web PI.

If for whatever reason you need to install Node.JS for Windows manually, you can do it via NodeJS.org.

Once the installation is finished, you should see the following item appear on your Windows bar:

image

Click on it and you’ll see a PowerShell command window fire up – this command window supports a number of Node + Azure-specific commands you can use for creating new roles and services.

image

We’ll get around to playing with PowerShell for Node a little later – next we need to set up your Windows Azure account.

Step 2 – Get a Windows Azure Account

In order to take advantage of Node.JS for Windows Azure you’re going to need to have… a Windows Azure account.

If you’re looking for a way to try Windows Azure for free, I recommend signing up for the three-month Windows Azure free trial.

If you’re in a startup and want access to some longer-term Windows Azure benefits, sign up for BizSpark, activate your BizSpark Windows Azure benefits, and get access to some more substantial Windows Azure benefits.

Step 3 – Install a Text Editor with JavaScript Syntax Highlighting

When you step into the world of Node.JS, you’re no longer in a universe where Visual Studio can really help you. Thus, we need to pick up a text editor to use in our Node.JS development.

I recommend that you pick one of the following:

  • Notepad++ – JavaScript syntax highlighting support, lightweight, simple formatting tools, and has a decent number of plug-ins. Cost: free.
  • Sublime Text – this is the community favorite for doing Node.JS; it’s cross-platform so it works on OS X, Linux, and Windows. Cost: free trial, but $59 for a single seat.

Install one of these two text editors and start playing around with it… You’ll get used to it.

Step 4 – Create Your First Node.JS + Azure Project

Now that you have your development environment all set up, now it’s time to create your first Node.JS on Azure project. The official Node.JS for Windows Azure guide will walk you through a lot of the same steps in more detail if you wish to use that instead.

Inside the PowerShell for Node.JS command window, do the following:

  • Create a directory for saving all of  your Node.JS projects – if you want to use “C:\Repositories\Node\” then you can just type mkdir C:\Repositories\Node inside of PowerShell and it will create the directory for you.
  • Change the current working directory to C:\Repositories\Node – type cd C:\Repositories\Node inside the PowerShell window.
  • Create a new Windows Azure service by typing New-AzureService [projectname] – this will automatically create a new [projectname] subdirectory and set it to the current working directory.
  • Create a new Node.JS Web Role (an Azure role that serves HTTP requests from within IIS) by typing Add-AzureNodeWebRole [rolename] – this will automatically create a set of folders under the [projectname]\[rolename] directory which contain everything you need to run your first Node.JS + Azure project.

Here’s what the output looks like in the command window:

image

Step 5 – Fire Up Your Node.JS Project in the Emulator

Just to make sure that our simple “Hello World” Node.JS project runs properly, we’re going to run it in the emulator before we do anything else with it.

Simply type “Start-AzureEmulator – launch” in the command line, and you’ll see a page like the one below:

image

Step 6 – Download and Import Your Windows Azure Publication Settings

In order to actually publish to Windows Azure, you have to download your .publishSettings file from the Windows Azure portal. Luckily, you can just do this using the Get-AzurePublishSettings command in the PowerShell window.

This will launch a new web browser window to this page on the Windows Azure portal, which will have you login to your Azure account and will then automatically ask you where you want to save your Windows Azure publication settings.

image

Save your Windows Azure publication settings to C:\Repositories\Node\[projectname].

image

Finally, you just need to import your publication settings in the Node.JS PowerShell Window like this:

Import-AzurePublishSettings C:\Repositories\[projectname]\[file-you-downloaded].publishSettings

You should see a message like this in the command window when the operation succeeds:

image

Step 7 – Publish (and then Take Down) Your “Hello World” Node.JS Project to Windows Azure

The last step before we start diving into Node.JS is getting some experience publishing Node.JS projects to Windows Azure – now that we’ve imported our .publishSettings file we can easily publish our little “Hello World” example directly to the Windows Azure account we set up earlier using the following command in the PowerShell terminal:

Publish-AzureService –name [NewServiceName] –location “North Central US” –launch

This will automatically publish the Windows Azure service in your current working directory (C:\Repositories\Node\[projectname]) to a new hosted service in the North Central United States Azure Data Center called “NewServiceName” with one small instance of the [rolename] Node.JS web role running. The –launch flag will automatically trigger the browser window to open to your service once it finishes deploying.

Here’s what the output window will look like in PowerShell upon a successful deployment:

image

And you’ll see this in your browser window once the deployment is complete:

image

If you’re using the Windows Azure free trial or a BizSpark Windows Azure account, you can leave up your Node.JS “Hello World” project if you wish as you’re not going to incur a bill.

However, it’s generally a good practice to take down Windows Azure instances that aren’t doing actual work because in the future you will be billed for the amount of time of the instance is deployed… So to take down an Azure deployment we can either delete it through the Windows Azure management portal or through the following command in the Node.JS PowerShell:

Remove-AzureService

That’s it for this tutorial – the next lesson will show you how to get your hands dirty with the Node Package Manager and Express.JS for doing MVC-style websites in Node!

If you enjoyed this post, make sure you subscribe to my RSS feed!



New Windows Phone 7 App: Lolcats Professional Pro Plus

December 22, 2011 08:30 by Aaronontheweb in Windows Phone 7 // Tags: , , // Comments (0)

localcatsproplus-icon-largeIn addition to my regular work for Microsoft and the open source projects I create and contribute to on Github, I also like to ship the occasional app or website under my StannardLabs masthead.

My absolute favorite app that I shipped early in the summer is Lolcats Professional Pro Plus, a simple app that makes it a breeze to flip through pictures of Lolcats from ICanHasCheezburger.

I’ve since modified the app pretty drastically so that it now includes touch support, a clean minimalist UI, and a selection of 60,000 plus images to page through, with hundreds more being added every day.

Lolcats Professional Pro Plus is currently designed for Windows Phone 7.0, although a Mango version is on the way soon. Here’s what the app currently looks like:

screenshot1screenshot2screenshot3screenshot4

Do me a favor and go download the app from the Windows Phone 7 app store – it’s free and is a great way to kill time when you need it.

If you enjoyed this post, make sure you subscribe to my RSS feed!



How to Modify Machine.config on Windows Azure Web Roles

December 1, 2011 19:04 by Aaronontheweb in Azure // Tags: , , // Comments (0)

Earlier this week I spent some time helping a company troubleshoot some performance issues with Windows Azure – their ASP.NET request queue was growing longer than the maximum amount of queued requests supported by default in IIS (5000) during bursts of high activity, so I had to help them find a way to empty the queue faster.

One way to do this is to modify the number of max/min IO and worker threads available to any web role instance, and these values can only be modified through machine.config settings.

As you know, Windows Azure VMs are not persistent so you can’t modify machine.config and save it to file system like how you might if you were hosting IIS on your own local box.

Thus, I crafted a solution using AppCmd and Windows Azure startup tasks.

Here’s some of the code below:

REM Increases the number of available IIS threads for high performance applications REM Uses the recommended values from http://msdn.microsoft.com/en-us/library/ms998549.aspx#scalenetchapt06_topic8 REM Values may be subject to change depending on your needs REM Assumes you're running on two cores (medium instance on Azure) %windir%\system32\inetsrv\appcmd set config /commit:MACHINE -section:processModel -maxWorkerThreads:100 >> log.txt 2>> err.txt %windir%\system32\inetsrv\appcmd set config /commit:MACHINE -section:processModel -minWorkerThreads:50 >> log.txt 2>> err.txt %windir%\system32\inetsrv\appcmd set config /commit:MACHINE -section:processModel -minIoThreads:50 >> log.txt 2>> err.txt %windir%\system32\inetsrv\appcmd set config /commit:MACHINE -section:processModel -maxIoThreads:100 >> log.txt 2>> err.txt

I’ve open-sourced the rest of my IIS machine.config / app pool scripts on Github. Take a look at them and let me know if you have any questions.

If you enjoyed this post, make sure you subscribe to my RSS feed!



Search

About

My name is Aaron, I'm an entrepreneur and a .NET developer who develops web, cloud, and mobile applications.

I left Microsoft recently to start my own company, MarkedUp - we provide analytics for desktop developers, focusing initially on Windows 8 developers.

You can find me on Twitter or on Github!

Recent Comments

Comment RSS

Sign in