-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(functions): migrate scheduler functions to v2
- Loading branch information
Showing
6 changed files
with
36 additions
and
40 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 |
---|---|---|
@@ -1,29 +1,25 @@ | ||
import * as functions from 'firebase-functions'; | ||
import { logger } from 'firebase-functions'; | ||
import { onSchedule } from 'firebase-functions/lib/v2/providers/scheduler'; | ||
import { DateTime } from 'luxon'; | ||
import { ExchangeRateImporter } from './ExchangeRateImporter'; | ||
|
||
/** | ||
* Function periodically scrapes currency exchange rates and saves them to firebase | ||
*/ | ||
export default functions | ||
.runWith({ | ||
timeoutSeconds: 540, | ||
}) | ||
.pubsub.schedule('0 1 * * *') | ||
.onRun(async () => { | ||
const exchangeRateImporter = new ExchangeRateImporter(); | ||
const existingExchangeRates = await exchangeRateImporter.getAllExchangeRates(); | ||
for ( | ||
let timestamp = ExchangeRateImporter.startTimestamp; | ||
timestamp <= Date.now() / 1000; | ||
timestamp += ExchangeRateImporter.secondsInDay | ||
) { | ||
if (!existingExchangeRates.has(timestamp)) { | ||
try { | ||
await exchangeRateImporter.fetchAndStoreExchangeRates(DateTime.fromSeconds(timestamp)); | ||
} catch (error) { | ||
functions.logger.error(`Could not ingest exchange rate`, error); | ||
} | ||
export default onSchedule('0 1 * * *', async () => { | ||
const exchangeRateImporter = new ExchangeRateImporter(); | ||
const existingExchangeRates = await exchangeRateImporter.getAllExchangeRates(); | ||
for ( | ||
let timestamp = ExchangeRateImporter.startTimestamp; | ||
timestamp <= Date.now() / 1000; | ||
timestamp += ExchangeRateImporter.secondsInDay | ||
) { | ||
if (!existingExchangeRates.has(timestamp)) { | ||
try { | ||
await exchangeRateImporter.fetchAndStoreExchangeRates(DateTime.fromSeconds(timestamp)); | ||
} catch (error) { | ||
logger.error(`Could not ingest exchange rate`, error); | ||
} | ||
} | ||
}); | ||
} | ||
}); |
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