update: new delegate that fits to new infra
This commit is contained in:
parent
7528dcdf81
commit
9d5c7cc47d
@ -4,10 +4,40 @@ import { SlidingWindow } from "mq/slidingWindow.ts";
|
|||||||
import { redis } from "db/redis.ts";
|
import { redis } from "db/redis.ts";
|
||||||
import { ReplyError } from "ioredis";
|
import { ReplyError } from "ioredis";
|
||||||
import { SECOND } from "../const/time.ts";
|
import { SECOND } from "../const/time.ts";
|
||||||
import { execFile } from 'child_process';
|
import { spawn, SpawnOptions } from "child_process";
|
||||||
import { promisify } from 'util';
|
|
||||||
|
|
||||||
const execAsync = promisify(execFile);
|
export function spawnPromise(
|
||||||
|
command: string,
|
||||||
|
args: string[] = [],
|
||||||
|
options?: SpawnOptions,
|
||||||
|
): Promise<{ stdout: string; stderr: string }> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const child = spawn(command, args, options);
|
||||||
|
|
||||||
|
let stdout = "";
|
||||||
|
let stderr = "";
|
||||||
|
|
||||||
|
child.stdout?.on("data", (data) => {
|
||||||
|
stdout += data;
|
||||||
|
});
|
||||||
|
|
||||||
|
child.stderr?.on("data", (data) => {
|
||||||
|
stderr += data;
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on("close", (code) => {
|
||||||
|
if (code !== 0) {
|
||||||
|
reject(new Error(`Error code: ${code}\nstderr: ${stderr}`));
|
||||||
|
} else {
|
||||||
|
resolve({ stdout, stderr });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on("error", (err) => {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
interface Proxy {
|
interface Proxy {
|
||||||
type: string;
|
type: string;
|
||||||
@ -234,7 +264,7 @@ class NetworkDelegate {
|
|||||||
|
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
|
|
||||||
return await response.json() as R;
|
return (await response.json()) as R;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new NetSchedulerError("Fetch error", "FETCH_ERROR", e);
|
throw new NetSchedulerError("Fetch error", "FETCH_ERROR", e);
|
||||||
}
|
}
|
||||||
@ -242,8 +272,7 @@ class NetworkDelegate {
|
|||||||
|
|
||||||
private async alicloudFcRequest<R>(url: string, region: string): Promise<R> {
|
private async alicloudFcRequest<R>(url: string, region: string): Promise<R> {
|
||||||
try {
|
try {
|
||||||
const decoder = new TextDecoder();
|
const output = await spawnPromise("aliyun", [
|
||||||
const output = await execAsync("aliyun", [
|
|
||||||
"fc",
|
"fc",
|
||||||
"POST",
|
"POST",
|
||||||
`/2023-03-30/functions/proxy-${region}/invocations`,
|
`/2023-03-30/functions/proxy-${region}/invocations`,
|
||||||
@ -261,23 +290,25 @@ class NetworkDelegate {
|
|||||||
"10",
|
"10",
|
||||||
"--profile",
|
"--profile",
|
||||||
`CVSA-${region}`,
|
`CVSA-${region}`,
|
||||||
])
|
]);
|
||||||
const out = output.stdout;
|
const out = output.stdout;
|
||||||
const rawData = JSON.parse(out);
|
const rawData = JSON.parse(out);
|
||||||
if (rawData.statusCode !== 200) {
|
if (rawData.statusCode !== 200) {
|
||||||
console.error("STATUS_CODE:", rawData.statusCode);
|
|
||||||
// noinspection ExceptionCaughtLocallyJS
|
// noinspection ExceptionCaughtLocallyJS
|
||||||
throw new NetSchedulerError(
|
throw new NetSchedulerError(
|
||||||
`Error proxying ${url} to ali-fc region ${region}, code: ${rawData.statusCode}.`,
|
`Error proxying ${url} to ali-fc region ${region}, code: ${rawData.statusCode}.`,
|
||||||
"ALICLOUD_PROXY_ERR",
|
"ALICLOUD_PROXY_ERR",
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
return JSON.parse(JSON.parse(rawData.body)) as R;
|
return JSON.parse(rawData.body) as R;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
|
||||||
logger.error(e as Error, "net", "fn:alicloudFcRequest");
|
logger.error(e as Error, "net", "fn:alicloudFcRequest");
|
||||||
throw new NetSchedulerError(`Unhandled error: Cannot proxy ${url} to ali-fc-${region}.`, "ALICLOUD_PROXY_ERR", e);
|
throw new NetSchedulerError(
|
||||||
|
`Unhandled error: Cannot proxy ${url} to ali-fc-${region}.`,
|
||||||
|
"ALICLOUD_PROXY_ERR",
|
||||||
|
e,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -362,7 +393,11 @@ for (const region of regions) {
|
|||||||
}
|
}
|
||||||
networkDelegate.addTask("getVideoInfo", "bilibili", "all");
|
networkDelegate.addTask("getVideoInfo", "bilibili", "all");
|
||||||
networkDelegate.addTask("getLatestVideos", "bilibili", "all");
|
networkDelegate.addTask("getLatestVideos", "bilibili", "all");
|
||||||
networkDelegate.addTask("snapshotMilestoneVideo", "bilibili", regions.map((region) => `alicloud-${region}`));
|
networkDelegate.addTask(
|
||||||
|
"snapshotMilestoneVideo",
|
||||||
|
"bilibili",
|
||||||
|
regions.map((region) => `alicloud-${region}`),
|
||||||
|
);
|
||||||
networkDelegate.addTask("snapshotVideo", "bili_test", [
|
networkDelegate.addTask("snapshotVideo", "bili_test", [
|
||||||
"alicloud-qingdao",
|
"alicloud-qingdao",
|
||||||
"alicloud-shanghai",
|
"alicloud-shanghai",
|
||||||
|
Loading…
Reference in New Issue
Block a user