fill-buffer-plugin.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { Mutation, Query, Resolver } from '@nestjs/graphql';
  2. import {
  3. CollectionService,
  4. Ctx,
  5. Logger,
  6. PluginCommonModule,
  7. ProductVariantService,
  8. RequestContext,
  9. VendurePlugin,
  10. } from '@vendure/core';
  11. import gql from 'graphql-tag';
  12. @Resolver()
  13. class FillBufferResolver {
  14. constructor(
  15. private productVariantService: ProductVariantService,
  16. private collectionService: CollectionService,
  17. ) {}
  18. @Mutation()
  19. async fillBuffer(@Ctx() ctx: RequestContext) {
  20. const { items: variants } = await this.productVariantService.findAll(ctx);
  21. const { items: collections } = await this.collectionService.findAll(ctx);
  22. const limit = 2000;
  23. for (let i = 0; i < limit; i++) {
  24. const variant = variants[i % variants.length];
  25. await this.productVariantService.update(ctx, [
  26. {
  27. id: variant.id,
  28. enabled: !variant.enabled,
  29. },
  30. ]);
  31. const collection = collections[i % collections.length];
  32. await this.collectionService.update(ctx, {
  33. id: collection.id,
  34. isPrivate: !collection.isPrivate,
  35. });
  36. if (i % 100 === 0) {
  37. Logger.info(`Updated ${i} / ${limit} items...`);
  38. }
  39. }
  40. Logger.info(`Done!`);
  41. return true;
  42. }
  43. }
  44. /**
  45. * Plugin to create a lot of buffered jobs to test help investigate and fix
  46. * issue https://github.com/vendure-ecommerce/vendure/issues/1433
  47. */
  48. @VendurePlugin({
  49. imports: [PluginCommonModule],
  50. adminApiExtensions: {
  51. schema: gql`
  52. extend type Mutation {
  53. fillBuffer: Boolean!
  54. }
  55. `,
  56. resolvers: [FillBufferResolver],
  57. },
  58. })
  59. export class FillBufferPlugin {}