cache-sync-debug.controller.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // apps/box-mgnt-api/src/cache-sync/cache-sync-debug.controller.ts
  2. import {
  3. Body,
  4. Controller,
  5. DefaultValuePipe,
  6. Logger,
  7. ParseIntPipe,
  8. Post,
  9. Query,
  10. } from '@nestjs/common';
  11. import { CacheSyncService } from './cache-sync.service';
  12. import { IsOptional, IsString } from 'class-validator';
  13. class ScheduleAdRefreshDto {
  14. @IsString()
  15. adId!: string;
  16. @IsOptional()
  17. @IsString()
  18. adType?: string;
  19. }
  20. @Controller('mgnt-debug/cache-sync')
  21. export class CacheSyncDebugController {
  22. private readonly logger = new Logger(CacheSyncDebugController.name);
  23. constructor(private readonly cacheSyncService: CacheSyncService) {}
  24. /**
  25. * POST /mgnt-debug/cache-sync/channel/refresh-all
  26. * Schedules a CHANNEL + REFRESH_ALL action.
  27. */
  28. @Post('channel/refresh-all')
  29. async scheduleChannelRefreshAll() {
  30. await this.cacheSyncService.scheduleChannelRefreshAll();
  31. this.logger.log('Scheduled CHANNEL REFRESH_ALL');
  32. return { ok: true, message: 'Scheduled CHANNEL REFRESH_ALL' };
  33. }
  34. /**
  35. * POST /mgnt-debug/cache-sync/category/refresh-all
  36. * Schedules a CATEGORY + REFRESH_ALL action.
  37. */
  38. @Post('category/refresh-all')
  39. async scheduleCategoryRefreshAll() {
  40. await this.cacheSyncService.scheduleCategoryRefreshAll();
  41. this.logger.log('Scheduled CATEGORY REFRESH_ALL');
  42. return { ok: true, message: 'Scheduled CATEGORY REFRESH_ALL' };
  43. }
  44. /**
  45. * POST /mgnt-debug/cache-sync/ad/refresh
  46. * Body: { adId: number; adType?: string }
  47. * Schedules AD REFRESH (+ optional AD_POOL REBUILD_POOL if adType provided).
  48. */
  49. @Post('ad/refresh')
  50. async scheduleAdRefresh(@Body() dto: ScheduleAdRefreshDto) {
  51. const { adId, adType } = dto;
  52. await this.cacheSyncService.scheduleAdRefresh(adId, adType);
  53. this.logger.log(
  54. `Scheduled AD REFRESH for adId=${adId}, adType=${adType ?? 'N/A'}`,
  55. );
  56. return {
  57. ok: true,
  58. message: 'Scheduled AD REFRESH (and pool rebuild if adType provided)',
  59. adId,
  60. adType: adType ?? null,
  61. };
  62. }
  63. /**
  64. * POST /mgnt-debug/cache-sync/process-once?limit=20
  65. * Manually processes pending actions (single batch).
  66. */
  67. @Post('process-once')
  68. async processOnce(
  69. @Query('limit', new DefaultValuePipe(20), ParseIntPipe) limit: number,
  70. ) {
  71. await this.cacheSyncService.processPendingOnce(limit);
  72. return {
  73. ok: true,
  74. message: `Processed up to ${limit} pending actions (see logs).`,
  75. };
  76. }
  77. }