update content-fetch
This commit is contained in:
@ -22,11 +22,10 @@ import {
|
||||
PageType,
|
||||
Profile,
|
||||
Recommendation,
|
||||
ResolverFn,
|
||||
SearchItem,
|
||||
} from '../generated/graphql'
|
||||
import { createPubSubClient } from '../pubsub'
|
||||
import { Claims, WithDataSourcesContext } from '../resolvers/types'
|
||||
import { redisDataSource } from '../redis_data_source'
|
||||
import { validateUrl } from '../services/create_page_save_request'
|
||||
import { updateLibraryItem } from '../services/library_item'
|
||||
import { Merge } from '../util'
|
||||
@ -391,17 +390,10 @@ export const setRecentlySavedItemInRedis = async (
|
||||
url: string
|
||||
) => {
|
||||
// save the url in redis for 26 hours so rss-feeder won't try to re-save it
|
||||
if (!redisClient) {
|
||||
console.info(
|
||||
'not setting recently saved item because redis is not configured'
|
||||
)
|
||||
return
|
||||
}
|
||||
// save the url in redis for 8 hours so rss-feeder won't try to re-save it
|
||||
const redisKey = `recent-saved-item:${userId}:${url}`
|
||||
const ttlInSeconds = 60 * 60 * 26
|
||||
try {
|
||||
return redisClient.set(redisKey, 1, 'EX', ttlInSeconds, 'NX')
|
||||
return await redisClient.set(redisKey, 1, 'EX', ttlInSeconds, 'NX')
|
||||
} catch (error) {
|
||||
logger.error('error setting recently saved item in redis', {
|
||||
redisKey,
|
||||
|
||||
@ -1,8 +1,12 @@
|
||||
import Redis, { RedisOptions } from 'ioredis'
|
||||
|
||||
type RedisClientType = 'cache' | 'mq'
|
||||
type RedisDataSourceOption = {
|
||||
url?: string
|
||||
cert?: string
|
||||
}
|
||||
export type RedisDataSourceOptions = {
|
||||
REDIS_URL?: string
|
||||
REDIS_CERT?: string
|
||||
[key in RedisClientType]: RedisDataSourceOption
|
||||
}
|
||||
|
||||
export class RedisDataSource {
|
||||
@ -14,8 +18,12 @@ export class RedisDataSource {
|
||||
constructor(options: RedisDataSourceOptions) {
|
||||
this.options = options
|
||||
|
||||
this.cacheClient = createRedisClient('cache', this.options)
|
||||
this.queueRedisClient = createRedisClient('queue', this.options)
|
||||
const cacheClient = createIORedisClient('cache', this.options)
|
||||
if (!cacheClient) throw 'Error initializing cache redis client'
|
||||
|
||||
this.cacheClient = cacheClient
|
||||
this.queueRedisClient =
|
||||
createIORedisClient('mq', this.options) || this.cacheClient // if mq is not defined, use cache
|
||||
}
|
||||
|
||||
setOptions(options: RedisDataSourceOptions): void {
|
||||
@ -32,46 +40,45 @@ export class RedisDataSource {
|
||||
}
|
||||
}
|
||||
|
||||
const createRedisClient = (name: string, options: RedisDataSourceOptions) => {
|
||||
const redisURL = options.REDIS_URL
|
||||
const cert = options.REDIS_CERT?.replace(/\\n/g, '\n') // replace \n with new line
|
||||
const createIORedisClient = (
|
||||
name: RedisClientType,
|
||||
options: RedisDataSourceOptions
|
||||
): Redis | undefined => {
|
||||
const option = options[name]
|
||||
const redisURL = option.url
|
||||
if (!redisURL) {
|
||||
throw 'Error: no redisURL supplied'
|
||||
console.log(`no redisURL supplied: ${name}`)
|
||||
return undefined
|
||||
}
|
||||
|
||||
const redisOptions: RedisOptions = {
|
||||
name,
|
||||
connectTimeout: 10000, // 10 seconds
|
||||
tls: cert
|
||||
const redisCert = option.cert
|
||||
const tls =
|
||||
redisURL.startsWith('rediss://') && redisCert
|
||||
? {
|
||||
cert,
|
||||
rejectUnauthorized: false, // for self-signed certs
|
||||
ca: redisCert,
|
||||
rejectUnauthorized: false,
|
||||
}
|
||||
: undefined,
|
||||
: undefined
|
||||
|
||||
const redisOptions: RedisOptions = {
|
||||
tls,
|
||||
name,
|
||||
connectTimeout: 10000,
|
||||
maxRetriesPerRequest: null,
|
||||
offlineQueue: false,
|
||||
}
|
||||
|
||||
const redis = new Redis(redisURL, redisOptions)
|
||||
|
||||
redis.on('connect', () => {
|
||||
console.log('Redis connected', name)
|
||||
})
|
||||
|
||||
redis.on('error', (err) => {
|
||||
console.error('Redis error', err, name)
|
||||
})
|
||||
|
||||
redis.on('close', () => {
|
||||
console.log('Redis closed', name)
|
||||
})
|
||||
|
||||
return redis
|
||||
return new Redis(redisURL, redisOptions)
|
||||
}
|
||||
|
||||
export const redisDataSource = new RedisDataSource({
|
||||
REDIS_URL: process.env.REDIS_URL,
|
||||
REDIS_CERT: process.env.REDIS_CERT,
|
||||
cache: {
|
||||
url: process.env.REDIS_CACHE_URL,
|
||||
cert: process.env.REDIS_CACHE_CERT,
|
||||
},
|
||||
mq: {
|
||||
url: process.env.MQ_REDIS_URL,
|
||||
cert: process.env.MQ_REDIS_CERT,
|
||||
},
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||
|
||||
Reference in New Issue
Block a user