rokudev / SceneGraphDeveloperExtensions

Other
115 stars 66 forks source link

Inside a Task node, When API call's Roku Device Restart's Automatically #50

Closed chaklasiyanikunj closed 4 years ago

chaklasiyanikunj commented 4 years ago

I tried to API call using below logic inside a Task node.

I test using the Postman tool My File Response takes around 800ms. I applied different below logic and the Results inside a Task node For fetch response from API.

'First Logic

url = "http://MyAPIHostSmiley Tongueort/API.json"
rxfer.SetUrl(url)
m.raw = rxfer.GetToString()
json = ParseJSON(m.raw)

'When I performed the First logic. Results are below :

When the Application launch :

  1. First Time Device Restarted
  2. Second Time It's Give Execution Timeout error
  3. Third Time It's working

'Second Logic

url = "http://MyAPIHostSmiley Tongueort/API.json"
rxfer.SetUrl(url)
m.port = CreateObject("roMessagePort")
rxfer.SetPort(m.port)
rxfer.AsyncGetToString()
m.msg = wait(3000,m.port)
if m.msg = invalid then 
   rxfer.AsyncCancel()
else
   m.raw = m.msg.GetString()
   ?"m.raw : "m.raw
end if 
json = ParseJSON(m.raw)

'When I performed the Second logic. Results are below :

When the Application launch :

  1. First Time Device Restarted
  2. Second Time Device Restarted
  3. Third Time It's Give Execution Timeout error
  4. Fourth Time It's Give Execution Timeout error
  5. Fifth Time It's working

Third Logic (Using Internal File):

m.raw = ReadASCIIFile("pkg:/api/API.json")
json = ParseJSON(m.raw)

'When I performed the Third logic. Results are below :

When the Application launch :

  1. First Time It's Working

I don't understand one thing. It's giving a Execution timeout error. It's ok but why Roku Device restart's the Automatic. Any Solution available for this thing?

RokuChris commented 4 years ago

Execution timeouts are often due to a rendezvous timing out. You want to do whatever you can to minimize the number of rendezvous your code is making. There's more information about that here: https://developer.roku.com/docs/developer-program/core-concepts/threads.md#task-node-thread-rendezvous-timeout

Another good practice is to avoid unnecessary use of the m variable within a ContentHandler. Use local variables whenever possible.

chaklasiyanikunj commented 4 years ago

Thank you for your great answer and suggestion.

chaklasiyanikunj commented 4 years ago

I removed all global variables in the Task node. Roku Device No More Restart. But, I have created one JSON file in the server. Inside a JSON File around 32000 lines. The file size is 800KB. I check with this API using the postman tool. With the Local Server My Response times around 120ms. And With Global Server(It means Region Difference) My Response times around 1100ms. Now, I used Below Function in Roku.

sub FetchContent()
    rxfer = CreateObject("roUrlTransfer")
    m.tempArray = CreateObject("roArray", 0, true)
    url = "MyAPIURL" 
    ?"url : "url
    rxfer.SetUrl(url)
    ?"raw Start"
    raw = rxfer.GetToString()     'This lines It's take too much time
    ?"raw End"
    json = ParseJson(raw)
        rows = {
            children: []
        }
        for each row in json
            rowNode = {}
            if row.title <> invalid and row.title <> ""
                rowNode.title = row.title
            else
                rowNode.title = "---"
            end if
            rows.children.Push(rowNode)
            m.tempArray.Push(rowNode)
        end for
        m.global.rowNode = m.tempArray
end sub

My Internet Speed is around 250mbps. When I Application launched these lines raw = rxfer.GetToString() takes to Average 5-10 mins Every times. Why does it take so long? Anything I'm doing wrong? and Currently, I am not set 0 in port. But how much times take when I set 0 in port like below. msg = Wait(0, m.port)