-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from surenpoghosian/feature/Scheduler
[feature/Scheduler] add scheduler, add scrape validator factory
- Loading branch information
Showing
13 changed files
with
223 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,9 @@ | |
### RUN | ||
`npx tsx src/index.ts` | ||
|
||
### RUN THE SCHEDULER | ||
|
||
`npx tsx src/Scheduler/index.ts` | ||
|
||
### TEST | ||
`npm run test` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import cron from 'node-cron'; | ||
import Main from '..'; | ||
|
||
class ScraperScheduler { | ||
private cronExpression: string; | ||
private task: cron.ScheduledTask | null = null; | ||
|
||
constructor( | ||
cronExpression: string | ||
) { | ||
this.cronExpression = cronExpression; | ||
} | ||
|
||
async scrape(): Promise<void> { | ||
console.log("Waking up to scrape..."); | ||
|
||
const main = new Main(); | ||
await main.run(); | ||
|
||
console.log("Scraping done. Going back to sleep."); | ||
} | ||
|
||
start(): void { | ||
this.task = cron.schedule(this.cronExpression, () => { | ||
console.log('cron triggered'); | ||
this.scrape().catch((error) => { | ||
console.error("Error during scraping:", error); | ||
}); | ||
}); | ||
|
||
console.log(`Scheduler started with cron expression: ${this.cronExpression}`); | ||
} | ||
|
||
stop(): void { | ||
if (this.task) { | ||
this.task.stop(); | ||
console.log("Scheduler stopped."); | ||
} | ||
} | ||
} | ||
|
||
// Configuration and initialization | ||
// const cronExpression = '0 */12 * * *'; // Every 12 hours | ||
const cronExpression = '*/5 * * * *'; // Every 5 minutes | ||
// const cronExpression = '* * * * *'; // Every minute | ||
|
||
const scheduler = new ScraperScheduler( | ||
cronExpression | ||
); | ||
|
||
// Start the scheduler | ||
scheduler.start(); | ||
|
||
// Handle graceful shutdown | ||
process.on('SIGINT', () => { | ||
console.log("Received SIGINT. Gracefully shutting down."); | ||
scheduler.stop(); | ||
process.exit(0); | ||
}); | ||
|
||
process.on('SIGTERM', () => { | ||
console.log("Received SIGTERM. Gracefully shutting down."); | ||
scheduler.stop(); | ||
process.exit(0); | ||
}); | ||
|
||
export default ScraperScheduler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import ListAm from "./products/ListAm"; | ||
import MobileCentre from "./products/MobileCentre"; | ||
import { ScrapeValidatorVariant } from "../configs/types"; | ||
|
||
class ScrapeValidatorFactory { | ||
static createScrapeValidator(type: ScrapeValidatorVariant) { | ||
switch (type) { | ||
case ScrapeValidatorVariant.LISTAM: | ||
return new ListAm(); | ||
case ScrapeValidatorVariant.MOBILECENTRE: | ||
return new MobileCentre(); | ||
default: | ||
throw new Error(`Unsupported scrape type: ${type}`); | ||
} | ||
} | ||
}; | ||
|
||
export default ScrapeValidatorFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { HTMLElement } from "node-html-parser"; | ||
import { ScrapeType, IScrapeValidator } from "../../configs/types"; | ||
|
||
class ListAm implements IScrapeValidator { | ||
|
||
validate(html: HTMLElement, scrapeType: ScrapeType): boolean { | ||
console.log(`Validating scrape...`); | ||
|
||
switch (scrapeType) { | ||
case ScrapeType.ITEM: | ||
throw new Error('ScrapeType.ITEM validator logic is not implemented!'); | ||
case ScrapeType.LIST: | ||
const parentDivs = html.querySelectorAll('.gl'); | ||
if (!parentDivs.length) { | ||
return false; | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
console.log('HTML is valid'); | ||
return true; | ||
}; | ||
} | ||
|
||
export default ListAm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { HTMLElement } from "node-html-parser"; | ||
import { ScrapeType, IScrapeValidator } from "../../configs/types"; | ||
|
||
class MobileCentre implements IScrapeValidator { | ||
|
||
validate(html: HTMLElement, scrapeType: ScrapeType): boolean { | ||
throw new Error('function not implemented') | ||
}; | ||
} | ||
|
||
export default MobileCentre; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.