Skip to content

Commit

Permalink
fix: refactor writing to request body
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Oct 30, 2023
1 parent 5b34f9e commit dbab393
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 50 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docker.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
retry_wait_seconds: 2
warning_on_retry: false
command: |
docker-compose ${{ env.COMPOSE_ARGS }} logs api
docker-compose ${{ env.COMPOSE_ARGS }} logs wdqs-updater
line_count=$(docker-compose ${{ env.COMPOSE_ARGS }} logs wdqs-updater | grep "org.wikidata.query.rdf.tool.Updater - Polled" | wc -l)
echo "line count: $line_count"
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ services:
default:
aliases:
- api.svc
depends_on:
- wikibase
environment:
- API_WIKIBASE_DOMAIN=localhost
- API_WDQS_BACKEND=localhost:9999
Expand Down Expand Up @@ -76,6 +78,7 @@ services:
default:
aliases:
- mysql.svc

volumes:
query-service-data:
mediawiki-mysql-data:
85 changes: 47 additions & 38 deletions seeder/server.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
var http = require('http');
const wbEdit = require('wikibase-edit')(require('./wikibase-edit.config'));

http.createServer(async function (req, res) {
switch (req.url) {
case '/getBatches':
numEntities = 20;
entities = [];
http.createServer(function (req, res) {
console.log('Routing request for %s', req.url);
(async () => {
switch (req.url) {
case '/getBatches':
numEntities = 20;
entities = [];

for (var i=1; i <= numEntities; ++i) {
const { entity } = await wbEdit.entity.create({
type: 'item',
labels: {
'en': new Date().toISOString()
},
descriptions: {
'en': new Date().toDateString() + new Date().toISOString()
}
});
for (var i = 1; i <= numEntities; ++i) {
const { entity } = await wbEdit.entity.create({
type: 'item',
labels: {
'en': new Date().toISOString()
},
descriptions: {
'en': new Date().toDateString() + new Date().toISOString()
}
});

console.log('created item id', entity.id)
entities.push(entity.id)
}
console.log('created item id', entity.id)
entities.push(entity.id)
}

console.log(new Date().toISOString());
console.log(new Date().toISOString());

responseObject = {
'entityIds': entities.join(','),
'wiki': {
'domain': process.env.API_WIKIBASE_DOMAIN,
'wiki_queryservice_namespace': {
'backend': process.env.API_WDQS_BACKEND,
'namespace': 'wdq'
}
},
responseObject = {
'entityIds': entities.join(','),
'wiki': {
'domain': process.env.API_WIKIBASE_DOMAIN,
'wiki_queryservice_namespace': {
'backend': process.env.API_WDQS_BACKEND,
'namespace': 'wdq'
}
},

};
res.end(JSON.stringify([responseObject]));
case '/markFailed':
case '/markDone':
res.writeHead(200);
res.end('1');
default:
res.writeHead(404);
res.end('Not found');
}
};
console.log('handled getBatches');
res.end(JSON.stringify([responseObject]));
case '/markFailed':
case '/markDone':
console.log('handled mark');
res.writeHead(200);
res.end('1');
default:
res.writeHead(404);
res.end('Not found');
}
})()
.catch((err) => {
res.writeHead(500);
res.end(err.toString());
})
}).listen(3030);
25 changes: 13 additions & 12 deletions src/main/java/org/wikidata/query/rdf/tool/WbStackUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.lang.management.ManagementFactory;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -202,29 +201,31 @@ private static JsonArray getBatchesFromApi() throws IOException {
}

private static void updateRemoteBatchStatus(int batchId, boolean success) throws IOException {
JsonObject body = new JsonObject();
JsonArray batches = new JsonArray();
batches.add(batchId);
body.add("batches", batches);

URL obj = new URL(success ? wbStackApiEndpointMarkDone : wbStackApiEndpointMarkFailed);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);

JsonObject body = new JsonObject();
JsonArray batches = new JsonArray();
batches.add(batchId);
body.add("batches", batches);

OutputStream os = con.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write(body.toString());
osw.flush();
osw.close();
os.close();
con.connect();
try (OutputStream os = con.getOutputStream()) {
byte[] input = body.toString().getBytes("utf-8");
os.write(input, 0, input.length);
} catch (Exception ex) {
throw new IOException("Failed writing to request body: " + ex.toString());
}

int responseCode = con.getResponseCode();
if (responseCode != 200) {
throw new IOException("Got non 200 response code from API: " + responseCode);
}
con.disconnect();
}

private static void updateBatch(JsonElement batchElement) {
Expand Down

0 comments on commit dbab393

Please sign in to comment.