Skip to content

Commit

Permalink
add schedule admin
Browse files Browse the repository at this point in the history
  • Loading branch information
mfornos committed Nov 25, 2024
1 parent 9dd27c0 commit 8692f3f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
52 changes: 52 additions & 0 deletions packages/server/src/services/admin/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,58 @@ describe('admin api', () => {
})
})

it('should delete a sublevel', async () => {
const db = server.levelDB.sublevel('deleteme')
await db.put('a', '1')
expect(await db.get('a')).toBe('1')

await new Promise<void>((resolve) => {
server.inject(
{
method: 'DELETE',
url: '/admin/level/deleteme',
headers: {
authorization: `Bearer ${rootToken}`,
},
},
(_err, response) => {
expect(response?.statusCode).toStrictEqual(200)
resolve()
},
)
})

expect(db.get('a')).rejects.toThrow()
})

it('should schedule a task', async () => {
const scheduleSpy = vi.spyOn(server.scheduler, 'schedule')
const timeString = new Date(Date.now() + 10_000_000).toISOString()
const key = timeString + 'something'

await new Promise<void>((resolve) => {
server.inject(
{
method: 'POST',
url: '/admin/sched',
headers: {
authorization: `Bearer ${rootToken}`,
},
body: {
key,
type: 'something',
task: null,
},
},
(_err, response) => {
expect(response?.statusCode).toStrictEqual(200)
expect(scheduleSpy).toHaveBeenCalled()
resolve()
},
)
})
})

it('should retrieve pending scheduler tasks', async () => {
const allTaskTimesSpy = vi.spyOn(server.scheduler, 'allTaskTimes')

Expand Down
18 changes: 18 additions & 0 deletions packages/server/src/services/admin/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FastifyInstance } from 'fastify'
import fp from 'fastify-plugin'

import { CAP_ADMIN } from '../auth/index.js'
import { Scheduled } from '../persistence/level/index.js'
import { NetworkURN, jsonEncoded, prefixes } from '../types.js'

type chainIdParam = {
Expand All @@ -26,6 +27,15 @@ async function AdminRoutes(api: FastifyInstance) {
},
}

api.delete<{
Params: {
prefix: string
}
}>('/admin/level/:prefix', opts, async (request, reply) => {
await rootStore.sublevel(request.params.prefix).clear()
reply.send()
})

api.get<chainIdParam>('/admin/cache/:chainId', opts, async (request, reply) => {
const db = rootStore.sublevel<string, any>(prefixes.cache.family(request.params.chainId), jsonEncoded)
reply.send(await db.iterator(itOps).all())
Expand All @@ -37,6 +47,14 @@ async function AdminRoutes(api: FastifyInstance) {
reply.send()
})

api.post<{
Body: Scheduled
}>('/admin/sched', opts, async (request, reply) => {
const task = request.body
await scheduler.schedule(task)
reply.send()
})

api.get<{
Querystring: { key?: string }
}>('/admin/sched', opts, async (request, reply) => {
Expand Down

0 comments on commit 8692f3f

Please sign in to comment.