Skip to content

Commit

Permalink
additional comments/logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Nov 19, 2021
1 parent 40e7371 commit aba132e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 192 deletions.
42 changes: 34 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
});

Expand All @@ -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) => {
Expand All @@ -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);
Expand All @@ -106,12 +129,15 @@ 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 => {
console.log(err);
});
};

createServer();
createServer();
23 changes: 16 additions & 7 deletions taskRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,29 +55,37 @@ const fetchToken = () => {
*/
const getCredentials = () => {
fetchToken();

// on initialization, will wait for credentials from server on 10 sec interval
initInterval = setInterval(() => {
fetchToken();
console.log(
'\x1b[93mtaskRunner.js::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);

// 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);
172 changes: 0 additions & 172 deletions test.js

This file was deleted.

11 changes: 6 additions & 5 deletions watcher.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@

const axios = require('axios');

/**
* Watches given folder id in the project, dispatches downloader for any files needing to be updated
* @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) => {
let timeFilter = {};

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}`
},
Expand Down

0 comments on commit aba132e

Please sign in to comment.