Upload status to prometheus pushgateway.

This commit is contained in:
2023-09-03 16:23:07 +00:00
parent 5182f605ce
commit c8257f2db1
7 changed files with 13968 additions and 99 deletions

29
sync.js
View File

@@ -3,6 +3,7 @@ const mysql = require('mysql2');
const slugify = require('slugify');
const GhostAdminAPI = require('@tryghost/admin-api');
const Downloader = require('nodejs-file-downloader');
const prom = require('prom-client');
const config = require('./config.js');
const db_conn = mysql.createConnection(config.db_conn).promise();
@@ -74,6 +75,11 @@ async function pullInstagramPosts() {
const insertSql = "INSERT IGNORE INTO `ig_posts` (`parent_instagram_id`, `instagram_id`, `caption`, `media_type`, `media_url`, `permalink`, `datetime`) VALUES ?";
const foundPosts = new prom.Gauge({
name: 'instasync_instagram_posts',
help: 'Total posts found on Instagram'
});
foundPosts.set(postData.length);
console.log("Inserting " + postData.length + " posts");
await db_conn.query(insertSql, [postData]);
}
@@ -95,7 +101,7 @@ function getAppropriateCard(post, uploadedMediaUrl) {
async function doWork() {
await pullInstagramPosts();
await pullInstagramPosts();
//Get all the Instagram photos/videos that we have get to post to the blog
const [posts, _] = await db_conn.query("SELECT * FROM ig_posts WHERE post_id IS NULL AND instagram_id = parent_instagram_id UNION SELECT * FROM ig_posts WHERE post_id IS NULL AND instagram_id != parent_instagram_id");
@@ -105,7 +111,16 @@ async function doWork() {
var cardIndex = 0;
var lastParentId = null;
const toProcess = new prom.Gauge({
name: 'instasync_processed_posts',
help: 'Total IG posts to post to Ghost'
});
toProcess.set(posts.length);
console.log("Processing " + posts.length + " posts");
const downloadCounter = new prom.Counter({
name: 'instasync_downloads',
help: 'Total IG posts downloaded'
});
for (post of posts) {
//Download the media to our server
@@ -125,6 +140,7 @@ async function doWork() {
await downloader.download();//Downloader.download() returns a promise.
//Upload it to the Ghost blog file directory
downloadCounter.inc();
console.log("Uploading " + fileName);
var upload;
if (fileName.endsWith('mp4')) {
@@ -222,6 +238,11 @@ async function doWork() {
//We will now submit the posts to the blog..
const submittedPosts = new prom.Gauge({
name: 'instasync_submitted_posts',
help: 'Total IG posts submitted to Ghost'
});
submittedPosts.set(posts.length);
console.log("Submitting " + posts.length + " posts");
for (instagramParentId of Object.keys(processedPosts)) {
@@ -279,6 +300,12 @@ async function doWork() {
console.log("MARKED AS POSTED");
}
let promClient = new prom.Pushgateway('http://pushgateway.service.consul:9091');
await promClient
.push({ jobName: 'instasync' })
.catch(err => {
console.log('Error: ${err}');
});
console.log("Done");
}