Files
Socialflow/server/services/scheduler.ts
T
michaelschal f881f668a6 Add Docker support and installation guide for easy server deployment
Includes a Dockerfile for building the application image and an INSTALLATION.md file detailing the setup process, prerequisites, and helpful commands for managing the application on a private server.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: ae4037a0-2a6f-4530-9bac-79b543286bda
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/397bca8c-984f-43ff-841a-10897aeb8140/ae4037a0-2a6f-4530-9bac-79b543286bda/ds3R7vP
2025-10-01 09:52:59 +00:00

70 lines
2.0 KiB
TypeScript

import cron, { ScheduledTask } from "node-cron";
import { storage } from "../storage";
export class SchedulerService {
private task: ScheduledTask | null = null;
start() {
// Run every minute to check for pending posts
this.task = cron.schedule("* * * * *", async () => {
await this.processPendingPosts();
});
console.log("Scheduler service started");
}
stop() {
if (this.task) {
this.task.stop();
console.log("Scheduler service stopped");
}
}
private async processPendingPosts() {
try {
const pendingPosts = await storage.getPendingScheduledPosts();
for (const scheduledPost of pendingPosts) {
try {
await this.publishPost(scheduledPost);
} catch (error) {
console.error(`Error publishing post ${scheduledPost.id}:`, error);
await storage.updateScheduledPost(scheduledPost.id, {
error: error instanceof Error ? error.message : "Unknown error",
});
}
}
} catch (error) {
console.error("Error processing pending posts:", error);
}
}
private async publishPost(scheduledPost: any) {
const post = await storage.getPost(scheduledPost.postId);
const page = await storage.getSocialPage(scheduledPost.pageId);
if (!post || !page) {
throw new Error("Post or page not found");
}
// In a real implementation, this would call Facebook/Instagram Graph API
// For now, we'll simulate the publishing
console.log(`Publishing post ${post.id} to ${page.platform} page ${page.pageName}`);
// Update the scheduled post as published
await storage.updateScheduledPost(scheduledPost.id, {
publishedAt: new Date(),
externalPostId: `simulated-${Date.now()}`, // In real app, this would be the ID from FB/IG
});
// Update post status
await storage.updatePost(post.id, {
status: "published",
});
console.log(`Successfully published post ${post.id}`);
}
}
export const schedulerService = new SchedulerService();