Skip to content

Commit

Permalink
added cron scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
Weldawadyathink committed Feb 8, 2024
1 parent c8a8bf6 commit 450180d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
23 changes: 23 additions & 0 deletions services/embedding-indexer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions services/embedding-indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"dependencies": {
"axios": "^1.6.7",
"cron": "^3.1.6",
"node-cron": "^3.0.3",
"pg": "^8.11.3"
}
Expand Down
9 changes: 7 additions & 2 deletions services/embedding-indexer/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import cron from "node-cron";
import { reindex } from "./reindex";
import { CronJob } from "cron";

reindex()
const job = CronJob.from({
cronTime: "0 */15 * * * *",
onTick: reindex,
start: true,
runOnInit: true,
});
6 changes: 3 additions & 3 deletions services/embedding-indexer/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const query = async (text: string, params: Array<any>) => {
const start = performance.now();
const res = await pool.query(text, params);
const duration = Math.trunc(performance.now() - start);
console.log(
`Executed query: { rows: ${res.rowCount}, duration: ${duration}} `
);
// console.log(
// `Executed query: { rows: ${res.rowCount}, duration: ${duration}} `
// );
return res;
};

Expand Down
10 changes: 5 additions & 5 deletions services/embedding-indexer/src/reindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { query } from "./db";
import Axios from "axios";

export const reindex = async () => {
console.log("Starting embedding reindex");
const results = await query(
// TODO: Remove limit
`
SELECT id
FROM image
WHERE
embedding IS NULL
AND NOT index_error
LIMIT 1
`,
[]
);
results.rows.map((row) => indexItem(row.id));
await Promise.all(results.rows.map((row) => indexItem(row.id)));
console.log("Completed embedding index");
};

const indexItem = async (id: string) => {
Expand All @@ -26,15 +26,15 @@ const indexItem = async (id: string) => {
)}`;
const embeddingInsertQuery = `
UPDATE image
SET test_embedding = $1::vector
SET embedding = $1::vector
WHERE id = $2
`;
const indexErrorQuery = `
UPDATE image
SET index_error = true
WHERE id = $1
`;
Axios.get(clip_url)
return Axios.get(clip_url)
.then((result) => JSON.stringify(result.data))
.then((embedding) => {
query(embeddingInsertQuery, [embedding, id]);
Expand Down

0 comments on commit 450180d

Please sign in to comment.