Skip to content

Commit

Permalink
fix: updating lastRunTime for multi controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
davodm committed Oct 10, 2023
1 parent a9b3405 commit 91d93e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
3 changes: 3 additions & 0 deletions instagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 17 additions & 6 deletions src/dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
},
})
);
Expand All @@ -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
},
})
);
Expand All @@ -62,6 +64,10 @@ async function updateLastRunTime($subject) {
}
}

/**
* Fetch twitter data from DynamoDB
* @returns {object}
*/
async function getTwitter() {
//Send Requests
try {
Expand All @@ -80,6 +86,11 @@ async function getTwitter() {
}
}

/**
* Update Twitter data on DynamoDB
* @param {object} $data
* @returns
*/
async function updateTwitter($data) {
//Send Request
try {
Expand Down
6 changes: 3 additions & 3 deletions tweet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
Expand All @@ -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);
Expand Down

0 comments on commit 91d93e7

Please sign in to comment.