Skip to content

Commit

Permalink
remove backup mode and route everything to our solr
Browse files Browse the repository at this point in the history
  • Loading branch information
mdrum committed Oct 18, 2024
1 parent 6fa705d commit 4c270ca
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 78 deletions.
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
NEXT_PUBLIC_SUPPLEMENTAL_SOLR=http://docker-prod-1.psi.edu:8983/solr
NEXT_PUBLIC_CORE_SOLR=https://pds.nasa.gov/services/search
NEXT_PUBLIC_SOLR_USER=arcnav
NEXT_PUBLIC_SOLR_PASS=changeme
37 changes: 1 addition & 36 deletions pages/[...identifier].js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import React from 'react';
import { resolveType, setTheme, types } from 'services/pages.js';
import GlobalContext from 'components/contexts/GlobalContext';
import Themed from 'components/Themed';
import runtime from 'services/runtime';
import { serializeError } from 'serialize-error';
import Head from 'next/head'

function ProductPageContent({error, loaded, model, type, ...otherProps}) {
Expand Down Expand Up @@ -82,29 +80,7 @@ export async function getServerSideProps(context) {
if(query.pdsOnly === 'true') { props.pdsOnly = true }
if(query.mockup === 'true') { props.mockup = true }

// handle forced queries for internal backup mode state
if(!!query.internal_enable_backup_mode) {
internalMessage(runtime.ENABLE_BACKUP_MODE_MESSAGE)
}
if(!!query.internal_disable_backup_mode) {
internalMessage(runtime.DISABLE_BACKUP_MODE_MESSAGE)
}

if(runtime.backupMode) {
props.backupMode = true

// if we're in backup mode, spin off a request to see if service is restored
serviceAvailable().then(
yes => {
console.log('SERVER: Registry service now available, disabling backup mode 🎉')
runtime.setBackupMode(false)
},
no => {
console.log('Registry service still unavailable')
}
)
}

// forced cache clear option
if(!!query.flush) {
cache.flushAll()
}
Expand All @@ -128,17 +104,6 @@ export async function getServerSideProps(context) {
? { message: err }
: "Error"
res.statusCode = 404
serviceAvailable().then(
yes => {
console.log('Registry service available. Error was ' + props.error.message)
},
no => {
console.error(no)
console.log('SERVER: Registry service unavailable, switching to backup mode 🚨🚨🚨')
// Engineering node registry not available. Switch to backup mode
runtime.setBackupMode(true)
}
)
}

setTheme(props, context)
Expand Down
44 changes: 3 additions & 41 deletions pages/api/proxy/[...site].js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,13 @@ import { createProxyMiddleware } from 'http-proxy-middleware'
import runtime from 'services/runtime'

const localSolr = process.env.NEXT_PUBLIC_SUPPLEMENTAL_SOLR || 'https://sbnpds4.psi.edu/solr'
const remoteSolr = process.env.NEXT_PUBLIC_CORE_SOLR || 'https://pds.nasa.gov/services/search'

// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req, res, fn) {
// check in on backup mode
if(runtime.backupMode) {

// if we're in backup mode, spin off a request to see if service is restored
serviceAvailable().then(
yes => {
console.log('PROXY: Registry service now available, disabling backup mode 🎉')
runtime.setBackupMode(false)
},
no => {
console.log('Registry service still unavailable')
}
)
}

return new Promise((resolve, reject) => {
fn(req, res, (result) => {
if (result instanceof Error) {
serviceAvailable().then(
yes => {
console.log('PROXY: Registry service available. Error was ' + props.error.message)
},
no => {
console.error(no)
console.log('PROXY: Registry service unavailable, switching to backup mode 🚨🚨🚨')
// Engineering node registry not available. Switch to backup mode
runtime.setBackupMode(true)
}
)
return reject(result)
}

Expand All @@ -52,33 +25,22 @@ const rewriteWeb = (path) => {
let [collection, parameters] = cleanPath.split('?')
return `${collection}/select?${parameters}`
}
const rewriteBackupCore = (path) => {
const rewriteCore = (path) => {
// console.log('Rewriting backup core request to ' + path)
let parameters = path.replace('/api/proxy/core', '')
return `/pds-alias/select${parameters}`
}
const rewriteCore = (path) => {
// console.log('Rewriting core request to ' + path)
let parameters = path.replace('/api/proxy/core', '').replace('/api/proxy/heartbeat', '')
return `/search${parameters}`
}

const coreMiddleware = createProxyMiddleware({ target: remoteSolr, changeOrigin: true, pathRewrite: rewriteCore })
const backupCoreMiddleware = createProxyMiddleware({ target: localSolr, changeOrigin: true, on: { proxyReq: addAuthorization }, pathRewrite: rewriteBackupCore })
const coreMiddleware = createProxyMiddleware({ target: localSolr, changeOrigin: true, on: { proxyReq: addAuthorization }, pathRewrite: rewriteCore })
const webMiddleware = createProxyMiddleware({ target: localSolr, changeOrigin: true, on: { proxyReq: addAuthorization }, pathRewrite: rewriteWeb })

async function handler(req, res) {
// console.log('Backup mode: ' + runtime.backupMode)
const site = req.query.site[0]
switch(site) {
case 'internal': handleMessage(req.query.message); return res.status(200).send('OK')
case 'web': return runMiddleware(req, res, webMiddleware)
case 'heartbeat': return runMiddleware(req, res, coreMiddleware)
case 'core': {
return runtime.backupMode
? runMiddleware(req, res, backupCoreMiddleware)
: runMiddleware(req, res, coreMiddleware)
}
case 'core': return runMiddleware(req, res, coreMiddleware)
default: res.status(400).send("Invalid proxy request to site " + site)
}

Expand Down

0 comments on commit 4c270ca

Please sign in to comment.