Dart HttpClient.getUrl invoked by Timer without client or http server -


edit: problem wasn't related timer or httpserver, dart.io sleep function pausing everything. described in documentation, bad.

//

i have weird problem httpclient working in server code. call

client.geturl(uri.parse(url)).then((httpclientrequest response) => response.close()).then(httpbodyhandler.processresponse).then((httpclientresponsebody body) {     print(body.response.statuscode); 

from timer object , never reach print step. copy , paste code previous version, wasn't called timer httprequest. working code in question [here][1]. fails on long line, suspect last future never reach (httpclientresponsebody).

timer object created (just test code):

main() {   t = new timer.periodic(new duration(minutes: period), (timer t) => hit());  }  void hit() {   if (new datetime.now().hour == 17) {     print("syncing rock");      loadurlbody(furl + filter).then((content) {       print("content loaded"); 

//edit: okay, here source, might trivial problem..which can't figure out 2 days :-d

import 'dart:async'; import 'dart:io'; import 'package:http_server/http_server.dart'; import 'package:slack/slack_io.dart' slack;  timer t; bool check; final period = 1; final furl = "https://****.tpondemand.com"; final filter = "somefilter";   main() {   t = new timer.periodic(new duration(minutes: period), (timer t) => hit());  }  void hit() {   if (new datetime.now().hour == 17) {     print("syncing rock");      loadurlbody(furl + filter).then((content) {       print("content loaded");       map parsedmap = content.body;       handlemap(parsedmap);     });     sleep(new duration(minutes: 60));   } else {     print("no time rock " + new datetime.now().tostring());     sleep(new duration(minutes: period * 10));   } }  future loadurlbody(string url) {   final c = new completer();   httpclient client = new httpclient();   client.addcredentials(uri.parse("https://****.tpondemand.com/api"), "tprealm", new httpclientbasiccredentials("user", "password"));   client.geturl(uri.parse(url)).then((httpclientrequest response) => response.close()).then(httpbodyhandler.processresponse).then((httpclientresponsebody body) {     print(body.response.statuscode);     c.complete(body);   });   return c.future; }    void send2slack(string m) {   slack.message message = new slack.message()..text = m;    slack.token = 'token';   slack.team = 'team';   slack.send(message); } void handlemap(map valuemap) {    final duration lostintime = new duration(days: 30);   var sb = new stringbuffer();   sb.write('k o m p o s t \n');    (var item in valuemap["items"]) {     if (item['createdate'] == null) item['createdate'] = '/date(1403167885000+0100)/';     if (item['modifydate'] == null) item['modifydate'] = '/date(1403167885000+0100)/';     if (item['lastcommentdate'] == null) item['lastcommentdate'] = '/date(1403167885000+0100)/';      datetime moonlanding = new datetime.frommillisecondssinceepoch(int.parse(item['createdate'].substring(6, 19)));     datetime modifylanding = new datetime.frommillisecondssinceepoch(int.parse(item['modifydate'].substring(6, 19)));     datetime commentlanding = new datetime.frommillisecondssinceepoch(int.parse(item['lastcommentdate'].substring(6, 19)));     datetime lastchangelanding = (modifylanding.isbefore(commentlanding)) ? commentlanding : modifylanding;     duration difference = new datetime.now().difference(lastchangelanding);      if (moonlanding.add(lostintime).isbefore(new datetime.now()) && difference.indays > 4) {       sb           ..write('<https://****.tpondemand.com/entity/')           ..write(item['id'])           ..write('|')           ..write(item['name'])           ..write('> last change: ')           ..write(difference.indays)           ..write(' days ago \n');      }     ;     }   send2slack(sb.tostring());   print("sent slack");   sb.clear(); } 

i created similar code can't reproduce problem.
work when called timer.

import 'dart:io'; import 'dart:async'; import 'package:http_server/http_server.dart';  timer t; final period = 1;  void main(args) {   t = new timer.periodic(new duration(minutes: period), (timer t) => hit()); }  void hit() {   loadurlbody('http://www.google.com')       .then((httpclientresponsebody b) => print('hit: ${b.response.statuscode}')); }  future loadurlbody(string url) {   print('executing');   httpclient client = new httpclient();   // commented out because have no server can use   // httpclient client = new httpclient()                 //    ..addcredentials(uri.parse("https://****.tpondemand.com/api"), "tprealm", new httpclientbasiccredentials("user", "password"));   return client.geturl(uri.parse(url))        // <== return important here       .then((httpclientrequest response) => response.close())       .then(httpbodyhandler.processresponse)       .then((httpclientresponsebody body) {         print('body: (${new datetime.now()}) ${body.response.statuscode}');         return body;                         // <== value value next 'then' receives.  // example x in: loadurlbody('http://someurl').then(x) => dosomething(x));        }); } 

you don't need use completer. completer more complicated used cases example 1 method returns completer , example eventhandler completes it.

you have ensure return future everywhere. then returns future. value of returned future value returned inside then.


Comments

Popular posts from this blog

double exclamation marks in haskell -

qml - Is it possible to implement SystemTrayIcon functionality in Qt Quick application -

Qt Creator - Searching files with Locator including folder -