简体   繁体   English

NodeJs 计划流程的最佳实践?

[英]NodeJs best practices for scheduled process?

I'm currently working on a large scale Tiktok like app where a video will be exist for 5 days.我目前正在开发一个类似 Tiktok 的大型应用程序,其中视频将存在 5 天。 My question how should I schedule to take down the video after 5 days?我的问题我应该如何安排在 5 天后删除视频? 2 Method that I came up are either I can build a background service worker that will continuously loop through the database and check if the video should be down or not OR I can put it in a setTimeOut() for 5 days, which means its gonna be on the RAM and there need to be a service that restart all the setTimeOut() in case the server is down. 2 我提出的方法是,我可以构建一个后台服务工作者,它会不断循环遍历数据库并检查视频是否应该关闭,或者我可以将其放入 setTimeOut() 5 天,这意味着它会在 RAM 上,并且需要有一个服务来重新启动所有 setTimeOut(),以防服务器关闭。 I'm using AWS EC2 so I wonder which method will be scalable and save some money.我正在使用 AWS EC2,所以我想知道哪种方法可以扩展并节省一些钱。 Thank you.谢谢你。

you could use queue management for storing tasks on it.您可以使用队列管理在其上存储任务。

suppose you have a queue that holds your tasks inside it.假设你有一个队列来保存你的任务。 you have a consumer that listens to your queue.你有一个消费者听你的队列。 if a message sends, the consumer reads the message and handles the job for you.如果消息发送,消费者会读取消息并为您处理工作。

but there is a challenge.但有一个挑战。 when you should send your task into the queue and when the consumer should handle the task.什么时候应该将任务发送到队列中,什么时候消费者应该处理任务。 for the availability part, message brokers are the good choices but managing the process is up to your approach, if you have a microservice system design, you can write your consumer as a service.对于可用性部分,消息代理是不错的选择,但管理流程取决于您的方法,如果您有微服务系统设计,则可以将消费者编写为服务。

rabbit-MQ and kafka are the most popular message brokers. rabbit-MQkafka是最受欢迎的消息代理。

of course, you should know about task queue and message queues concepts.当然,您应该了解任务队列消息队列的概念。

take a look at the difference:看看区别:

https://serverfault.com/questions/669434/whats-the-difference-between-a-task-queue-and-a-message-queue-in-rabbitmq https://serverfault.com/questions/669434/whats-the-difference-between-a-task-queue-and-a-message-queue-in-rabbitmq

https://www.tech101.in/message-queue-and-task-queue/ https://www.tech101.in/message-queue-and-task-queue/

there is a simple task queue package that works with Redis :有一个适用于 Redis 的简单任务队列包:

bee-queue蜂队

note that, handling the message brokers are complicated in the production level.请注意,在生产级别处理消息代理很复杂。

and you should know about cron systems too:你也应该了解 cron 系统:

https://en.wikipedia.org/wiki/Cron https://en.wikipedia.org/wiki/Cron

https://www.redhat.com/sysadmin/automate-linux-tasks-cron https://www.redhat.com/sysadmin/automate-linux-tasks-cron

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM