Running a task on role start in Azure

Apr 9, 2014 at 1:07 PM

I would like to run a simple command line script when the Node JS web role is ready to start on Azure.

In the C# world this is done by adding the following to the service definition:
   <NetFxEntryPoint assemblyName="MyWeb.dll" targetFrameworkVersion="v4.0"/>
And then in MyWeb.dll we add a class that derives from RoleEntryPoint and override the required methods:
public class MyRole : RoleEntryPoint
   public override bool OnStart()
      // Run the command line script here
       return base.OnStart();
Does anyone know the equivalent of this for a Node JS website?

What can be declared in the service definition that would cause Azure to look for an entry point in the deployed Node JS project?

Apr 22, 2014 at 5:17 PM
Sorry for the delayed response here, but you can do this via updating your ServiceDefinition.csdef file in the cloud project. It will already contain something like:
  <Task commandLine="bin\AzureSetup.cmd" executionContext="elevated" taskType="simple">
      <Variable name="EMULATED">
        <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
And you can add additional commands to be run on startup here and just include them in your Node.js project.
Apr 22, 2014 at 8:25 PM
@dinov, thanks for the response. Better late than never!

There is a very import point here with regard to an Azure deployment. I want to run a powershell script after the website has been created on IIS.

If you look at the MSDN documentation it says:
IIS may not be fully configured during the startup task stage in the startup process, so role-specific data may not be available. Startup tasks that require role-specific data should use Microsoft.WindowsAzure.ServiceRuntime.RoleEntryPoint.OnStart.
What is the equivalent of Microsoft.WindowsAzure.ServiceRuntime.RoleEntryPoint.OnStart for a Node deployment?

At the moment I'm carrying out the work using a background startup task. But this is dangerous because
  • Azure does not wait for the background task to complete before letting in traffic, and
  • The failure of the background task will not prevent the role from starting.