none
storing variable in serviceconfig.csfg file so worker role can read

    Question

  • Hi, I am thinking of using the serviceconfig file to store some variable such as user email, user ID etc., that can be referenced inside the workerrole.cs or a class file that gets instantiated inside the worker role.  I know i could use the appsettings approach for the web.config. I saw an example in a blog somewhere but couldn't find it. COuld some one help me with a steps or sample? Also, my cloud project will only contain the workerfole (no webrole).  

    The idea is to modify these variable at later time without having to redeploy the cloud project. Also could someone advise how can I modify the serviceconfig (where in the management portal)? 

    I am using the worker role to automate a process (similar to webserivce) that is timed to run every few hours or so. (i am using the thread.sleep to wait for 2 hours and repeat the process inside worker role). Is there any better way to do that?  

    Also, is it possible to execute some function say i have class called "converttocelcius". What I want is to instantiate and run this class inside the run method of the worker role every two hours. I am able to do that. So problem here. What I want is to call this function exactly or close to a specified time say 10AM every day. Is it possible and if so, could someone tell me what is the condition or code sample? 

    Thank a lot for your help. 

     

    Tuesday, July 24, 2012 1:39 PM

Answers

  • So if you open up your cscfg file in Visual Studio (settings for your Azure role are defined in this file), you will be able to see the settings for your role. Also there is a GUI based editor in VS for your config file. Just double click on your role name and then go to settings tab.

    For example, this is how it looks for one of my projects:

      <Role name="WorkerRole1">
        <Instances count="1" />
        <ConfigurationSettings>
          <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
          <Setting name="xyz" value="123" />
        </ConfigurationSettings>
      </Role>
    

    Hope this helps.

    • Marked as answer by CRM elite Tuesday, July 24, 2012 3:50 PM
    Tuesday, July 24, 2012 3:23 PM

All replies

  • The idea is to modify these variable at later time without having to redeploy the cloud project. Also could someone advise how can I modify the serviceconfig (where in the management portal)?

    Yes, it is possible to do so. On the new management portal, click on your cloud service in question, then go to the deployment slot (Staging or Production) and then click on "CONFIGURE" tab. There you will be able to change the existing settings. You could also upload a new configuration file there.

    What I want is to call this function exactly or close to a specified time say 10AM every day. Is it possible and if so, could someone tell me what is the condition or code sample?

    I'm assuming your Worker role is in an infinite loop, what you could do is check for time in that loop and if it is 10:00 AM, you execute some function. I've included a sample code below. Obviously you would need to ensure that you call this "TriggerMyFunction()" only once per day.

                
                while (true)
                {
                    Thread.Sleep(10000);
                    Trace.WriteLine("Working""Information");
                    DateTime currentDateTime = DateTime.Now;//Will return date/time in UTC
                    if (currentDateTime.TimeOfDay.Hours == 10)
                    {
                        TriggerMyFunction();
                    }
                }
    

    Hope this helps.

                    
    Tuesday, July 24, 2012 1:59 PM
  • Thanks and that's how I have the function written in the worker role. Now Could you please tell me how can I read the variable stored in the serviceconfig file inside workerrole. some code samples will be very helpful.
    Tuesday, July 24, 2012 3:09 PM
  • To read the variable stored in the config file:

    RoleEnvironment.GetConfigurationSettingValue("<your setting name>");
    

    Hope this helps.

    Tuesday, July 24, 2012 3:12 PM
  • thanks Gaurav. how does the settings look like. I know for web.app it will be like 

    <appsettings>

    key and value here... 

    </appsetting>

    Tuesday, July 24, 2012 3:19 PM
  • So if you open up your cscfg file in Visual Studio (settings for your Azure role are defined in this file), you will be able to see the settings for your role. Also there is a GUI based editor in VS for your config file. Just double click on your role name and then go to settings tab.

    For example, this is how it looks for one of my projects:

      <Role name="WorkerRole1">
        <Instances count="1" />
        <ConfigurationSettings>
          <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
          <Setting name="xyz" value="123" />
        </ConfigurationSettings>
      </Role>
    

    Hope this helps.

    • Marked as answer by CRM elite Tuesday, July 24, 2012 3:50 PM
    Tuesday, July 24, 2012 3:23 PM
  • Hi Gaurav, do you happened to know if the server time will be the UTC time? I need to know so that I can set the time accordingly per the timezone that the user is in.  ie. say if the function has to fire at 10 AM Pacific ( i need to put equivalent UTC time or whatever time format that azure VM follows in there) 
    Tuesday, July 24, 2012 3:34 PM
  • That's correct. In Windows Azure the server time is always in UTC. The reason being Windows Azure data centers are spread all over the world (4 in US, 2 in Europe and Asia each) and you could deploy your application anywhere in these 8 data centers and it would be a total mess if each data center uses their local time. I would recommend that you base your application logic on UTC.

    Hope this helps.

    Tuesday, July 24, 2012 3:40 PM
  • Hi Gaurav, would I even need a Thread.Sleep(1000) here. since the program is set to fire at a specified time, i can have the program in a loop without having to put to sleep. correct? 

    If for reason, the specified time is missed during the sleep. 

    • Edited by CRM elite Tuesday, July 24, 2012 5:26 PM
    Tuesday, July 24, 2012 5:26 PM
  • Yes, you do. The reason being if don't put a sleep, your loop will end up consuming 100% resources on your VM. Regarding "missing the specified time", you would need to allow some margin i.e. your process should start anywhere between 10:00:00 AM - 10:01:00 AM (in that 60 seconds period). Since you're putting your loop to sleep for 1 second, I'm sure you will get ample opportunity to execute your process if you provide some margin.

    Hope this helps.

    Wednesday, July 25, 2012 1:40 AM
  • Hi Gaurav, i noticed that using DateTime.TimeOfDay.Hours with 1 minute sleep time, cause the program to execute every minute within that 1 hour period.  I can increase the sleep time to say 60 minutes or few minutes less say 58 minute. 

    is there a performance overhead if the program fires every one minute.? 

    now i am checking at the minute level ie, if hour == say 10 and minute ==1, then I have the program trigger the function. <if I set the sleep time here to say 1 hour, it may miss the 1st minute)? 

    or is it possible for the program to sleep every day until a specified period of time. say 10 AM every day? 

    also note this is a not a set time (this can be modified at the database)  

    Sunday, July 29, 2012 3:30 PM
  • is there a performance overhead if the program fires every one minute.?

    I don't believe so as far as your worker role is concerned if it is not doing anything else.

    or is it possible for the program to sleep every day until a specified period of time. say 10 AM every day?

    It is certainly possible however it all depends on your task. Does it need to fire everyday at eactly 10:00 AM or it can be fired once a day between 10:00 AM and say 11:00 AM. Also are you executing this task on the main thread or on a separate thread (using Task Parallel Library or something)? To be honest, I have not done such work but I am thinking (and this is thinking out loud) is if your task is a blocking task and let's say it takes 5 minutes to consume, it may introduce some skewness. So the first time the task executes say at exactly 10:00 AM and let's say it takes 5 minutes to finish. In a blocking thread scenario, your code will execute the Sleep command at 10:05 AM. If you make it sleep for say 1 day, next time the code next to it will excute at 10:05 AM and the skewness will increase everyday.

    Hope this helps.

    Sunday, July 29, 2012 3:45 PM