Skip to content

Commit

Permalink
fix(scheduler): resolve update domain availability
Browse files Browse the repository at this point in the history
  • Loading branch information
gilsondev committed Oct 19, 2023
1 parent 4d45cd1 commit 5e55288
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
50 changes: 38 additions & 12 deletions scheduler/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createClient } from "@checkstatusgovbr/database-ts";
import { DatabaseClient } from "@checkstatusgovbr/database-ts/src/client";
import AvailabilityDomains from "./availability";
import pino from "pino";
import { Domain } from "domain";

const logger = pino();

Expand All @@ -19,12 +20,25 @@ export default class AvailabilityDomainsService {
this.pingClient = new AvailabilityDomains();
}

private async fetchDomains(): Promise<DomainData[]> {
try {
logger.info("Fetching domains");
const result = await this.client.sql<DomainData>(
"SELECT domain FROM domains",
[]
);
return result;
} catch (error) {
logger.error(error);
} finally {
await this.client.close();
}

return [];
}

async pingDomains() {
logger.info("Pinging domains");
const domains = await this.client.sql<DomainData>(
"SELECT domain FROM domains",
[]
);
const domains = await this.fetchDomains();

let results: DomainData[] = [];
for (const data of domains) {
Expand All @@ -39,15 +53,27 @@ export default class AvailabilityDomainsService {
results.push({ ...data, available: isAvailable });
}

const updateValues = results
.map((data) => `('${data.domain}', ${data.available})`)
await this.updateDomainsStatus(results);
}

public async updateDomainsStatus(domains: DomainData[]) {
const updateValues = domains
.map((data) => `('${data.domain}', ${data.available}, NOW())`)
.join(", ");

await this.client.sql<DomainData>(
`UPDATE domains SET available = v.available FROM (VALUES ${updateValues}) AS v(domain, available) WHERE domains.domain = v.domain`,
[]
);
logger.info(`Updating domains`);
try {
this.client = createClient();
await this.client.rawSQL<DomainData>(
`UPDATE domains SET available = v.available, updated_at = v.updated_at FROM (VALUES ${updateValues}) AS v(domain, available, updated_at) WHERE domains.domain = v.domain`
);

logger.info(`Updated ${results.length} domains`);
logger.info(`Updated ${domains.length} domains`);
} catch (error) {
logger.error(error);
} finally {
logger.debug("Closing connection");
await this.client.close();
}
}
}
15 changes: 12 additions & 3 deletions shared/database_ts/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ export class DatabaseClient {
this.client.connect();
}

async sql<T>(query: string, values: any[]) {
async connect() {
this.client.connect();
}

async sql<T>(query: string, values: any[]): Promise<T[]> {
const result = await this.client.query(query, values);
return result.rows.map((row: T) => row);
}

async close() {
await this.client.end();
async rawSQL<T>(query: string): Promise<T[]> {
const result = await this.client.query(query);
return result.rows.map((row: T) => row);
}

async close(): Promise<void> {
this.client.end();
}
}

0 comments on commit 5e55288

Please sign in to comment.