botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Bot unloaded from memory after a certain time of non-use

Open KIBASSA opened this issue 9 years ago • 6 comments

Hi,

I have a rather permanent problem which I manage not to solve.

I the impression that my dll unloaded from the memory after a while. For example, when I do not use the bot during 30 minutes and when I reuse it, that takes time before reacting like this I came to re-deployed.

It is as if the dll was no longer in the RAM and it must to recharges before response.

Is there a manipulation to be done to prevent the BOT from being unloaded from the memory after a certain time of non-use?

(Sorry for my not very clear English)

Thank you in advance

Junior

KIBASSA avatar Feb 28 '17 12:02 KIBASSA

I think this has nothing to do with the bot, but with the option where and how you host it.

If you host it as App Service on Azure there is an option "Always On" to solve your problem. But this option is only available in the Basic and Standard pricing Tier. In the shared tier

Theoretically it should be possible to write a script/bot/something else that sends a request every now and then to your Bot and therefore always keeps it alive/hot.

ThisWillDoIt avatar Feb 28 '17 12:02 ThisWillDoIt

Thank you for your return. In fact, I also thought of making a script which is going to throw requests all one minute for example to keep the Bot alive. I wish to use WebClient of .Net who is going to make regularly requests POST on ApiController but I do not know how to do it. Have you an example of an end of code c# which makes a request POST on ApiController in passing also MicrosoftAppId and MicrosoftAppPassword as Credentials?

I do not have to find of example on the Internet.

Thank you

KIBASSA avatar Feb 28 '17 19:02 KIBASSA

@KIBASSA refer this link https://docs.botframework.com/en-us/technical-faq/ read question in given link. "My bot is slow to respond to the first message it receives. How can I make it faster?"

aakashkag avatar Mar 01 '17 08:03 aakashkag

Thank you for the link. At the moment, I cannot pass on the paying offer to pass in mode 'Always on'. I am always in mode development. I followed recommend them of @ThisWillDoIt to create an automatic script finally to keep Bot alive. This is what I made:

` var authValue = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{MyMicrosoftAppId}:{MyMicrosoftAppPassword}")));

       var client = new HttpClient()
        {
            DefaultRequestHeaders = { Authorization = authValue }

        };

        var jsonObject = new Rootobject()
        {
          type = "message",
          id = "0a24ca1428074419a1679b37e0e3dd39",
          timestamp = DateTime.Now,
          serviceUrl = "http://localhost:9000/",
          channelId = "emulator",
          from = new From()
          {
              id = "2c1c7fa3",
              name = "User1"
          },
          conversation = new Conversation()
          {
              isGroup = false,
              id = "8a684db8",
              name = "Conv1"
          },
          recipient =   new Recipient()
          {
              id = "56800324",
              name = "Bot1"
          },
          text = "ping",
          attachments = new object[0],
          entities = new object[0]
        };

        var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
        var result = client.PostAsync("http://emiko.azurewebsites.net/api/messages", content).Result;
        result.ToString();

` But I get an authorization error: StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent...

How can I login correctly on the Bot and send a message on its Url (http://emiko.azurewebsites.net/api/messages)

thanks for the help

KIBASSA avatar Mar 01 '17 12:03 KIBASSA

This issue is more related to Azure instances and not an issue related to SDK. You can make Azure instances to be always on and this behavior will go away. Also you cannot generate the tokens sent by channels to emulate behavior of a message sent by channel as keep alive. You can look at the emulator token generation and use emulator tokens to send a keep alive to your bot. Also instead of message activity use ping activity for this since you only want to poke your web app with this.

Another less complicated approach is exposing a keep-alive endpoint (this can be any arbitrary route) in your web app and have a scheduled job that do a fetch from that endpoint (you can implement your own access control for this endpoint).

msft-shahins avatar Mar 10 '17 20:03 msft-shahins

Just a quick note on Azure's 'always-on' setting: in my experience, this has had absolutely no effect on web app/api slow startup times. In my 'always-on' app, I consistently experience behaviour consistent with app/dll unloading after a period of non-use. I'd recommend going with the ping approach.

oflahero avatar Jan 05 '18 10:01 oflahero