Skip to content

Commit ca8bded

Browse files
authored
Merge pull request #44 from RapidAPI/staging
Merge staging to master
2 parents ea2136a + d2bd280 commit ca8bded

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

.github/workflows/master-change.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ jobs:
7676
LOCATION_CONTEXT=Default,
7777
LOCATION_KEY=${{ matrix.region.key }},
7878
LOCATION_SECRET=${{ secrets.LOCATION_SECRET_PROD_DEFAULT }},
79-
WORKER_LOGGING=off
79+
WORKER_LOGGING=off,
80+
POLLING_TIME_MAX=5000,
81+
FREQUENCY=1000
8082
aws_access_key_id: ${{ secrets.AWS_PROD_ACCESS_KEY_ID }}
8183
aws_secret_access_key: ${{ secrets.AWS_PROD_SECRET_ACCESS_KEY }}
8284
aws_region: ${{ matrix.region.name }}

.github/workflows/staging-change.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ jobs:
3131
LOCATION_CONTEXT=Default,
3232
LOCATION_KEY=AWS-STAGING,
3333
LOCATION_SECRET=${{ secrets.LOCATION_SECRET_STAGING_DEFAULT }},
34-
WORKER_LOGGING=cli
34+
WORKER_LOGGING=cli,
35+
POLLING_TIME_MAX=5000,
36+
FREQUENCY=1000
3537
aws_access_key_id: ${{ secrets.AWS_STAGING_ACCESS_KEY_ID }}
3638
aws_secret_access_key: ${{ secrets.AWS_STAGING_SECRET_ACCESS_KEY }}
3739
aws_region: us-east-1

src/RapidRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const fetchRequests = async ({ baseUrl, locationSecret, locationKey, locationCon
1919
let requestsResponse = (
2020
await axios.get(`${baseUrl}/api/location/request?amount=${batchSize}`, {
2121
headers,
22-
timeout: 10000,
22+
timeout: 5000,
2323
})
2424
).data;
2525
requests = requestsResponse["requests"];

src/RapidTest.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,23 @@ async function fetchAndExecuteTests({ baseUrl, locationSecret, locationKey, loca
119119
headers["x-location-context"] = locationContext;
120120
}
121121

122+
if (logging) {
123+
consola.info(`Getting executions....`);
124+
}
125+
122126
let executionsResponse = (
123127
await axios.get(`${baseUrl}/api/location/executable?amount=${batchSize}`, {
124128
headers,
125-
timeout: 10000,
129+
timeout: 5000,
126130
})
127131
).data;
128132

129133
testExecutions = executionsResponse["testExecutions"];
130134

135+
if (logging) {
136+
consola.info(`Fetched ${testExecutions.length} executions`);
137+
}
138+
131139
if (logging) {
132140
if (testExecutions.length > 0) {
133141
consola.info(`Processing executions for ${baseUrl}:\n`);

src/main.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,27 @@ const { program } = require("commander");
1212
const pjson = require("../package.json");
1313

1414
if (require.main === module) {
15-
execute("cli");
15+
// local execution
16+
execute({ Records: [{ body: null }] }, {});
1617
}
1718

18-
async function execute(logLevel = "on") {
19+
// eslint-disable-next-line no-unused-vars
20+
async function execute(event, _context) {
21+
let baseURlFromEvent;
22+
const body = event.Records?.[0]?.body;
23+
if (body) {
24+
baseURlFromEvent = JSON.parse(body)?.baseUrl;
25+
}
1926
const ignoreSSL = "false"; // Fail any "https" tests with missing cert.
27+
2028
program.version(pjson.version);
2129
program
2230
.description("Start worker to execute RapidAPI tests and requests")
2331
.requiredOption(
2432
"-u, --url <baseUrl>",
2533
"The base URL to fetch executions from (env variable: BASE_URL)",
26-
process.env.BASE_URL || process.env.URL || "https://rapidapi.com/testing"
34+
// default workers get baseUrl from SQS event body, otherwise from config
35+
baseURlFromEvent || process.env.BASE_URL || "https://rapidapi.com/testing"
2736
)
2837
.requiredOption(
2938
"-s, --secret <secret>",
@@ -58,7 +67,7 @@ async function execute(logLevel = "on") {
5867
.option(
5968
"-l, --logging [on, off, cli]",
6069
"Logging level. 'cli' prints additional information at startup useful for debugging",
61-
process.env.WORKER_LOGGING || logLevel
70+
process.env.WORKER_LOGGING || "off"
6271
)
6372
.option(
6473
"--ignore-ssl [true, false]",
@@ -97,39 +106,56 @@ async function execute(logLevel = "on") {
97106
const START_TIMESTAMP = Date.now();
98107

99108
let cycle = 1;
100-
// eslint-disable-next-line
101-
if (logging) console.log(`Staring cycle ${cycle++}`);
102-
try {
103-
await executeOnce({ ...global.settings });
104-
} catch (err) {
105-
consola.error(err);
106-
}
107-
if (cmd.frequency) {
109+
if (!cmd.frequency) {
110+
if (logging) consola.log(`Staring cycle ${cycle}`);
111+
try {
112+
await executeOnce({ ...global.settings });
113+
} catch (err) {
114+
consola.error(err);
115+
}
116+
} else {
117+
let unresolvedCycles = 0;
118+
let maxTimeReached = false;
108119
const testLoop = new Promise((resolve) => {
109120
const interval = setInterval(async function () {
110121
if (parseInt(cmd.max)) {
111122
let currentTimestamp = Date.now();
112123
if (currentTimestamp > parseInt(START_TIMESTAMP) + parseInt(cmd.max)) {
124+
if (logging) consola.log(`Max polling time reached`);
113125
clearInterval(interval);
114-
resolve();
126+
maxTimeReached = true;
127+
if (unresolvedCycles <= 0) {
128+
resolve();
129+
}
130+
return;
115131
}
116132
}
117133
if (logging) {
118134
// eslint-disable-next-line
119135
console.log(`Staring cycle ${cycle++}`);
120136
}
121137
try {
138+
unresolvedCycles += 1;
122139
await executeOnce({ ...global.settings });
123140
} catch (err) {
124141
consola.error(err);
142+
} finally {
143+
unresolvedCycles -= 1;
144+
if (maxTimeReached && unresolvedCycles <= 0) {
145+
resolve();
146+
}
125147
}
126148
}, cmd.frequency);
127149
});
128150
await testLoop;
151+
if (logging) {
152+
consola.log(`End execute()`);
153+
}
129154
}
130155
}
131156

132157
async function executeOnce(overwriteDetails = {}) {
158+
// eslint-disable-next-line max-len
133159
return Promise.all([fetchAndExecuteTests(overwriteDetails), fetchAndExecuteRequests(overwriteDetails)]).catch((e) => {
134160
if (global.settings.logging) {
135161
if (e.response) {

0 commit comments

Comments
 (0)