触发器实现海豚调度失败钉钉自动告警

sp_send存过

postgresql函数实现钉钉发送消息

t_ds_user增加字段

alter table t_ds_user add column dingding_name varchar(100);
--人为将海豚账号对应的钉钉用户名更新上去

编写触发器

CREATE OR REPLACE FUNCTION dp.tg_ds_udef_alert_ding()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
/*
 * 作者:v-yuzhenc
 * 功能:海豚调度工作流失败自动告警
 * */
declare
    i record;
    v_user varchar;
    v_mobile varchar;
    v_content text;
    v_message varchar;
begin
    if new.state in (4,5,6) then 
        for i in (
            select 
			     d.user_name
			    ,d.phone 
			    ,d.dingding_name
			    ,g.name project_name
			    ,e.name process_name
			    ,string_agg(distinct b.name||' '||to_char(b.end_time,'yyyy-mm-dd hh24:mi:ss'),'\r\n') task_name
			from t_ds_process_instance a 
			inner join t_ds_task_instance b 
			on (a.id = b.process_instance_id)
			inner join t_ds_task_definition c 
			on (b.task_code = c.code and b.task_definition_version = c."version")
			inner join t_ds_user d 
			on (c.user_id = d.id)
			inner join t_ds_process_definition e 
			on (a.process_definition_code = e.code and a.process_definition_version = e."version")
			inner join t_ds_project g 
            on (e.project_code = g.code)
			where c.task_type <> 'SUB_PROCESS'
			    and a.state = 6
			    and b.state = 6
			    and a.id = new.id
			group by d.user_name
				,d.phone 
				,d.dingding_name
				,g.name
				,e.name
        ) loop 
            v_mobile := i.phone;
            v_user := i.dingding_name;
            v_content := '海豚工作流执行失败,请尽快处理!\r\n项目名称:\r\n'||i.project_name||'\r\n工作流名称:\r\n'||i.process_name||'\r\n任务名称:\r\n'||i.task_name;
            v_message := $v_message${
    "at": {
        "atMobiles":[
            "$v_message$||v_mobile||$v_message$"
        ],
        "atUserIds":[
            "$v_message$||v_user||$v_message$"
        ],
        "isAtAll": false
    },
    "text": {
        "content":"$v_message$||v_content||$v_message$"
    },
    "msgtype":"text"
}$v_message$;
            --告警
            perform tool.sp_send(v_message::json);
        end loop;
    end if;
    return new;
end;
$function$
;

create trigger tg_state_ds_process_instance after update on t_ds_process_instance for each row execute procedure tg_ds_udef_alert_ding();

测试

触发器实现海豚调度失败钉钉自动告警_第1张图片

你可能感兴趣的:(postgresql,钉钉,dolphinschedule,触发器)