Files
firecrawl/apps/api/src/lib/job-priority.ts
T

96 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-08-21 20:54:39 -03:00
import { redisConnection } from "../../src/services/queue-service";
import { PlanType } from "../../src/types";
import { Logger } from "./logger";
2024-08-27 15:26:43 -03:00
const SET_KEY_PREFIX = "limit_team_id:";
2024-08-21 20:54:39 -03:00
export async function addJobPriority(team_id, job_id) {
try {
const setKey = SET_KEY_PREFIX + team_id;
// Add scrape job id to the set
await redisConnection.sadd(setKey, job_id);
2024-08-27 15:26:43 -03:00
2024-08-27 15:06:13 -03:00
// This approach will reset the expiration time to 60 seconds every time a new job is added to the set.
2024-08-27 15:04:10 -03:00
await redisConnection.expire(setKey, 60);
2024-08-21 20:54:39 -03:00
} catch (e) {
Logger.error(`Add job priority (sadd) failed: ${team_id}, ${job_id}`);
}
}
export async function deleteJobPriority(team_id, job_id) {
try {
const setKey = SET_KEY_PREFIX + team_id;
// remove job_id from the set
await redisConnection.srem(setKey, job_id);
} catch (e) {
Logger.error(`Delete job priority (srem) failed: ${team_id}, ${job_id}`);
}
}
export async function getJobPriority({
plan,
team_id,
2024-08-27 15:26:43 -03:00
basePriority = 10,
2024-08-21 20:54:39 -03:00
}: {
plan: PlanType;
team_id: string;
2024-08-27 15:26:43 -03:00
basePriority?: number;
2024-08-21 20:54:39 -03:00
}): Promise<number> {
2024-09-30 19:17:52 +02:00
if (team_id === "d97c4ceb-290b-4957-8432-2b2a02727d95") {
return 50;
}
2024-08-27 16:58:28 -03:00
try {
const setKey = SET_KEY_PREFIX + team_id;
2024-08-21 20:54:39 -03:00
2024-08-27 16:58:28 -03:00
// Get the length of the set
const setLength = await redisConnection.scard(setKey);
2024-08-21 20:54:39 -03:00
2024-08-27 16:58:28 -03:00
// Determine the priority based on the plan and set length
let planModifier = 1;
let bucketLimit = 0;
2024-08-21 20:54:39 -03:00
2024-08-27 16:58:28 -03:00
switch (plan) {
case "free":
bucketLimit = 25;
planModifier = 0.5;
break;
case "hobby":
2024-08-27 17:03:46 -03:00
bucketLimit = 100;
2024-08-27 16:58:28 -03:00
planModifier = 0.3;
break;
case "standard":
case "standardnew":
2024-08-27 17:03:46 -03:00
bucketLimit = 200;
2024-08-27 16:58:28 -03:00
planModifier = 0.2;
break;
case "growth":
case "growthdouble":
2024-08-27 17:03:46 -03:00
bucketLimit = 400;
2024-08-27 17:04:04 -03:00
planModifier = 0.1;
2024-08-27 16:58:28 -03:00
break;
2024-08-27 15:26:43 -03:00
2024-08-27 16:58:28 -03:00
default:
bucketLimit = 25;
planModifier = 1;
break;
}
2024-08-21 20:54:39 -03:00
2024-08-27 16:58:28 -03:00
// if length set is smaller than set, just return base priority
if (setLength <= bucketLimit) {
return basePriority;
} else {
// If not, we keep base priority + planModifier
return Math.ceil(
basePriority + Math.ceil((setLength - bucketLimit) * planModifier)
);
}
} catch (e) {
Logger.error(
`Get job priority failed: ${team_id}, ${plan}, ${basePriority}`
2024-08-21 20:54:39 -03:00
);
2024-08-27 16:58:28 -03:00
return basePriority;
2024-08-21 20:54:39 -03:00
}
}