|
1 | | -import { fetchWithRetry } from "@/lib/utils"; |
| 1 | +import { SINGLE_TENANT_ORG_ID, SOURCEBOT_GUEST_USER_ID } from "@/lib/constants"; |
| 2 | +import { isServiceError } from "@/lib/utils"; |
2 | 3 | import { __unsafePrisma } from "@/prisma"; |
3 | | -import { createLogger, env, SOURCEBOT_VERSION, decryptActivationCode } from "@sourcebot/shared"; |
4 | | -import { SINGLE_TENANT_ORG_ID } from "@/lib/constants"; |
5 | | -import { lighthouseResponseSchema } from "./types"; |
| 4 | +import { createLogger, decryptActivationCode, env, SOURCEBOT_VERSION } from "@sourcebot/shared"; |
| 5 | +import { sendServicePing } from "./client"; |
| 6 | +import { ServicePingRequest } from "./types"; |
6 | 7 |
|
7 | 8 | const logger = createLogger('service-ping'); |
8 | 9 |
|
9 | 10 | const SERVICE_PING_INTERVAL_MS = 24 * 60 * 60 * 1000; // 1 day |
10 | 11 |
|
11 | | -export const sendServicePing = async () => { |
| 12 | +export const syncWithLighthouse = async (orgId: number) => { |
12 | 13 | // Look up the activation code from the License record |
13 | 14 | const license = await __unsafePrisma.license.findUnique({ |
14 | | - where: { orgId: SINGLE_TENANT_ORG_ID }, |
| 15 | + where: { orgId }, |
| 16 | + }); |
| 17 | + |
| 18 | + const userCount = await __unsafePrisma.userToOrg.count({ |
| 19 | + where: { |
| 20 | + orgId, |
| 21 | + userId: { |
| 22 | + not: SOURCEBOT_GUEST_USER_ID, |
| 23 | + } |
| 24 | + }, |
15 | 25 | }); |
16 | 26 |
|
17 | 27 | const activationCode = license?.activationCode |
18 | 28 | ? decryptActivationCode(license.activationCode) |
19 | 29 | : undefined; |
20 | 30 |
|
21 | | - const payload = { |
| 31 | + const payload: ServicePingRequest = { |
22 | 32 | installId: env.SOURCEBOT_INSTALL_ID, |
23 | 33 | version: SOURCEBOT_VERSION, |
| 34 | + userCount, |
24 | 35 | ...(activationCode && { activationCode }), |
25 | 36 | }; |
26 | 37 |
|
27 | | - try { |
28 | | - const response = await fetchWithRetry(`${env.SOURCEBOT_LIGHTHOUSE_URL}/ping`, { |
29 | | - method: 'POST', |
30 | | - headers: { 'Content-Type': 'application/json' }, |
31 | | - body: JSON.stringify(payload), |
32 | | - }); |
33 | | - |
34 | | - if (!response.ok) { |
35 | | - logger.error(`Service ping failed with status ${response.status}`); |
36 | | - return; |
37 | | - } |
38 | | - |
39 | | - logger.info(`Service ping sent successfully`); |
| 38 | + const response = await sendServicePing(payload); |
| 39 | + if (isServiceError(response)) { |
| 40 | + logger.error(`Service ping failed:\n ${JSON.stringify(response, null, 2)}`) |
| 41 | + return; |
| 42 | + } |
40 | 43 |
|
41 | | - // If we have a license, update it with the Lighthouse response |
42 | | - if (license) { |
43 | | - const body = await response.json(); |
44 | | - const result = lighthouseResponseSchema.safeParse(body); |
| 44 | + logger.info(`Service ping sent successfully`); |
45 | 45 |
|
46 | | - if (!result.success) { |
47 | | - logger.error(`Invalid Lighthouse response: ${result.error}`); |
48 | | - return; |
49 | | - } |
| 46 | + // If we have a license and Lighthouse returned license data, sync it |
| 47 | + if (license && response.license) { |
| 48 | + const { plan, seats, status } = response.license; |
50 | 49 |
|
51 | | - const { plan, seats, status } = result.data; |
52 | | - |
53 | | - await __unsafePrisma.license.update({ |
54 | | - where: { orgId: SINGLE_TENANT_ORG_ID }, |
55 | | - data: { |
56 | | - plan, |
57 | | - seats, |
58 | | - status, |
59 | | - lastSyncAt: new Date(), |
60 | | - }, |
61 | | - }); |
| 50 | + await __unsafePrisma.license.update({ |
| 51 | + where: { orgId: SINGLE_TENANT_ORG_ID }, |
| 52 | + data: { |
| 53 | + plan, |
| 54 | + seats, |
| 55 | + status, |
| 56 | + lastSyncAt: new Date(), |
| 57 | + }, |
| 58 | + }); |
62 | 59 |
|
63 | | - logger.info(`License synced: plan=${plan}, seats=${seats}, status=${status}`); |
64 | | - } |
65 | | - } catch (error) { |
66 | | - logger.error(`Service ping failed: ${error}`); |
| 60 | + logger.info(`License synced: plan=${plan}, seats=${seats}, status=${status}`); |
67 | 61 | } |
68 | 62 | }; |
69 | 63 |
|
70 | 64 | export const startServicePingCronJob = () => { |
71 | | - sendServicePing(); |
72 | | - setInterval(sendServicePing, SERVICE_PING_INTERVAL_MS); |
| 65 | + syncWithLighthouse(SINGLE_TENANT_ORG_ID); |
| 66 | + setInterval( |
| 67 | + () => syncWithLighthouse(SINGLE_TENANT_ORG_ID), |
| 68 | + SERVICE_PING_INTERVAL_MS |
| 69 | + ); |
73 | 70 | }; |
0 commit comments