-
Notifications
You must be signed in to change notification settings - Fork 5
/
sync-assets-queue.js
187 lines (170 loc) · 6.85 KB
/
sync-assets-queue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const externalSequelize = require('./runtime-node-sequelize-connection');
const { QueryTypes } = require('sequelize');
const sequelize = require('sequelize');
const { SyncedAsset, Notification } = require('./models');
const { Queue, Worker } = require('bullmq');
const redis = require('ioredis');
const axios = require('axios');
const connection = new redis({
maxRetriesPerRequest: null
});
const mockWait = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const syncQueue = new Queue('syncQueue', { connection });
new Worker(
'syncQueue',
async (job) => {
console.log(`Starting sync job...Time: ${job.data.timestamp}`);
console.log(`Checking Edge node publish mode...Time: ${job.data.timestamp}`);
const publicConfig = await axios.get(
`${process.env.AUTH_SERVICE_ENDPOINT}/auth/params/public`
);
const edgeNodePublishMode = publicConfig.data.config.find(
item => item.option === 'edge_node_publish_mode'
).value || null;
if(edgeNodePublishMode === "public") {
console.log(`Edge node publish mode is public, aborting sync operation...Time: ${job.data.timestamp}`);
return;
}
const paranetUAL = publicConfig.data.config.find(
item => item.option === 'edge_node_paranet_ual'
).value || null;
try {
const internalSyncedAssets = await SyncedAsset.findAll({
where: {
paranet_ual: paranetUAL,
},
});
if (internalSyncedAssets.length === 0) {
console.log(`First time query...`);
const assets = await externalSequelize.query(
getInitialQuery(paranetUAL),
{
type: QueryTypes.SELECT
}
);
if (assets.length > 0) {
let notification = await storeNotification(assets);
await storeSyncedAssets(assets, notification);
}
} else if (internalSyncedAssets.length > 0) {
const lastSyncedAsset = await SyncedAsset.findOne({
where: {
paranet_ual: paranetUAL,
},
order: [['id', 'DESC']]
});
const date = new Date(lastSyncedAsset.backend_synced_at);
const utcDateString = date.toISOString().replace('Z', '').replace('T', ' ').slice(0, 19);
const assets = await externalSequelize.query(getNextQuery(utcDateString, paranetUAL)
,
{
type: QueryTypes.SELECT
}
);
if (assets.length > 0) {
let notification = await storeNotification(assets);
await storeSyncedAssets(assets, notification);
}
}
} catch (error) {
console.error(error);
}
console.log(`Sync job completed.Time: ${job.data.timestamp}`);
},
{
connection,
concurrency: 1 // Ensure only one job runs at a time
}
);
const getInitialQuery = (paranetUAL) => {
return `
SELECT sa.*
FROM paranet_synced_asset sa
INNER JOIN (
SELECT ual, MAX(id) AS max_id
FROM paranet_synced_asset
WHERE (ual, created_at) IN
(SELECT ual, MAX(created_at)
FROM paranet_synced_asset
GROUP BY ual)
AND paranet_ual = '${paranetUAL}'
GROUP BY ual
) latest ON sa.id = latest.max_id;`;
};
const getNextQuery = (date, paranetUAL) => {
return `
SELECT sa.*
FROM paranet_synced_asset sa
INNER JOIN (
SELECT ual, MAX(id) AS max_id
FROM paranet_synced_asset
WHERE created_at > CONVERT_TZ('${date}', @@session.time_zone, '+00:00')
AND (ual, created_at) IN
(SELECT ual, MAX(created_at)
FROM paranet_synced_asset
WHERE created_at > CONVERT_TZ('${date}', @@session.time_zone, '+00:00')
GROUP BY ual)
AND paranet_ual = '${paranetUAL}'
GROUP BY ual
) latest ON sa.id = latest.max_id;
`;
};
function getCurrentTimeProperFormat() {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
function getFormattedDate(inputDate) {
// Convert to a string and remove the 'Z' to avoid UTC conversion
const dateString = inputDate.toISOString().replace('Z', '');
// Manually extract year, month, day, hour, minute, second from the ISO string
const [datePart, timePart] = dateString.split('T');
const [year, month, day] = datePart.split('-');
const [hour, minute, second] = timePart.split(':');
// Combine them into the desired format
return `${year}-${month}-${day} ${hour}:${minute}:${second.split('.')[0]}`;
}
function getFormattedDate2(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are 0-based
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
async function storeNotification(assets) {
let notification = await Notification.create({
title: 'New Knowledge assets are created!'
});
notification.message = `Your node has ingested ${assets.length} new knowledge assets since your last login.`;
await notification.save();
return notification;
}
async function storeSyncedAssets(assets, notification) {
for (let x = 0; x < assets.length; x++) {
let syncedData = assets[x];
syncedData.backend_synced_at = getCurrentTimeProperFormat();
syncedData.runtime_node_synced_at = getFormattedDate(
syncedData.created_at
);
syncedData.notification_id = notification.id;
delete syncedData.id;
delete syncedData.created_at;
delete syncedData.updated_at;
let createdSyncedAsset = await SyncedAsset.create(syncedData);
}
return true;
}
// Add Jobs Every 10 Seconds
setInterval(async () => {
console.log('Queueing sync job...');
await syncQueue.add('syncJob', { timestamp: Date.now() });
}, 10000);