index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. require('dotenv').config();
  2. const Fastify = require('fastify');
  3. const { Queue, Worker, QueueEvents } = require('bullmq');
  4. const IORedis = require('ioredis');
  5. const axios = require('axios');
  6. const { getDb, connect } = require('./utils/MongoDBConnector');
  7. const { createLogger } = require('./utils/logger');
  8. const REDIS_URL = process.env.REDIS_URL || 'redis://redis:6379';
  9. const PLATFORM_SERVICES = {
  10. twitter: process.env.TWITTER_SERVICE_URL || 'http://twitter:3001',
  11. linkedin: process.env.LINKEDIN_SERVICE_URL || 'http://linkedin:3002',
  12. mastodon: process.env.MASTODON_SERVICE_URL || 'http://mastodon:3003',
  13. bluesky: process.env.BLUESKY_SERVICE_URL || 'http://bluesky:3004',
  14. instagram: process.env.INSTAGRAM_SERVICE_URL || 'http://instagram:3005',
  15. facebook: process.env.FACEBOOK_SERVICE_URL || 'http://facebook:3006',
  16. };
  17. const log = createLogger('scheduler');
  18. const app = Fastify({ logger: log });
  19. let postQueue;
  20. let redis;
  21. // ─── Job Worker ──────────────────────────────────────────────────────────────
  22. async function processPostJob(job) {
  23. // destinations: [{ platform, accountId?, imageUrl?, videoUrl?, link? }]
  24. // Falls back to legacy { platforms: string[] } format
  25. const { postId, content, destinations, platforms, media = [] } = job.data;
  26. const destList = destinations || (platforms || []).map((p) => ({ platform: p }));
  27. log.info({ action: 'job_process', jobId: job.id, attempt: job.attemptsMade + 1, destinations: destList.map((d) => d.accountId ? `${d.platform}:${d.accountId}` : d.platform) });
  28. const db = await getDb();
  29. // Load any results already recorded from previous attempts so we can skip
  30. // destinations that already succeeded — preventing duplicate posts on retry.
  31. const existingPost = postId ? await db.collection('posts').findOne({ _id: postId }, { projection: { platformResults: 1 } }) : null;
  32. const results = { ...(existingPost?.platformResults || {}) };
  33. for (const dest of destList) {
  34. const { platform, accountId, imageUrl, videoUrl, link } = dest;
  35. const resultKey = accountId ? `${platform}:${accountId}` : platform;
  36. if (results[resultKey]?.success) {
  37. log.info({ action: 'job_skip_dest', jobId: job.id, destination: resultKey, reason: 'already_published' });
  38. continue;
  39. }
  40. const serviceUrl = PLATFORM_SERVICES[platform];
  41. if (!serviceUrl) {
  42. results[resultKey] = { success: false, error: 'Unknown platform' };
  43. continue;
  44. }
  45. try {
  46. const response = await axios.post(`${serviceUrl}/post`, { content, accountId, imageUrl, videoUrl, link, media }, { timeout: 30000 });
  47. results[resultKey] = { success: true, ...response.data.result };
  48. } catch (err) {
  49. results[resultKey] = { success: false, error: err.message };
  50. }
  51. }
  52. // MongoDB güncelle
  53. await db.collection('posts').updateOne(
  54. { _id: postId },
  55. {
  56. $set: {
  57. status: Object.values(results).every((r) => r.success) ? 'published' : 'failed',
  58. publishedAt: new Date(),
  59. platformResults: results,
  60. },
  61. }
  62. );
  63. await db.collection('scheduled_jobs').updateOne(
  64. { bullJobId: String(job.id) },
  65. {
  66. $set: {
  67. status: 'completed',
  68. completedAt: new Date(),
  69. },
  70. }
  71. );
  72. return results;
  73. }
  74. // ─── HTTP Endpoints ──────────────────────────────────────────────────────────
  75. app.get('/health', async () => ({ status: 'ok', service: 'scheduler' }));
  76. // Create a scheduled post.
  77. // Body: { content, scheduledAt, destinations: [{ platform, accountId?, imageUrl?, videoUrl?, link? }] }
  78. // Legacy { platforms: string[] } still accepted for backwards compatibility.
  79. app.post('/schedule', async (request, reply) => {
  80. const { postId, content, destinations, platforms, scheduledAt, media = [] } = request.body;
  81. const destList = destinations || (platforms || []).map((p) => ({ platform: p }));
  82. if (!content || !destList.length || !scheduledAt) {
  83. return reply.code(400).send({ error: 'content, destinations, and scheduledAt are required' });
  84. }
  85. const delay = new Date(scheduledAt).getTime() - Date.now();
  86. if (delay < 0) {
  87. return reply.code(400).send({ error: 'scheduledAt must be in the future' });
  88. }
  89. const job = await postQueue.add(
  90. 'scheduled-post',
  91. { postId, content, destinations: destList, media },
  92. { delay, attempts: 3, backoff: { type: 'exponential', delay: 60000 } }
  93. );
  94. const db = await getDb();
  95. await db.collection('scheduled_jobs').insertOne({
  96. postId,
  97. type: 'one-time',
  98. scheduledAt: new Date(scheduledAt),
  99. destinations: destList,
  100. status: 'pending',
  101. attempts: 0,
  102. maxAttempts: 3,
  103. bullJobId: String(job.id),
  104. createdAt: new Date(),
  105. });
  106. return { success: true, jobId: job.id, scheduledAt };
  107. });
  108. // Zamanlanmış görevleri listele
  109. app.get('/jobs', async (request) => {
  110. const { status = 'pending' } = request.query;
  111. const db = await getDb();
  112. const jobs = await db
  113. .collection('scheduled_jobs')
  114. .find({ status })
  115. .sort({ scheduledAt: 1 })
  116. .toArray();
  117. return { success: true, count: jobs.length, jobs };
  118. });
  119. // Görevi iptal et
  120. app.delete('/jobs/:jobId', async (request, reply) => {
  121. const { jobId } = request.params;
  122. const job = await postQueue.getJob(jobId);
  123. if (!job) return reply.code(404).send({ error: 'Job bulunamadı' });
  124. await job.remove();
  125. const db = await getDb();
  126. await db.collection('scheduled_jobs').updateOne(
  127. { bullJobId: jobId },
  128. { $set: { status: 'cancelled' } }
  129. );
  130. return { success: true, jobId };
  131. });
  132. // ─── Başlatma ────────────────────────────────────────────────────────────────
  133. async function start() {
  134. await connect();
  135. redis = new IORedis(REDIS_URL, { maxRetriesPerRequest: null });
  136. postQueue = new Queue('post-queue', { connection: redis });
  137. const worker = new Worker('post-queue', processPostJob, { connection: redis });
  138. worker.on('failed', (job, err) => {
  139. log.error({ action: 'job_process', jobId: job?.id, outcome: 'failure', err: err.message });
  140. });
  141. await app.listen({ port: process.env.PORT || 3011, host: '0.0.0.0' });
  142. log.info({ action: 'service_start', port: 3011, outcome: 'success' }, 'Scheduler started');
  143. }
  144. start().catch((err) => { log.error({ action: 'service_start', outcome: 'failure', err: err.message }); process.exit(1); });