Skip to content

Commit a16b265

Browse files
committed
Initial push of sandbox creation endpoint
1 parent 846c13e commit a16b265

File tree

5 files changed

+40
-21
lines changed

5 files changed

+40
-21
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Sandbox creation endpoint for use with tutorials
2+
To use locally, zip entire directory and install service on system database.
3+
4+
Currently, endpoint is accessible via https://a0434a558688.arangodb.cloud:8529/_db/_system/tutorialDB/tutorialDB
5+
6+
**/tutorialDB**
7+
* Returns dbName, username, password, hostname address
8+
* Creates tutorialInstances collection that has all existing username and database information.
9+
* Deletes databases every 4 hours, checks once an hour.
10+
* When a database is dropped some information, including email is transferred to expiredtutorialInstances collection.

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const users = require("@arangodb/users");
66
const router = createRouter();
77
const joi = require('joi');
88

9-
const aisisInstances = "aisisInstances"
9+
const collectionName = db._collection("tutorialInstances");
1010

1111
module.context.use(router);
1212

13-
router.post('/createDB', function (req,res) {
13+
router.post('/tutorialDB', function (req,res) {
1414
const data = req.body;
1515
const dbName = data.dbName ? data.dbName : randomStringGenerator();
1616
const username = data.username ? data.username : randomStringGenerator();
@@ -51,7 +51,7 @@ router.post('/createDB', function (req,res) {
5151
"port": ${port},
5252
"email": ${email},
5353
"timestamp": DATE_NOW()
54-
} INTO aisisInstances`
54+
} INTO ${collectionName}`;
5555

5656
res.send({dbName, username, password, hostname, port});
5757
}
@@ -63,5 +63,5 @@ router.post('/createDB', function (req,res) {
6363

6464
function randomStringGenerator() {
6565
// Database name must start with letter.
66-
return "ML" + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
66+
return "TUT" + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
6767
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"engines": {
3-
"arangodb": "^3.5.0"
3+
"arangodb": "^3.6.0"
44
},
55
"main": "index.js",
66
"scripts": {

scripts/expire.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,27 @@
22
const { aql, query, db } = require("@arangodb");
33
const users = require("@arangodb/users");
44

5-
const expirationTime = (336 * 60 * 60 * 1000)
6-
const expired = []
5+
const expirationTime = (4 * 60 * 60 * 1000) // 4 hours
6+
const expired = [];
77

88
let dbs = query`
9-
FOR i IN aisisInstances
10-
RETURN {key: i._key, dbName: i.dbName, timestamp: i.timestamp}`
9+
FOR i IN tutorialInstances
10+
RETURN {key: i._key, dbName: i.dbName, timestamp: i.timestamp, username: i.username}`;
1111

1212
dbs.toArray().map((d) => {
13-
(Date.now() - expirationTime ) > d.timestamp ? removeDatabase(d.dbName, d.key) : ''
13+
(Date.now() - expirationTime ) > d.timestamp ? removeDatabase(d.dbName, d.key, d.username) : ''
1414
})
1515

16-
query`
16+
let cleanupCollection = query`
1717
FOR key IN ${expired}
18-
REMOVE { _key: key } IN aisisInstances
19-
`
18+
FOR i IN tutorialInstances
19+
FILTER i._key
20+
INSERT {email: i.email, username: i.username, dbName: i.dbName} INTO expiredtutorialInstances
21+
REMOVE { _key: i._key } IN tutorialInstances`;
2022

21-
function removeDatabase(dbName, key) {
22-
console.log(db._engineStats())
23-
db._dropDatabase(dbName)
24-
expired.push(key)
23+
function removeDatabase(dbName, key, username) {
24+
console.log(db._engineStats());
25+
users.remove(username);
26+
db._dropDatabase(dbName);
27+
expired.push(key);
2528
}

scripts/setup.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
'use strict';
22
const db = require('@arangodb').db;
3-
const collectionName = 'aisisInstances';
3+
const collectionName = 'tutorialInstances';
4+
const expiredCollectionName = "expired"+collectionName;
45

6+
// Creates collection to hold current user and instance information, to be deleted
57
if (!db._collection(collectionName)) {
68
db._createDocumentCollection(collectionName);
79
}
10+
// Creates collection to hold expired documents
11+
if (!db._collection(expiredCollectionName)) {
12+
db._createDocumentCollection(expiredCollectionName);
13+
}
814
const queues = require('@arangodb/foxx/queues');
9-
const queue = queues.create('expirationQueue');
15+
const queue = queues.create('tutorialExpirationQueue');
1016

1117
// Creates the expire job to check if databases should be expired and dropped.
1218
queue.push(
13-
{mount: '/createDB', name: 'expire'},
19+
{mount: '/tutorialDB', name: 'expire'},
1420
{},
1521
{
1622
repeatTimes: Infinity,
1723
repeatUntil: -1, // Forever
18-
repeatDelay: (24 * 60 * 60 * 1000) // Daily
24+
repeatDelay: (60 * 60 * 1000) // Every hour
1925
}
2026
);

0 commit comments

Comments
 (0)