I have to fire off ad beacons from an SG app at specific points in a stream. In practice this means I can have over a dozen HTTP calls at once. I do not care about the responses.
Right now I have a task to handle the HTTP requests, and I create a new task for each request.
However I ran into a problem that plagued me in SDK1, where the task is garbage collected before the HTTP request is made.
To get around this I append the tasks to a list, then every now and then trim the list of completed requests. This feels like a kludge. Is there a best practice for spinning up a task and keeping it around until it completes, and only then allowing it to be garbage collected?
Here's the task code:
url = m.top.url
? "[HTTP Request] ", url
request = createobject("roUrlTransfer")
port = createobject("roMessagePort")
msg = wait(5000, port)
if (type(msg) = "roUrlEvent")
if (msg.getresponsecode() > 0 OR msg.getresponsecode() < 400)
? "[HTTP Request] SUCCESS. ",msg.getresponsecode()
msgfailureresponse = msg.getstring()
if msg.getresponsecode() <> invalid
msgcode = msg.getresponsecode()
if msg.getfailurereason() <> invalid
msgfailurereason = msg.getfailurereason()
? "[HTTP Request] REQUEST FAILED: ",url
? "[HTTP Request] CODE: ",msgcode, "REASON: ",msgfailurereason
else if (msg = invalid)
? "[HTTP Request] BEACON REQUEST FAILED: ",url
Here's my first stab at the code that uses it:
Sub fireBeacon(url as String)
? "Fire Beacon. ", url
task = CreateObject("roSGNode", "http_request")
task.url = url
task.control = "RUN"
'keep task from getting garbage collected
? "m.http_tasks 1: ",m.http_tasks.count()
for each t in m.http_tasks
? "m.http_tasks 2: ",m.http_tasks.count()