From aba132ecaa4da4f953bdabf0c46972571d16b421 Mon Sep 17 00:00:00 2001 From: pan261 Date: Fri, 19 Nov 2021 00:19:19 -0500 Subject: [PATCH] additional comments/logging --- server.js | 42 +++++++++--- taskRunner.js | 23 +++++-- test.js | 172 -------------------------------------------------- watcher.js | 11 ++-- 4 files changed, 56 insertions(+), 192 deletions(-) delete mode 100644 test.js diff --git a/server.js b/server.js index aceff99..dd3c676 100644 --- a/server.js +++ b/server.js @@ -44,7 +44,7 @@ let intervalID = null; * - will return credentials JSON object or null if there isn't one * @returns server object */ -function createServer () { +const createServer = () => { const app = express(); const PORT = process.env.PORT || 3000; @@ -55,6 +55,10 @@ function createServer () { // Endpoint to begin authentication process app.get('/auth', function (req, res) { + console.log( + '\x1b[96mserver.js::createServer:', + '\x1b[0m/auth endpoint called' + ); res.redirect(oAuth2ThreeLegged.generateAuthUrl()); }); @@ -65,16 +69,23 @@ function createServer () { credentials = creds; refreshTime = creds.expires_in - 300; res.send('Generated token: ' + credentials.access_token); + console.log( + '\x1b[92mserver.js::createServer:', + '\x1b[0m/callback reached, token generated' + ); }) .then(() => { // sets refresh() function to run on an interval every 55 minutes intervalID = setInterval(() => refresh(), refreshTime * 1000); // 55 seconds to ms - - // sets timeout for 13 days, before clearing refresh interval and clearing credentials + + // sets timeout for 13 days, before clearing refresh interval and clearing credentials setTimeout(() => { credentials = null; clearInterval(intervalID); - console.log("Refresh token expiring, need to auth again"); + console.log( + '\x1b[93mserver.js::createServer:', + '\x1b[0mRefresh token expiring, need to authenticate again' + ); }, 13 * 24 * 3600 * 1000); // 13 days to ms }) .catch((err) => { @@ -85,9 +96,21 @@ function createServer () { // Endpoint for internal use, to get credentials from our auth server app.get('/credentials', function (req, res) { - res.send(credentials ? credentials : "Need to authenticate at localhost:3000/auth"); + if (credentials) { + console.log( + '\x1b[96mserver.js::createServer:', + '\x1b[0m/credentials endpoint called, credentials returned' + ); + res.send(credentials); + } else { + console.log( + '\x1b[91mserver.js::createServer:', + '\x1b[0m/credentials endpoint called, no credentials found' + ); + res.send('Need to authenticate at localhost:3000/auth'); + } }); - + // Default endpoint app.use((err, req, res, next) => { console.error(err); @@ -106,7 +129,10 @@ const refresh = () => { oAuth2ThreeLegged.refreshToken(credentials, scopes) .then(creds => { credentials = creds; - console.log('new token generated from refresh token:'); + console.log( + '\x1b[92mserver.js::refresh:', + '\x1b[0mnew token generated from refresh token:' + ); console.log(credentials); }) .catch(err => { @@ -114,4 +140,4 @@ const refresh = () => { }); }; -createServer(); +createServer(); \ No newline at end of file diff --git a/taskRunner.js b/taskRunner.js index 8cebeab..edc9851 100644 --- a/taskRunner.js +++ b/taskRunner.js @@ -4,10 +4,11 @@ require('dotenv').config(); /** * This file will be responsible for managing the watcher - * as well as ensuring the credentials are avaiable + * as well as fetching the credentials from the server */ -const projectID = 'a.YnVzaW5lc3M6cHVyZHVlMjY5NiMyMDIxMTEwODQ2NDQzMzk3Ng'; +const adminProjectID = 'a.YnVzaW5lc3M6cHVyZHVlMjY5NiMyMDIxMTEwODQ2NDQzMzk3Ng'; +const adminProjectTopFolder = 'urn:adsk.wipprod:fs.folder:co.ltuYad4ZTtieJLm6j3MGuA'; let credentials = null; let initInterval = null; @@ -54,6 +55,8 @@ const fetchToken = () => { */ const getCredentials = () => { fetchToken(); + + // on initialization, will wait for credentials from server on 10 sec interval initInterval = setInterval(() => { fetchToken(); console.log( @@ -61,22 +64,28 @@ const getCredentials = () => { '\x1b[0mWaiting for initial auth' ); }, 10000); + + // if token is present, call fetchToken every 55 min to get fresh tokens updateInterval = setInterval(fetchToken, 3300 * 1000); }; /** * Function to dispatch the watcher */ -const dispatchWatcher = () => { - if (credentials == null) { +const dispatchWatcher = (interval) => { + if (credentials === null) { console.log( '\x1b[93mtaskRunner.js::dispatchWatcher:', '\x1b[0mNo credentials' ); - } else { - watcher.watch(projectID, 'urn:adsk.wipprod:fs.folder:co.ltuYad4ZTtieJLm6j3MGuA', credentials, 5); + return; } + + watcher.watch(adminProjectID, adminProjectTopFolder, credentials, interval); }; getCredentials(); -setInterval(dispatchWatcher, 5000); \ No newline at end of file + +// There should be a buffer between how far back the watcher checks +// and how often it runs. We may need to test this out. +setInterval(dispatchWatcher, 5000, 7000); \ No newline at end of file diff --git a/test.js b/test.js deleted file mode 100644 index 9d33275..0000000 --- a/test.js +++ /dev/null @@ -1,172 +0,0 @@ -const authServer = require('./server'); - -const fs = require("fs").promises; -const ForgeSDK = require('forge-apis'); -const fileCreds = require("./credentials"); -const axios = require('axios'); -const watcher = require('./watcher'); -/* - Look at this for fetching new auth tokens in a loop: - https://nodejs.org/en/docs/guides/timers-in-node/ -*/ - -var hasCredentials = false; -var credentials; -var authClient = authServer.authClient; - -var HubsApi = new ForgeSDK.HubsApi(); //Hubs Client -var ProjectsApi = new ForgeSDK.ProjectsApi(); -var BucketsApi = new ForgeSDK.BucketsApi(); //Buckets Client -var FoldersApi = new ForgeSDK.FoldersApi(); -var ObjectsApi = new ForgeSDK.ObjectsApi(); - -// Define constants for file sync folders/project -const projectID = "a.YnVzaW5lc3M6cHVyZHVlMjY5NiMyMDIxMTEwODQ2NDQzMzk3Ng"; - - -// async function waitUntil(hasCredentials) { -// return await new Promise(resolve => { -// const interval = setInterval(() => { -// console.log('Running every 5 sec, waiting for browser authorization at localhost:3000/auth'); -// hasCredentials = authServer.getCredentials(); -// if (hasCredentials != undefined) { -// credentials = hasCredentials; -// // console.log(credentials) -// process.env.CREDENTIALS = JSON.stringify(credentials); -// fs.writeFile("credentials.json", JSON.stringify(credentials), err => { -// if (err) { -// throw err; -// } -// console.log("credentials file written") -// }) -// resolve('credentials recieved'); -// clearInterval(interval); -// }; -// }, 5000); -// }); -// } - - -const donwload = (storageLocation, fileName, destination) => { - ObjectsApi.getObject('wip.dm.prod', loc, {}, authClient, credentials) - .then((res) => { - - // For now writing to project (node project) directory, eventually this - // will be something else - fs.writeFile(`./${destination}/${fileName}`, res.body); - }) - .catch((err) => { - console.log("error while processing " + loc); - console.log(err); - }); -} - -(async () => { - // var content = null; - - // var data = await fs.readFile("credentials.json", 'ascii'); - - // // Converting to JSON - // content = await JSON.parse(data); - // // console.log(Object.keys(content).length) - - // if (Object.keys(content).length == 0) { - // // create the server - // let server = await authServer.create(); - - // // wait until user authenticates through browser - // await waitUntil(hasCredentials); - - // // cannot shutdown the server becuase it is running on the same thread, doing this will just kill the node process (i.e. test.js) - // // authServer.shutdown(server); - // } - - // Object.keys(fileCreds).length == 0 ? credentials = hasCredentials : credentials = fileCreds; - - console.log("Authenticated"); - // console.log(credentials); - // console.log(authClient); - - console.log(JSON.parse(process.env.CREDENTIALS)); - - // Can do use forge APIs now, using credentials and authClient - - // testing - - // Using "Team Admin Access Test" as the test project - - const gavinHubID = 'a.YnVzaW5lc3M6cHVyZHVlMjY5Ng'; - const adminProjectID = 'a.YnVzaW5lc3M6cHVyZHVlMjY5NiMyMDIxMTEwODQ2NDQzMzk3Ng'; - const adminProjectTopFolder = 'urn:adsk.wipprod:fs.folder:co.ltuYad4ZTtieJLm6j3MGuA'; - - // HubsApi.getHubs({}, authClient, credentials).then(function(hubs) { - // console.log(hubs.body.data); - // }, function(err) { - // console.log(err); - // }); - - // ProjectsApi.getHubProjects(gavinHubID, {}, authClient, credentials).then(function(projects) { - // console.log(projects.body.data); - // }, function(err) { - // console.log(err) - // }) - - // ProjectsApi.getProjectTopFolders(gavinHubID, adminProjectID, authClient, credentials).then(function(folders) { - // console.log(folders.body.data); - // }, function(err) { - // console.log(err) - // }) - - var obj; - - watcher.watch(projectID, adminProjectTopFolder, credentials); - - // FoldersApi.getFolderContents(adminProjectID, adminProjectTopFolder, {}, authClient, credentials).then(async function(res) { - - - // // res.body.data includes items, res.body.included has the versions - // // we will use versions to download - - // // items will be downloaded to /download_tests - - // content = res.body.included; - // console.log(content); - // for (var i = 0; i < content.length; i++) { - // // console.log(content[i].type) - // if (content[i].type == "versions") { - // var loc = content[i].relationships.storage.data.id - // loc = loc.substring(loc.indexOf("/") + 1) - // console.log(loc) // bucket location - - // console.log(content[i].relationships.storage) - - // var fileName = content[i].attributes.displayName; - // var extension = content[i].attributes.name.substring(content[i].attributes.name.indexOf(".")); - - // if (fileName.indexOf(".") == -1) { - // fileName = fileName + extension; - // } - - // ObjectsApi.getObject('wip.dm.prod', loc, {}, authClient, credentials) - // .then((res) => { - // // console.log(res) - // fs.writeFile(`./download_tests/${fileName}`, res.body) - // }) - // .catch((err) => { - // console.log("error while processing " + loc); - // console.log(err); - // }); - - // } - // } - - // }, function(err) { - // console.log(err) - // }) - - // var file = await obj; - - // even more secret donut id: urn:adsk.wipprod:dm.lineage:VAON-KqMTpWIqGzxw9k42w - - -})(); diff --git a/watcher.js b/watcher.js index fc0e88c..e439dcb 100644 --- a/watcher.js +++ b/watcher.js @@ -1,4 +1,3 @@ - const axios = require('axios'); /** @@ -6,7 +5,7 @@ const axios = require('axios'); * @param {String} projectId the project id * @param {String} folderId the folder id * @param {Object} credentials credentials for the call - * @param {Integer} interval number of seconds used to filter the date + * @param {Integer} interval number of ms used to filter the date * if an interval is not passed, watcher will loop through all files in the folder */ exports.watch = (projectID, folderID, credentials, interval) => { @@ -14,13 +13,15 @@ exports.watch = (projectID, folderID, credentials, interval) => { if (interval) { var dateNow = new Date(); - var dateXSecondsAgo = new Date(dateNow.getTime() - interval * 1000); + var dateXSecondsAgo = new Date(dateNow.getTime() - interval); timeFilter = { 'filter[lastModifiedTime]-ge': dateXSecondsAgo.toISOString() }; } - + console.log( + '\x1b[96mwatcher.js::watch:', + '\x1b[0mChecking for files modified since ' + interval/1000 + ' sec ago'); axios({ method: 'GET', - url: `https://developer.api.autodesk.com//data/v1/projects/${projectID}/folders/${folderID}/contents`, + url: `https://developer.api.autodesk.com/data/v1/projects/${projectID}/folders/${folderID}/contents`, headers: { Authorization: `Bearer ${credentials.access_token}` },