Skip to content

Commit

Permalink
Periodic builds for the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehrdad Niknami committed Aug 26, 2019
1 parent 0849920 commit 6aa8d1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
15 changes: 15 additions & 0 deletions bin/server.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';

import 'package:args/args.dart';
Expand Down Expand Up @@ -25,6 +26,10 @@ main(List<String> 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);
Expand All @@ -34,6 +39,16 @@ main(List<String> 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) {
Expand Down
28 changes: 22 additions & 6 deletions lib/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand All @@ -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<String> cmdArgs, IOSink log) async {
bool catchError = false;
var cmd = cmdArgs.join(" ");
Expand Down Expand Up @@ -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' &&
Expand Down

0 comments on commit 6aa8d1d

Please sign in to comment.