check-connection.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const { RedisConnection } = require('bullmq');
  2. const { redisHost, redisPort } = require('./constants');
  3. const connection = new RedisConnection({
  4. port: redisPort,
  5. host: redisHost,
  6. });
  7. let timer;
  8. /**
  9. * When contributing to Vendure, developers who made changes unrelated to
  10. * this plugin should not be expected to set up an Redis instance
  11. * locally just so they can get the pre-push hook to pass. So if no
  12. * instance is available, we skip the tests.
  13. */
  14. function checkConnection() {
  15. return new Promise(async (resolve, reject) => {
  16. try {
  17. timer = setTimeout(() => {
  18. logConnectionFailure();
  19. process.exit(0);
  20. }, 5000);
  21. connection.on('error', err => {
  22. resolve(0);
  23. });
  24. const client = await connection.client;
  25. clearTimeout(timer);
  26. await client.ping((err, result) => {
  27. if (err) {
  28. resolve(0);
  29. } else {
  30. // If the connection is available, we exit with 1 in order to invoke the
  31. // actual e2e test script (since we are using the `||` operator in the "e2e" script)
  32. resolve(1);
  33. }
  34. });
  35. } catch (e) {
  36. logConnectionFailure();
  37. // If no redis available, we exit with 0 so that the npm script
  38. // exits
  39. resolve(0);
  40. }
  41. });
  42. }
  43. checkConnection().then(result => {
  44. process.exit(result);
  45. });
  46. function logConnectionFailure() {
  47. console.log(`Could not connect to Redis instance at "${redisHost}:${redisPort}"`);
  48. console.log(`Skipping e2e tests for BullMQJobQueuePlugin`);
  49. process.env.SKIP_ELASTICSEARCH_E2E_TESTS = true;
  50. }