-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
73 lines (63 loc) · 1.88 KB
/
Copy pathmain.cpp
File metadata and controls
73 lines (63 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <hiredis/hiredis.h>
#include <QCoreApplication>
#include <QElapsedTimer>
#include <QDebug>
#include <QString>
#include <unistd.h>
#include "judge.h"
#include "settings.h"
#include "database.h"
static Settings settings;
static Database db;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if (!settings.readConfig())
{
qDebug() << "Cannot read config file.";
return -1;
}
redisContext *c = redisConnect(settings.getRedisIP().toLatin1().data(), settings.getRedisPort());
int retrytimes = 0;
while ((c == nullptr || c->err) && retrytimes < 10)
{
qDebug("Cannot connect to redis, retrying... (times = %d)", ++retrytimes);
sleep(1);
c = redisConnect(settings.getRedisIP().toLatin1().data(), settings.getRedisPort());
}
if (c == nullptr || c->err)
{
if (c == nullptr)
qDebug("\nCannot allocate redis context.");
else
qDebug("\nError: %s", c->errstr);
qDebug("Please restart the judger.");
return -1;
}
db.setSettings(&settings);
if (!db.init())
{
qDebug("Cannot connect to database.\nPlease restart the judger.");
return -1;
}
qDebug() << "Judger started successfully. Waiting for submitting...";
while (true)
{
redisReply *reply = static_cast<redisReply *>(redisCommand(c, "LPOP intoj-waiting"));
if (reply == nullptr || reply->type == REDIS_REPLY_NIL)
{
usleep(200000);
continue;
}
int runid = QString(reply->str).toInt();
qDebug() << "got submit: " << runid;
judgeThread *judge = new judgeThread();
judge->setDatabase(&db);
judge->setSettings(&settings);
judge->setTask(runid);
judge->run();
delete judge;
QCoreApplication::processEvents();
}
return a.exec();
}