简体   繁体   English

分布式环境中的任务执行

[英]Task execution in distributed environment

I have a table say employee(id, name, email, is_email_verified, created_date). 我有一张桌子说员工(id,姓名,电子邮件,is_email_verified,created_date)。 I need to send an email everyday for the users which has been created and email is not verified. 我需要每天为已创建的用户发送一封电子邮件,并且电子邮件未经验证。 I need to create a service which can be deployed on two boxes. 我需要创建一个可以部署在两个盒子上的服务。 It will read the user table and get the user id and send the mail for verification. 它将读取用户表并获取用户ID,然后发送邮件进行验证。 Since, there are two boxes and each will read the same number of users and hence will send the two mails. 由于有两个框,每个框将读取相同数量的用户,因此将发送两个邮件。

How, can we make this service so as user ids will get distributed among the servers (if I have n servers). 我们如何做这项服务,以便用户ID在服务器之间分配(如果我有n台服务器)。

There are few ways to do that. 有几种方法可以做到这一点。

The most straightforward & reliable: 最简单,最可靠:

  1. Create a job task that read all user ID from the table and queue them to a task queue - 1 task per user ID. 创建一个作业任务,该任务从表中读取所有用户ID并将它们排入任务队列-每个用户ID 1个任务。
  2. Pull task from queue using as many servers as you want (1-N). 使用所需数量的服务器(1-N)从队列中拉任务。

Not requiring queues (not fault tolerant): 不需要队列(不容错):

Choose a hash function that would decide which user IDs should server process. 选择一个散列函数,该函数将决定服务器应处理哪些用户标识。 For example 1st server processes odd IDs and 2nd even IDs, etc. Or something like: 例如,第一台服务器处理奇数ID,第二台偶数ID等。

def should_i_send(user_id, server_id):
  return user_id % server_id == 0

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

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