序
本文主要研究一下PowerJob的UserInfoController
UserInfoController
tech/powerjob/server/web/controller/UserInfoController.java
代码语言:javascript复制@RestController
@RequestMapping("/user")
public class UserInfoController {
@Resource
private UserService userService;
@Resource
private UserInfoRepository userInfoRepository;
@PostMapping("save")
public ResultDTO<Void> save(@RequestBody ModifyUserInfoRequest request) {
UserInfoDO userInfoDO = new UserInfoDO();
BeanUtils.copyProperties(request, userInfoDO);
userService.save(userInfoDO);
return ResultDTO.success(null);
}
@GetMapping("list")
public ResultDTO<List<UserItemVO>> list(@RequestParam(required = false) String name) {
List<UserInfoDO> result;
if (StringUtils.isEmpty(name)) {
result = userInfoRepository.findAll();
}else {
result = userInfoRepository.findByUsernameLike("%" name "%");
}
return ResultDTO.success(convert(result));
}
private static List<UserItemVO> convert(List<UserInfoDO> data) {
if (CollectionUtils.isEmpty(data)) {
return Lists.newLinkedList();
}
return data.stream().map(x -> new UserItemVO(x.getId(), x.getUsername())).collect(Collectors.toList());
}
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static final class UserItemVO {
private Long id;
private String username;
}
}
UserInfoController提供了save、list方法,其中save方法用到了userService.save
UserService
tech/powerjob/server/core/service/UserService.java
代码语言:javascript复制@Service
public class UserService {
@Resource
private UserInfoRepository userInfoRepository;
/**
* 保存/修改 用户
* @param userInfoDO user
*/
public void save(UserInfoDO userInfoDO) {
userInfoDO.setGmtCreate(new Date());
userInfoDO.setGmtModified(userInfoDO.getGmtCreate());
userInfoRepository.saveAndFlush(userInfoDO);
}
/**
* 根据用户ID字符串获取用户信息详细列表
* @param userIds 逗号分割的用户ID信息
* @return 用户信息详细列表
*/
public List<UserInfoDO> fetchNotifyUserList(String userIds) {
if (StringUtils.isEmpty(userIds)) {
return Lists.newLinkedList();
}
// 去重
Set<Long> userIdList = Splitter.on(",").splitToList(userIds).stream().map(Long::valueOf).collect(Collectors.toSet());
List<UserInfoDO> res = userInfoRepository.findByIdIn(Lists.newLinkedList(userIdList));
res.forEach(x -> x.setPassword(null));
return res;
}
}
UserService提供了save、fetchNotifyUserList方法
alert
tech/powerjob/server/core/instance/InstanceManager.java
代码语言:javascript复制 private void alert(Long instanceId, String alertContent) {
InstanceInfoDO instanceInfo = instanceInfoRepository.findByInstanceId(instanceId);
JobInfoDO jobInfo;
try {
jobInfo = instanceMetadataService.fetchJobInfoByInstanceId(instanceId);
} catch (Exception e) {
log.warn("[InstanceManager-{}] can't find jobInfo, alarm failed.", instanceId);
return;
}
JobInstanceAlarm content = new JobInstanceAlarm();
BeanUtils.copyProperties(jobInfo, content);
BeanUtils.copyProperties(instanceInfo, content);
List<UserInfoDO> userList = SpringUtils.getBean(UserService.class).fetchNotifyUserList(jobInfo.getNotifyUserIds());
if (!StringUtils.isEmpty(alertContent)) {
content.setResult(alertContent);
}
alarmCenter.alarmFailed(content, AlarmUtils.convertUserInfoList2AlarmTargetList(userList));
}
InstanceManager的alert方法会根据instanceId拉取InstanceInfoDO、JobInfoDO信息,然后根据alertContent创建JobInstanceAlarm,最后通过alarmCenter.alarmFailed(content, AlarmUtils.convertUserInfoList2AlarmTargetList(userList))进行告警;如果用户信息的webHook不为空,则会通过webhook回调;若DingTalkUtils存在且用户信息的phone不为空,则会通过dingTalkUtils.fetchUserIdByMobile(phone)获取dingtalk的userId然后进行dingtalk告警;若javaMailSender实例存在,且配置了from则会通过email进行告警
UserInfoDO
tech/powerjob/server/persistence/remote/model/UserInfoDO.java
代码语言:javascript复制@Data
@Entity
@Table(indexes = {
@Index(name = "uidx01_user_info", columnList = "username"),
@Index(name = "uidx02_user_info", columnList = "email")
})
public class UserInfoDO {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
@GenericGenerator(name = "native", strategy = "native")
private Long id;
private String username;
private String password;
/**
* 手机号
*/
private String phone;
/**
* 邮箱地址
*/
private String email;
/**
* webHook
*/
private String webHook;
/**
* 扩展字段
*/
private String extra;
private Date gmtCreate;
private Date gmtModified;
}
UserInfoDO定义了id、username、password、phone、email、webHook、extra、gmtCreate、gmtModified属性
相关表
user_info
代码语言:javascript复制Create Table: CREATE TABLE `user_info` (
`id` bigint NOT NULL AUTO_INCREMENT,
`email` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`extra` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`gmt_create` datetime(6) DEFAULT NULL,
`gmt_modified` datetime(6) DEFAULT NULL,
`password` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`phone` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`username` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`web_hook` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `uidx01_user_info` (`username`),
KEY `uidx02_user_info` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
1 row in set (0.00 sec)
这里的user_info仅仅作为任务告警用
job_info
代码语言:javascript复制CREATE TABLE `job_info` (
`id` bigint NOT NULL AUTO_INCREMENT,
`alarm_config` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`app_id` bigint DEFAULT NULL,
`concurrency` int DEFAULT NULL,
`designated_workers` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`dispatch_strategy` int DEFAULT NULL,
`execute_type` int DEFAULT NULL,
`extra` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`gmt_create` datetime(6) DEFAULT NULL,
`gmt_modified` datetime(6) DEFAULT NULL,
`instance_retry_num` int DEFAULT NULL,
`instance_time_limit` bigint DEFAULT NULL,
`job_description` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`job_name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`job_params` longtext COLLATE utf8mb4_general_ci,
`lifecycle` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`log_config` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`max_instance_num` int DEFAULT NULL,
`max_worker_count` int DEFAULT NULL,
`min_cpu_cores` double NOT NULL,
`min_disk_space` double NOT NULL,
`min_memory_space` double NOT NULL,
`next_trigger_time` bigint DEFAULT NULL,
`notify_user_ids` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`processor_info` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`processor_type` int DEFAULT NULL,
`status` int DEFAULT NULL,
`tag` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`task_retry_num` int DEFAULT NULL,
`time_expression` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`time_expression_type` int DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx01_job_info` (`app_id`,`status`,`time_expression_type`,`next_trigger_time`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
job_info定义了notify_user_ids字段,存储了任务要告警的用户的id,多个用逗号分隔
小结
PowerJob的UserInfoController提供了save、list方法,其中save方法用到了userService.save;UserService提供了save、fetchNotifyUserList方法;它的userInfo仅仅是作为job的告警用户,其中job_info定义了notify_user_ids字段,存储了任务要告警的用户的id,多个用逗号分隔;InstanceManager的alert方法会根据instanceId拉取InstanceInfoDO、JobInfoDO信息,然后根据alertContent创建JobInstanceAlarm,最后通过alarmCenter.alarmFailed(content, AlarmUtils.convertUserInfoList2AlarmTargetList(userList))进行告警。