【发布时间】:2014-06-27 07:53:16
【问题描述】:
编辑:问题与 Timer 或 HttpServer 无关,它是 dart.io 睡眠功能暂停一切。文档中清楚地描述了它,我的错。
//
我在 HttpClient 在服务器代码中工作时遇到了奇怪的问题。我打电话给
client.getUrl(Uri.parse(url)).then((HttpClientRequest response) => response.close()).then(HttpBodyHandler.processResponse).then((HttpClientResponseBody body) {
print(body.response.statusCode);
来自 Timer 对象,它永远不会到达打印步骤。 它几乎是以前版本的复制和粘贴代码,不是从 Timer 调用而是从 HttpRequest 调用的。工作代码在我的问题 [这里][1] 中。 从长远来看它失败了,我怀疑这是它永远无法到达的最后一个 Future (HttpClientResponseBody)。
Timer 对象是这样创建的(只是测试代码):
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");
//编辑: 好的,这是源代码,可能是一些琐碎的问题..我两天都想不通:-D
import 'dart:async';
import 'dart:io';
import 'package:http_server/http_server.dart';
import 'package:slack/slack_io.dart' as 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 to 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');
for (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 to Slack");
sb.clear();
}
【问题讨论】:
-
您的代码片段似乎有点脱节。你能创建一个允许重现问题的最小代码示例吗?你从
Timer写getUrl不起作用,但是你从Timer调用hit()。我假设您忘记了一些return或类似的简单疏忽,但很难从您的代码中分辨出来。 -
更新后的代码要好得多,但您应该将其精简到允许重现问题的最低限度。您认为
handleMap和send2Slack与您的问题有关吗?sleep的定义在哪里。您确实应该尝试减少代码,以便留下重现问题所需的最少代码量。大多数情况下,仅此一项就可以揭示问题的原因。 -
好的,正在处理它,似乎睡眠冻结了所有进程,并导致中间的请求冻结:)
-
您能否将
sleep()的实现添加到您的问题中?sleep似乎很奇怪。我认为你尝试了一些在 Dart 中效果不佳的东西。 Dart 本身不提供类似sleep的东西,因为它不适合异步编程模型。我想你需要另一种方法。如果我可以看看你的代码,我可能会提出建议。
标签: dart server-side dart-async