Skip to content

Commit a0f6d26

Browse files
committed
use command line argument to run daily cron tasks
1 parent 4084453 commit a0f6d26

File tree

4 files changed

+45
-42
lines changed

4 files changed

+45
-42
lines changed

dub.sdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ copyright "Copyright © 2015, Martin Nowak"
44
authors "Martin Nowak"
55
dependency "vibe-d" version="~>0.7.30"
66
configuration "default" {
7-
versions "VibeDefaultMain"
7+
versions "VibeCustomMain"
88
targetType "executable"
99
}
1010
configuration "unittest" {

source/dlangbot/app.d

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ public import dlangbot.bugzilla : bugzillaURL;
77
public import dlangbot.github : githubAPIURL, githubAuth, hookSecret;
88
public import dlangbot.trello : trelloAPIURL, trelloAuth, trelloSecret;
99

10-
string cronDailySecret;
11-
1210
import std.datetime : Clock, days, Duration, minutes, seconds, SysTime;
1311

12+
import vibe.core.args;
13+
import vibe.core.core;
1414
import vibe.core.log;
1515
import vibe.data.json;
1616
import vibe.http.client : HTTPClient;
@@ -29,32 +29,6 @@ Duration prInactivityDur = 90.days; // PRs with no activity within X days will g
2929

3030
enum trelloHookURL = "https://dlang-bot.herokuapp.com/trello_hook";
3131

32-
version(unittest){} else
33-
shared static this()
34-
{
35-
import std.process : environment;
36-
import vibe.core.args : readOption;
37-
38-
auto settings = new HTTPServerSettings;
39-
settings.port = 8080;
40-
readOption("port|p", &settings.port, "Sets the port used for serving.");
41-
startServer(settings);
42-
43-
githubAuth = "token "~environment["GH_TOKEN"];
44-
trelloSecret = environment["TRELLO_SECRET"];
45-
trelloAuth = "key="~environment["TRELLO_KEY"]~"&token="~environment["TRELLO_TOKEN"];
46-
hookSecret = environment["GH_HOOK_SECRET"];
47-
cronDailySecret = environment["CRON_DAILY_SECRET"];
48-
49-
// workaround for stupid openssl.conf on Heroku
50-
if (environment.get("DYNO") !is null)
51-
{
52-
HTTPClient.setTLSSetupCallback((ctx) {
53-
ctx.useTrustedCertificateFile("/etc/ssl/certs/ca-certificates.crt");
54-
});
55-
}
56-
}
57-
5832
void startServer(HTTPServerSettings settings)
5933
{
6034
import vibe.core.core : vibeVersionString;
@@ -71,7 +45,6 @@ void startServer(HTTPServerSettings settings)
7145
.post("/github_hook", &githubHook)
7246
.match(HTTPMethod.HEAD, "/trello_hook", (req, res) => res.writeVoidBody)
7347
.post("/trello_hook", &trelloHook)
74-
.get("/cron_daily", &cronDaily)
7548
;
7649

7750
HTTPClient.setUserAgentString("dlang-bot vibe.d/"~vibeVersionString);
@@ -152,19 +125,13 @@ void githubHook(HTTPServerRequest req, HTTPServerResponse res)
152125

153126
//==============================================================================
154127

155-
void cronDaily(HTTPServerRequest req, HTTPServerResponse res)
128+
void cronDaily()
156129
{
157-
enforceBadRequest(req.query.length > 0, "No repo slugs provided");
158-
enforceHTTP(req.query.get("secret") == cronDailySecret,
159-
HTTPStatus.unauthorized, "Invalid or no secret provided");
160-
161-
foreach (ref slug; req.query.getAll("repo"))
130+
foreach (repo; ["dlang/phobos"])
162131
{
163-
logInfo("running cron.daily for: %s", slug);
164-
runTaskHelper(&searchForInactivePrs, slug, prInactivityDur);
132+
logInfo("running cron.daily for: %s", repo);
133+
searchForInactivePrs(repo, prInactivityDur);
165134
}
166-
167-
return res.writeBody("OK");
168135
}
169136

170137
//==============================================================================
@@ -213,3 +180,39 @@ void handlePR(string action, PullRequest* _pr)
213180
if (runTrello)
214181
updateTrelloCard(action, pr.htmlURL, refs, descs);
215182
}
183+
184+
//==============================================================================
185+
186+
version (unittest) {}
187+
else void main(string[] args)
188+
{
189+
import std.process : environment;
190+
import vibe.core.args : readOption;
191+
192+
githubAuth = "token "~environment["GH_TOKEN"];
193+
trelloSecret = environment["TRELLO_SECRET"];
194+
trelloAuth = "key="~environment["TRELLO_KEY"]~"&token="~environment["TRELLO_TOKEN"];
195+
hookSecret = environment["GH_HOOK_SECRET"];
196+
197+
// workaround for stupid openssl.conf on Heroku
198+
if (environment.get("DYNO") !is null)
199+
{
200+
HTTPClient.setTLSSetupCallback((ctx) {
201+
ctx.useTrustedCertificateFile("/etc/ssl/certs/ca-certificates.crt");
202+
});
203+
}
204+
205+
bool runDailyCron;
206+
auto settings = new HTTPServerSettings;
207+
settings.port = 8080;
208+
readOption("port|p", &settings.port, "Sets the port used for serving.");
209+
readOption("cron-daily", &runDailyCron, "Run daily cron tasks.");
210+
if (!finalizeCommandLineOptions())
211+
return;
212+
if (runDailyCron)
213+
return cronDaily();
214+
215+
startServer(settings);
216+
lowerPrivileges();
217+
runEventLoop();
218+
}

test/cronjob.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ unittest
4747
},
4848
);
4949

50-
openUrl("/cron_daily?repo=dlang/phobos&secret=" ~ cronDailySecret, "OK");
50+
import dlangbot.app : cronDaily;
51+
cronDaily();
5152
}

test/utils.d

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ shared static this()
2929
githubAuth = "GH_DUMMY_AUTH_TOKEN";
3030
hookSecret = "GH_DUMMY_HOOK_SECRET";
3131
trelloAuth = "key=01234&token=abcde";
32-
cronDailySecret = "dummyCronSecret";
3332

3433
// start our hook server
3534
auto settings = new HTTPServerSettings;

0 commit comments

Comments
 (0)