I had set up a cron which will update my table in the postgres database by fetching values from redis , the cron runs every 10 minutes . The values will be updated in the DB if same date data is present in the redis , otherwise will be updated with new value in the redis , Here is the code
const CacheUtils = require('../../utils/cache-utils');
const { DateTime } = require('luxon');
const {bookshelf} = require('../../schema/knex');
const googleLeadsSearchDailyEntries = require('../../schema/models/google-leads-search-daily-entries');
module.exports = class GoogleLeadFinder {
static async googleLeadSearchUpsert() {
const dailyCount = await this._getDataFromRedis();
if (dailyCount.length === 0) {
return;
}
await bookshelf
.transaction(async trans => {
const uniqueConstraint = `("userId","orgId","quotaDate")`;
const params = {
table: 'googleLeadsSearchDailyEntries',
objects: dailyCount,
constraint: uniqueConstraint
};
const action = `UPDATE SET "dailySearch"=EXCLUDED."dailySearch","updatedAt" = NOW()`;
await googleLeadsSearchDailyEntries.upsert(params, trans,action);
})
}
static async _getDataFromRedis() {
const todayDate = DateTime.local();
const client = await CacheUtils.getClient();
const results = [];
const filterCondition = `googleleads-userdaycountval-${todayDate.day}-${todayDate.month}-${todayDate.year}-*`;
const keys = await client.keysAsync(filterCondition);
for (const key of keys) {
const fetchDataFromKey = key.match(/d+/g).map(numStr => parseInt(numStr));
const dailySearch = parseInt(await client.getAsync(key));
results.push({ userId: fetchDataFromKey[3], orgId: fetchDataFromKey[4], dailySearch, quotaDate: todayDate.toISODate() });
}
return results;
}
};
the code is running on my EC2 instance , and the database is on my aws rds check the image
as you can see on the image , cron is still updating till 26-04-2024(updatedAt) when the quota date is of 25-04-2024 , it should’nt have updated that
i want the cron to stop updating the data of the tuple whose quota date have been passed , working as expected on my local but not showing consistent results on stage environment
(Note : i have defined userId,orgId,quotaDate as unique fields)