Hello,
My apologies that terminal session is gone.
Here is what I am attempting. Here is some code that I would like to see run
indefinitely.
I would think that while I have it running and I turn off my network that at
some point I would see an Error raised. I almost never do.
My naive understanding of what I wrote is that if any Errors would work up to
my code then it could loop correctly and checking the connection and eventually
when the network is restored continue getting Google.
I am not understanding why this isn't working and why I am not seeing the
Errors.
Maybe this is enough information that you who are far, far more knowledgeable
than I can see the error in my ways and put me on the right path. Wisdom and
knowledge gratefully accepted.
Thanks.
import httpclient, httpcore, net, times, strutils
from os import sleep
let
resetDuration = initDuration(seconds=2)
deciSecondDuration* = initDuration(milliseconds = 100)
qtrsecondDuration* = initDuration(milliseconds = 250)
var
client = newHttpClient()
lastConnection = getTime().utc
proc resetHttpClient() =
if (getTime().utc - lastConnection) > resetDuration:
# Probably a new timeout. We have not yet experienced a long outage.
# We may however be entering an extended outage.
# Creating the new clients seems to use up lots of CPU.
# I want to do that as little as possible.
try:
client.close()
except:
echo("Attempted to close clients. Probably do not exist.")
echo("Current exception: ", getCurrentExceptionMsg())
client = newHttpClient(timeout=500)
proc getGoogle() =
resetHttpClient()
var dt = getTime().utc
let enddate = dt + initDuration(days = 1)
try:
while dt <= enddate:
echo(dt)
echo(client.get("http://www.google.com").body[0..14], " ",
getTime().utc)
let diff = getTime().utc - dt
if diff < deciSecondDuration:
sleep(diff.milliseconds.int)
dt = getTime().utc
except TimeoutError, IOError, OSError:
# I naively think I would see this thrown or the plain except below.
# But I almost never see an Error raised.
echo("Current Exception: ", getCurrentException().name)
echo("Current Exception Msg: ", getCurrentExceptionMsg())
echo("Sleeping for 1 seconds at: ", getTime().utc)
sleep(1000)
resetHttpClient()
except:
echo("Current Exception: ", getCurrentException().name)
echo("Current Exception Msg: ", getCurrentExceptionMsg())
echo("Sleeping for 1 seconds at: ", getTime().utc)
when isMainModule:
echo("Executing network_test")
getGoogle()
Run
If I turn of my network and then Ctrl-C after a few seconds I get this:
\--(Is there a style code for something like this terminal session output?)
<!doctype html> 2018-11-30T22:35:27Z 2018-11-30T22:35:27Z ^CTraceback (most
recent call last) proxyexe.nim(62) proxyexe proxyexe.nim(49) main
osproc.nim(1136) waitForExit SIGINT: Interrupted by Ctrl-C. Traceback (most
recent call last) network_test.nim(48) network_test network_test.nim(33)
getGoogle httpclient.nim(1235) get httpclient.nim(1227) request
httpclient.nim(1204) request httpclient.nim(1189) requestAux
httpclient.nim(1024) parseResponse net.nim(1312) recvLine net.nim(1272)
readLine net.nim(1069) recv net.nim(1056) readIntoBuf net.nim(1052) uniRecv
SIGINT: Interrupted by Ctrl-C. Error: execution of an external program failed:
'/home/jimmie/Dev/Nim/network_test/network_test '
After almost 16 minutes I get this output.
2018-11-30T22:44:21Z <!doctype html> 2018-11-30T22:44:21Z 2018-11-30T22:44:21Z
Current Exception: OSError Current Exception Msg: Invalid argument Sleeping for
1 seconds at: 2018-11-30T23:00:06Z