From 6aa8d1d2678156c80bb26358cd3fdd2ed7c4cada Mon Sep 17 00:00:00 2001 From: Mehrdad Niknami <> Date: Mon, 26 Aug 2019 02:41:49 -0700 Subject: [PATCH] Periodic builds for the server --- bin/server.dart | 15 +++++++++++++++ lib/build.dart | 28 ++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/bin/server.dart b/bin/server.dart index f947f0c..281a6e0 100644 --- a/bin/server.dart +++ b/bin/server.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:io'; import 'package:args/args.dart'; @@ -25,6 +26,10 @@ main(List args) async { var server = await HttpServer.bind('0.0.0.0', config.port); + print('Scheduling periodic builds...'); + var now = new DateTime.now(); + new Timer(new DateTime(now.year, now.month, now.day + 1 /* next day so that we don't mistakenly schedule an event for earlier today */, 3, 0, 0).difference(now), () => scheduleBuilds(new Duration(days: 1))); + await for (HttpRequest request in server) { try { handle(request); @@ -34,6 +39,16 @@ main(List args) async { } } +scheduleBuilds(Duration period) { + new Timer.periodic(period, (Timer t) { + try { + build.queueDefaultDeployBuild(); + } on Exception catch (e) { + print(e); + } + }); +} + handle(HttpRequest request) { String host = request.headers.host; if (host == config.buildDomain) { diff --git a/lib/build.dart b/lib/build.dart index 62e54f9..409366c 100644 --- a/lib/build.dart +++ b/lib/build.dart @@ -60,22 +60,30 @@ run(QueuedBuild build) async { log = file.openWrite(); } String logUrl = build.url == null ? null : build.url + '/.log'; - github.updateStatus(build.ref, 'pending', 'Build in progress', logUrl); + if (build.ref != null) { + github.updateStatus(build.ref, 'pending', 'Build in progress', logUrl); + } for (var command in commands) { if (await repoShell(command, log) != 0) { print('Build failed!'); log?.close(); - github.updateStatus(build.ref, 'failure', 'Failed to build', logUrl); + if (build.ref) { + github.updateStatus(build.ref, 'failure', 'Failed to build', logUrl); + } return; } } print('Build successful!'); log?.close(); - github.updateStatus(build.ref, 'success', 'Build successful!', build.url); + if (build.ref) { + github.updateStatus(build.ref, 'success', 'Build successful!', build.url); + } if (build.pr != null) { github.makeBuildComment(build.pr, build.url, build.url2); } else { - github.postToSlack(build.ref); + if (build.ref) { + github.postToSlack(build.ref); + } } } @@ -97,6 +105,15 @@ queueBuild(QueuedBuild build) { queue.add(build); } +queueDeployBuild(String ref) { + queueBuild(new QueuedBuild( + config.deployBranch, config.deployDirectory, ref)); +} + +queueDefaultDeployBuild() { + queueDeployBuild(null); +} + repoShell(List cmdArgs, IOSink log) async { bool catchError = false; var cmd = cmdArgs.join(" "); @@ -158,8 +175,7 @@ handleEvent(String event, String contents) { data['ref'] == 'refs/heads/${config.deployBranch}' && data['repository']['full_name'] == config.githubRepo) { print('Queueing deploy...'); - queueBuild(new QueuedBuild( - config.deployBranch, config.deployDirectory, data['after'])); + queueDeployBuild(data['after']); } else if (event == 'issue_comment' && data.containsKey('pull_request') && data['action'] == 'created' &&