Skip to content

Commit

Permalink
feat: add cron job
Browse files Browse the repository at this point in the history
  • Loading branch information
rotimi-best committed Dec 5, 2024
1 parent a8cef43 commit e9a1d45
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ jobs:
- name: Install step
run: 'deno install'

- name: Build step
run: 'deno task fe:build'

- name: Upload to Deno Deploy
uses: denoland/deployctl@v1
with:
Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"fe:build": "deno run -A --node-modules-dir npm:vite build",
"fe:preview": "deno run -A --node-modules-dir npm:vite preview",
"fe:serve": "deno run --allow-net --allow-read vite_server.ts",
"be:dev": "deno run --allow-all --watch --env-file main.ts",
"be:scrape": "deno run --allow-all --env-file utils/scrape/index.ts",
"be:dev": "deno run --allow-all --watch --env-file --unstable-cron main.ts",
"be:scrape": "deno run --allow-all --env-file utils/scrape/run.ts",
"be:deploy": "deployctl deploy --prod --env-file=.env --project=naijastars-api --exclude=./node_modules --exclude=./dist --exclude=./src --exclude=./.vite main.ts"
},
"compilerOptions": {
Expand Down
14 changes: 14 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { rateLimiter } from 'npm:hono-rate-limiter';
import { Redis } from '@upstash/redis';
import { RedisStore } from '@hono-rate-limiter/redis';

import { scrape } from './utils/scrape/index.ts';

import RepositoryHandler from './routes/repository.ts';

import 'jsr:@std/dotenv/load';
Expand Down Expand Up @@ -60,3 +62,15 @@ app.route('/v1', RepositoryHandler);
console.log(`API server running on http://localhost:${PORT}`);

Deno.serve({ port: PORT }, app.fetch);

/** Cron Jobs */

Deno.cron('scrape-repositories', '0 */12 * * *', async () => {
console.log('Running repository scraper...');
try {
await scrape();
console.log('Repository scrape completed successfully');
} catch (error) {
console.error('Error running repository scraper:', error);
}
});
4 changes: 1 addition & 3 deletions utils/scrape/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function convertToJSON(repositories: string[]): ScrapedRepository[] {
});
}

const getData = async () => {
export const scrape = async () => {
console.log('Fetching Repositories From GitHub...');

const response = await fetch(
Expand Down Expand Up @@ -183,5 +183,3 @@ const getData = async () => {

console.log('Data saved to Neon.');
};

getData();
3 changes: 3 additions & 0 deletions utils/scrape/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { scrape } from './index.ts';

scrape();

0 comments on commit e9a1d45

Please sign in to comment.