-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: route for reset the jobs cache (#331)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
8a49e5b
commit 6845d85
Showing
6 changed files
with
115 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,4 @@ CRON_SECRET= | |
NEXT_PUBLIC_PLAUSIBLE_ADDRESS= | ||
|
||
REDIS_HOST= | ||
REDIS_PORT= | ||
REDIS_PORT= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Jobs Cache Reset Endpoint | ||
|
||
This endpoint allows resetting and repopulating the jobs cache in Redis. | ||
|
||
## Endpoint | ||
|
||
`GET /api/resetJobsCache` | ||
|
||
## Purpose | ||
|
||
To clear the existing jobs cache and repopulate it with up-to-date data from the database. This is useful when significant changes have been made to the data and you need these changes to be reflected immediately, without waiting for the natural expiration of the cache. | ||
|
||
## How to Use | ||
|
||
1. Make a GET request to `/api/resetJobsCache` with the authentication token. | ||
2. The endpoint will delete the existing cache, fetch new data from the database, and store it in Redis. | ||
|
||
## Parameters | ||
|
||
- `token` (required): Authentication token to access the endpoint. | ||
|
||
Example usage: | ||
|
||
``` | ||
https://www.trampardecasa.com.br/api/resetJobsCache?token=SECRET_KEY | ||
``` | ||
|
||
## Response | ||
|
||
- Success (200 OK): | ||
|
||
```json | ||
{ | ||
"message": "Cache reset and refilled successfully" | ||
} | ||
``` | ||
|
||
- Error (401 Unauthorized): | ||
|
||
```json | ||
{ | ||
"error": "Unauthorized" | ||
} | ||
``` | ||
|
||
- Error (500 Internal Server Error): | ||
```json | ||
{ | ||
"error": "Failed to reset and refill cache" | ||
} | ||
``` | ||
|
||
## Environment Variables | ||
|
||
Ensure the following environment variable is set: | ||
|
||
- `SECRET_KEY`: The secret token for authenticating reset requests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { NextResponse } from 'next/server' | ||
import getRedisClient from 'app/utils/getRedisClient' | ||
import { fetchJobs } from 'app/(roles)/vagas/action' | ||
|
||
const ONE_DAY_IN_MINUTES = 86_400 | ||
|
||
export async function GET(request: Request) { | ||
const { searchParams } = new URL(request.url) | ||
const token = searchParams.get('token') | ||
if (token !== process.env.SECRET_KEY) { | ||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
} | ||
|
||
try { | ||
const client = await getRedisClient() | ||
|
||
await client.del('web_jobs') | ||
|
||
const { data: fetchedJobs } = await fetchJobs([]) | ||
|
||
await client.set('web_jobs', JSON.stringify(fetchedJobs), { | ||
EX: ONE_DAY_IN_MINUTES, | ||
}) | ||
|
||
await client.quit() | ||
|
||
return NextResponse.json({ | ||
message: 'Cache reset and refilled successfully', | ||
}) | ||
} catch (error) { | ||
console.error('Failed to reset and refill cache:', error) | ||
return NextResponse.json( | ||
{ error: 'Failed to reset and refill cache' }, | ||
{ status: 500 } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters