From 91d93e78b04a042d76cbf4e7c2f05bf6e8fd1ac1 Mon Sep 17 00:00:00 2001 From: davodm Date: Tue, 10 Oct 2023 16:02:19 +0100 Subject: [PATCH] fix: updating lastRunTime for multi controllers --- instagram.js | 3 +++ src/dynamodb.js | 23 +++++++++++++++++------ tweet.js | 6 +++--- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/instagram.js b/instagram.js index 26c17a9..6efdc1f 100644 --- a/instagram.js +++ b/instagram.js @@ -6,11 +6,14 @@ const { abbreviateNumber, numFormat } = require("./src/number"); const { isOffline } = require("./src/env"); const { publishImage } = require("./src/instagram"); const { writeCaption } = require("./src/ai"); +const { updateLastRunTime } = require("./src/dynamodb"); exports.handler = async function (event) { try { // Run weekly recap await weeklyRecap(); + // Update last run time on DynamoDB + updateLastRunTime("instagram", { type: "weekly-recap" }); } catch (err) { console.error(err); } diff --git a/src/dynamodb.js b/src/dynamodb.js index 13f2f2b..0ef327f 100644 --- a/src/dynamodb.js +++ b/src/dynamodb.js @@ -19,16 +19,17 @@ const docClient = DynamoDBDocumentClient.from(client); /** * Fetch latest run timestamp from DynamoDB + * @param {string} $type type of action * @returns {object} */ -async function getLastRunTime() { +async function getLastRunTime($type) { //Send Request try { const result = await docClient.send( new GetCommand({ TableName: process.env.DYNAMODB_TABLE, Key: { - id: "last-run", + id: `last-run-${$type}`, }, }) ); @@ -41,18 +42,19 @@ async function getLastRunTime() { /** * Update last run time with current timestamp - * @param {*} time + * @param {string} $type type of action + * @param {object} $data data object to save */ -async function updateLastRunTime($subject) { +async function updateLastRunTime($type,$data={}) { //Send Request try { return await docClient.send( new PutCommand({ TableName: process.env.DYNAMODB_TABLE, Item: { - id: "last-run", + id: `last-run-${$type}`, timestamp: Date.now(), - actionSubject: $subject, + ...$data }, }) ); @@ -62,6 +64,10 @@ async function updateLastRunTime($subject) { } } +/** + * Fetch twitter data from DynamoDB + * @returns {object} + */ async function getTwitter() { //Send Requests try { @@ -80,6 +86,11 @@ async function getTwitter() { } } +/** + * Update Twitter data on DynamoDB + * @param {object} $data + * @returns + */ async function updateTwitter($data) { //Send Request try { diff --git a/tweet.js b/tweet.js index 28f81aa..4c1ddb4 100644 --- a/tweet.js +++ b/tweet.js @@ -23,7 +23,7 @@ exports.handler = async function (event) { }; //Last run check - const lastRun = await getLastRunTime(); + const lastRun = await getLastRunTime('tweet'); if (Date.now() - (lastRun.timestamp ?? 0) < 3600 * 1000) { throw new Error("Last run is less than one hour!"); } @@ -49,8 +49,8 @@ exports.handler = async function (event) { await tweet(post); } - //Update last run - await updateLastRunTime(lastKey); + //Update last run time to know what was the last tweet + await updateLastRunTime('tweet',{actionSubject: lastKey}); //Out console.log("Tweet sent successfully!", post);