mongodb-driver-startup.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { Meteor } from 'meteor/meteor';
  2. import { mongodbConnectionManager } from '/models/lib/mongodbConnectionManager';
  3. import { mongodbDriverManager } from '/models/lib/mongodbDriverManager';
  4. import { meteorMongoIntegration } from '/models/lib/meteorMongoIntegration';
  5. /**
  6. * MongoDB Driver Startup
  7. *
  8. * This module initializes the MongoDB driver system on server startup,
  9. * providing automatic version detection and driver selection for
  10. * MongoDB versions 3.0 through 8.0.
  11. */
  12. // Initialize MongoDB driver system on server startup
  13. Meteor.startup(async function() {
  14. // MongoDB Driver System Startup (status available in Admin Panel)
  15. try {
  16. // Check if MONGO_URL is available
  17. const mongoUrl = process.env.MONGO_URL;
  18. if (!mongoUrl) {
  19. // MONGO_URL not found, skipping MongoDB driver initialization
  20. return;
  21. }
  22. // MONGO_URL found, initializing MongoDB driver system
  23. // Connection string: (credentials hidden for security)
  24. // Initialize the Meteor integration
  25. meteorMongoIntegration.initialize(mongoUrl);
  26. // Test the connection
  27. const testResult = await meteorMongoIntegration.testConnection();
  28. if (testResult.success) {
  29. // MongoDB connection test successful
  30. // Driver and version information available in Admin Panel
  31. } else {
  32. // MongoDB connection test failed
  33. // Error details available in Admin Panel
  34. }
  35. // Connection statistics available in Admin Panel
  36. const stats = meteorMongoIntegration.getStats();
  37. // Driver compatibility information available in Admin Panel
  38. const supportedVersions = mongodbDriverManager.getSupportedVersions();
  39. // MongoDB Driver System Ready (status available in Admin Panel)
  40. } catch (error) {
  41. console.error('Error during MongoDB driver system startup:', error.message);
  42. console.error('Stack trace:', error.stack);
  43. // Don't fail the entire startup, just log the error
  44. console.log('Continuing with default MongoDB connection...');
  45. }
  46. });
  47. // Add server-side methods for debugging and monitoring
  48. if (Meteor.isServer) {
  49. // Method to get MongoDB driver statistics
  50. Meteor.methods({
  51. 'mongodb-driver-stats': function() {
  52. if (!this.userId) {
  53. throw new Meteor.Error('not-authorized', 'Must be logged in');
  54. }
  55. return {
  56. connectionStats: mongodbConnectionManager.getConnectionStats(),
  57. driverStats: mongodbDriverManager.getConnectionStats(),
  58. integrationStats: meteorMongoIntegration.getStats()
  59. };
  60. },
  61. 'mongodb-driver-test-connection': async function() {
  62. if (!this.userId) {
  63. throw new Meteor.Error('not-authorized', 'Must be logged in');
  64. }
  65. return await meteorMongoIntegration.testConnection();
  66. },
  67. 'mongodb-driver-reset': function() {
  68. if (!this.userId) {
  69. throw new Meteor.Error('not-authorized', 'Must be logged in');
  70. }
  71. meteorMongoIntegration.reset();
  72. return { success: true, message: 'MongoDB driver system reset' };
  73. },
  74. 'mongodb-driver-supported-versions': function() {
  75. if (!this.userId) {
  76. throw new Meteor.Error('not-authorized', 'Must be logged in');
  77. }
  78. return {
  79. supportedVersions: mongodbDriverManager.getSupportedVersions(),
  80. compatibility: mongodbDriverManager.getSupportedVersions().map(version => {
  81. const driverInfo = mongodbDriverManager.getDriverInfo(
  82. mongodbDriverManager.getDriverForVersion(version)
  83. );
  84. return {
  85. version,
  86. driver: driverInfo?.driver || 'unknown',
  87. driverVersion: driverInfo?.version || 'unknown',
  88. minServer: driverInfo?.minServer || 'unknown',
  89. maxServer: driverInfo?.maxServer || 'unknown'
  90. };
  91. })
  92. };
  93. }
  94. });
  95. // Add a publication for real-time monitoring
  96. Meteor.publish('mongodb-driver-monitor', function() {
  97. if (!this.userId) {
  98. throw new Meteor.Error('not-authorized', 'Must be logged in');
  99. }
  100. const self = this;
  101. // Send initial data
  102. const stats = meteorMongoIntegration.getStats();
  103. self.added('mongodbDriverMonitor', 'stats', stats);
  104. // Update every 30 seconds
  105. const interval = setInterval(() => {
  106. const updatedStats = meteorMongoIntegration.getStats();
  107. self.changed('mongodbDriverMonitor', 'stats', updatedStats);
  108. }, 30000);
  109. // Clean up on unsubscribe
  110. self.onStop(() => {
  111. clearInterval(interval);
  112. });
  113. self.ready();
  114. });
  115. }
  116. // Export for use in other modules
  117. export { mongodbConnectionManager, mongodbDriverManager, meteorMongoIntegration };