Question

I would like to enable or disable start up tasks depending on the build configuration or service configuration. What I'm hoping to achieve is that I can disable New Relic on our test environment but enable the start up task for the stage and production environment. Is that possible?

Was it helpful?

Solution

The answer by NR_Jacob inspired me, thanks! I found out that you could pass a setting of the role to the batch file. So you add a setting like this: Cloud configuration settings And specify true or false for each individual Service Configuration.

Then in the ServiceDefinition.csdef you add the and lines:

<Task commandLine="newrelic.cmd" executionContext="elevated" taskType="simple">
  <Environment>
        <Variable name="DisableNewRelic">
            <RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='DisableNewRelic']/@value" />
        </Variable>
  </Environment>
</Task>

And within newrelic.cmd add these lines at the top:

IF "%DisableNewRelic%" == "true" (
    ECHO Found setting to disable New Relic, exiting... >> "%RoleRoot%\nr.log" 2>&1
    EXIT /B 0
)

@NR_Jacob: Can you add the lines of newrelic.cmd to your code base? Doesn't hurt to have a setting like that in the cmd ;-) Otherwise I would have to do the modifications to newrelic.cmd every time there is an update to the nuget package.

OTHER TIPS

This article from Microsoft has a good solution for this type of problem. To summarize:

Add an Environment variable to the ServiceDefinition.csdef file like the following:

<Variable name="ComputeEmulatorRunning">
    <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" />
</Variable>

This will set the variable to either true if you are running in the emulator or false if not. Next you will need to alter the newrelic.cmd file to wrap the whole file thusly:

IF "%ComputeEmulatorRunning%" == "true" (
    REM   This task is running on the compute emulator. Nothing goes here since we want nothing to happen.
) ELSE (
    REM   This task is running on the cloud. Place the entirety of the newrelic.cmd file in here.
)

This will still call the cmd file, but will prevent it from doing anything except in production.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top