test-with-logging.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // 测试并监控日志
  2. const { spawn } = require('child_process');
  3. const fs = require('fs');
  4. const path = require('path');
  5. // 生成有效的JWT token
  6. function generateTestToken() {
  7. const crypto = require('crypto');
  8. const header = Buffer.from(JSON.stringify({
  9. alg: 'HS256',
  10. typ: 'JWT'
  11. })).toString('base64url');
  12. const payload = Buffer.from(JSON.stringify({
  13. id: 1,
  14. username: 'test',
  15. tenantId: 1,
  16. iat: Math.floor(Date.now() / 1000),
  17. exp: Math.floor(Date.now() / 1000) + 3600
  18. })).toString('base64url');
  19. const secret = 'your-secret-key';
  20. const signature = crypto
  21. .createHmac('sha256', secret)
  22. .update(`${header}.${payload}`)
  23. .digest('base64url');
  24. return `${header}.${payload}.${signature}`;
  25. }
  26. async function testAndMonitor() {
  27. console.log('=== 测试支付成功触发API并监控日志 ===\n');
  28. const token = generateTestToken();
  29. const apiUrl = 'http://localhost:8080/api/v1/payments/payment/trigger-success';
  30. const testOrderId = 25;
  31. // 创建一个日志文件来记录
  32. const logFile = path.join(__dirname, 'test-log.txt');
  33. console.log(`日志将记录到: ${logFile}\n`);
  34. // 开始监控应用程序日志
  35. const appPid = 16074; // 现有的应用程序进程ID
  36. console.log(`监控应用程序进程ID: ${appPid}`);
  37. // 测试API
  38. console.log(`测试API: ${apiUrl}`);
  39. console.log(`订单ID: ${testOrderId}`);
  40. console.log(`Token: ${token.substring(0, 30)}...\n`);
  41. try {
  42. const response = await fetch(apiUrl, {
  43. method: 'POST',
  44. headers: {
  45. 'Content-Type': 'application/json',
  46. 'Authorization': `Bearer ${token}`
  47. },
  48. body: JSON.stringify({
  49. orderId: testOrderId
  50. })
  51. });
  52. console.log(`状态码: ${response.status}`);
  53. console.log(`状态文本: ${response.statusText}`);
  54. if (response.ok) {
  55. const result = await response.json();
  56. console.log('成功响应:', JSON.stringify(result, null, 2));
  57. console.log(`\n✅ API调用成功`);
  58. // 等待一会儿,让应用程序处理
  59. console.log('\n等待3秒让应用程序处理...');
  60. await new Promise(resolve => setTimeout(resolve, 3000));
  61. // 检查数据库
  62. console.log('\n检查数据库...');
  63. const { exec } = require('child_process');
  64. exec(
  65. 'psql -h 127.0.0.1 -U postgres -d postgres -c "SELECT id, tenant_id, task_id, order_id, print_status, scheduled_at, created_at FROM feie_print_task_mt WHERE order_id = 25 ORDER BY created_at DESC"',
  66. (error, stdout, stderr) => {
  67. if (error) {
  68. console.error('查询数据库失败:', error.message);
  69. } else {
  70. console.log('订单ID 25的打印任务:');
  71. console.log(stdout);
  72. if (stdout.includes('(0 rows)')) {
  73. console.log('❌ 数据库中没有找到打印任务');
  74. console.log('\n可能的原因:');
  75. console.log('1. 飞鹅打印模块导入失败');
  76. console.log('2. PrintTriggerService处理失败');
  77. console.log('3. 数据库插入失败');
  78. console.log('4. 事务回滚');
  79. } else {
  80. console.log('✅ 数据库中找到打印任务');
  81. }
  82. }
  83. }
  84. );
  85. } else {
  86. const errorText = await response.text();
  87. console.log('错误响应:', errorText);
  88. console.log(`\n❌ API调用失败`);
  89. }
  90. } catch (error) {
  91. console.error('请求失败:', error.message);
  92. }
  93. // 检查应用程序的stderr输出
  94. console.log('\n=== 检查应用程序日志 ===');
  95. console.log('尝试读取应用程序的stderr...');
  96. // 尝试通过/proc文件系统读取进程的输出
  97. const stderrFile = `/proc/${appPid}/fd/2`;
  98. if (fs.existsSync(stderrFile)) {
  99. try {
  100. const stderrContent = fs.readFileSync(stderrFile, 'utf8');
  101. console.log('应用程序stderr输出:');
  102. console.log(stderrContent.substring(-500)); // 最后500个字符
  103. } catch (err) {
  104. console.log('无法读取stderr:', err.message);
  105. }
  106. } else {
  107. console.log('stderr文件不存在');
  108. }
  109. // 检查系统日志
  110. console.log('\n=== 检查系统日志 ===');
  111. exec('dmesg | tail -20', (error, stdout, stderr) => {
  112. if (!error) {
  113. console.log('系统日志最后20行:');
  114. console.log(stdout);
  115. }
  116. });
  117. }
  118. // 运行测试
  119. testAndMonitor().catch(error => {
  120. console.error('测试脚本执行失败:', error);
  121. });