fluxpro-engine 0.1.0

A feature-gated workflow and process engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
use crate::db_models::process_def_db::ProcessDefDb;
use crate::db_models::process_instance_context_db::ProcessInstanceContextVariableDb;
use crate::models::commands::post_signal::PostSignal;
use crate::models::context_map::context_map::{ContextMap, ContextValue};
use crate::models::execution_log::ExecutionLogEvent;
use crate::models::id_field::IdField;
use crate::models::process_def::escalation_def::{EscalationActionDef, EscalationDef};
use crate::models::process_def::{Node, ProcessDefinition, StageDef};
use crate::models::queue::queue_task::{FluxproQueueTask, FluxproQueueTaskDefinition};
use crate::models::queue::queue_task_cancel::QueueTaskCancel;
use crate::models::version_id::VersionId;
use anyhow::Context;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use log::{error, info};
use serde_json::json;
use sqlx::types::JsonValue;
use sqlx::{Error, Pool, Postgres, Row};
use std::collections::HashMap;
use uuid::Uuid;

#[derive(Debug, Clone, Default, sqlx::FromRow)]
pub struct FluxproQueueHealth {
    pub ready_tasks: i64,
    pub locked_tasks: i64,
    pub oldest_ready_at: Option<DateTime<Utc>>,
}

#[async_trait]
pub trait FluxproDbService {
    async fn create_service_process_def(
        &self,
        process_def: &ProcessDefinition,
        compiled_process_def: JsonValue,
        source_definition: Option<&str>,
    ) -> Result<Uuid, sqlx::Error>;
    async fn get_current_process_def(
        &self,
        process_def_id: IdField,
        version: Option<VersionId>,
    ) -> Result<ProcessDefDb, sqlx::Error>;
    async fn get_process_def_by_instance(
        &self,
        process_token: &IdField,
    ) -> Result<ProcessDefDb, sqlx::Error>;
    async fn create_process_instance(
        &self,
        process_def_uuid: Uuid,
        process_id: IdField,
    ) -> Result<IdField, sqlx::Error>;
    async fn check_process_id(
        &self,
        process_def_uuid: Uuid,
        process_id: &IdField,
    ) -> Result<bool, sqlx::Error>;
    async fn get_stage_uuid(
        &self,
        process_token: &IdField,
        stage_id: &IdField,
    ) -> Result<Uuid, sqlx::Error>;

    async fn set_process_instance_stage(
        &self,
        process_def_uuid: &IdField,
        stage_uuid: Uuid,
        reason: Option<String>,
    ) -> Result<(), sqlx::Error>;
    async fn add_context_variable(
        &self,
        process_token: &IdField,
        scope: &str,
        var_id: &IdField,
        value: &ContextValue,
    ) -> Result<(), sqlx::Error>;
    async fn get_process_def_node(
        &self,
        process_token: &IdField,
        node_id: &IdField,
    ) -> Result<Node, sqlx::Error>;
    async fn get_process_def_node_uuid(
        &self,
        process_token: &IdField,
        node_id: &IdField,
    ) -> Result<Uuid, sqlx::Error>;
    async fn set_process_current_node(
        &self,
        process_token: &IdField,
        node: &Node,
    ) -> Result<(), sqlx::Error>;
    async fn add_queue_task<'a>(
        &self,
        queue_task: FluxproQueueTask,
        run_after: Option<DateTime<Utc>>,
        cancel_events: Option<Vec<QueueTaskCancel<'a>>>,
    ) -> Result<(), sqlx::Error>;
    async fn fetch_queue_task(
        &self,
        lock_key: &IdField,
        lease_ms: u64,
    ) -> anyhow::Result<Option<FluxproQueueTaskDefinition>>;
    async fn get_queue_health(&self) -> anyhow::Result<FluxproQueueHealth>;
    async fn renew_queue_task_lock(
        &self,
        task_uuid: Uuid,
        lock_key: &str,
        lease_ms: u64,
    ) -> anyhow::Result<bool>;
    async fn retry_queue_task(
        &self,
        task: &FluxproQueueTaskDefinition,
        run_after: DateTime<Utc>,
    ) -> anyhow::Result<()>;
    async fn kill_queue_task(&self, task: &FluxproQueueTaskDefinition) -> anyhow::Result<()>;
    async fn create_event_schedule(
        &self,
        process_token: &IdField,
        node: &Node,
        when: DateTime<Utc>,
        on_time: &IdField,
    ) -> anyhow::Result<()>;
    async fn get_process_instance_node(
        &self,
        process_token: &IdField,
    ) -> anyhow::Result<Option<Node>>;
    async fn get_process_instance_context(
        &self,
        process_token: &IdField,
    ) -> anyhow::Result<ContextMap>;
    async fn save_process_instance_context(
        &self,
        process_token: &IdField,
        context: &ContextMap,
    ) -> anyhow::Result<()>;
    async fn cancel_queue_tasks(
        &self,
        node_id: &IdField,
        signal_id: &IdField,
        process_token: &IdField,
    ) -> anyhow::Result<()>;
    async fn get_process_token_by_process_id(
        &self,
        process_id: &IdField,
    ) -> anyhow::Result<IdField>;
    async fn get_escalation_actions(
        &self,
        process_token: &IdField,
        escalation_topic: &IdField,
    ) -> anyhow::Result<Vec<EscalationActionDef>>;
    async fn log_signal(&self, process_token: &IdField, signal: &PostSignal) -> anyhow::Result<()>;
    async fn get_last_signal(&self, process_token: &IdField) -> anyhow::Result<Option<PostSignal>>;
    /// Persists a signal and enqueues its delivery as one operation.
    ///
    /// Implementations should override this method when they can provide transactional
    /// serialization per process instance. The default keeps custom implementations
    /// source-compatible, but cannot prevent concurrent duplicate submissions.
    async fn enqueue_signal_if_new(
        &self,
        process_token: &IdField,
        signal: &PostSignal,
    ) -> anyhow::Result<bool> {
        if is_duplicate_signal(self.get_last_signal(process_token).await?.as_ref(), signal) {
            return Ok(false);
        }
        self.log_signal(process_token, signal).await?;
        self.add_queue_task(
            FluxproQueueTask::ProcessSignal {
                process_token: process_token.clone(),
                signal: signal.clone(),
            },
            None,
            None,
        )
        .await?;
        Ok(true)
    }
    async fn write_execution_log(
        &self,
        process_token: &IdField,
        event: &ExecutionLogEvent,
    ) -> anyhow::Result<()>;
}

pub struct FluxproDbServiceImpl {
    db_pool: Pool<Postgres>,
}

impl FluxproDbServiceImpl {
    pub fn new(db_pool: Pool<Postgres>) -> Self {
        Self { db_pool }
    }
}

#[async_trait]
impl FluxproDbService for FluxproDbServiceImpl {
    async fn write_execution_log(
        &self,
        process_token: &IdField,
        event: &ExecutionLogEvent,
    ) -> anyhow::Result<()> {
        sqlx::query(
            r#"insert into fluxpro.process_instance_log
               (process_instance_uuid, level, event_type, source, message, node_id,
                handler_id, queue_task_uuid, attempt, error_kind, error_message, details)
               select uuid, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
                 from fluxpro.process_instance where token = $1"#,
        )
        .bind(process_token.get_id())
        .bind(event.level.to_string())
        .bind(&event.event_type)
        .bind(&event.source)
        .bind(&event.message)
        .bind(&event.node_id)
        .bind(&event.handler_id)
        .bind(event.queue_task_uuid)
        .bind(event.attempt)
        .bind(&event.error_kind)
        .bind(&event.error_message)
        .bind(&event.details)
        .execute(&self.db_pool)
        .await?;
        Ok(())
    }

    async fn create_service_process_def(
        &self,
        process_def: &ProcessDefinition,
        compiled_process_def: JsonValue,
        source_definition: Option<&str>,
    ) -> Result<Uuid, sqlx::Error> {
        let mut tr = self.db_pool.begin().await?;
        let index_id = sqlx::query_scalar(
            r#"select max(index_id) from fluxpro.process_definition where key_=$1"#,
        )
        .bind(process_def.key.get_id())
        .fetch_one(&mut *tr)
        .await
        .map_err(|e| {
            error!(
                "[create_service_process_def] error while fetching the current max index_id: {}",
                e
            );
            e
        })
        .unwrap_or(0);
        let process_def_uuid: Uuid = sqlx::query_scalar(
            r#"INSERT INTO
                    fluxpro.process_definition
                        (key_, version, index_id, status, effective_from, deprecated_at,
                         version_comment, definition, source_definition)
                        VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING uuid"#,
        )
        .bind(process_def.key.get_id())
        .bind(process_def.version.as_str())
        .bind(index_id + 1)
        .bind(process_def.status.to_string())
        .bind(process_def.effective_from)
        .bind(process_def.deprecated_at)
        .bind(process_def.metadata.comment.as_deref())
        .bind(compiled_process_def)
        .bind(source_definition)
        .fetch_one(&mut *tr)
        .await?;
        for node in &process_def.nodes {
            sqlx::query(
                r#"insert into fluxpro.process_def_node
                    (process_def_uuid, node_id, definition) values ($1, $2, $3)
                "#,
            )
            .bind(process_def_uuid)
            .bind(node.id().get_id())
            .bind(json!(node))
            .execute(&mut *tr)
            .await?;
        }
        for signal in &process_def.signals {
            sqlx::query(
                r#"insert into fluxpro.process_def_signal (
                    process_def_uuid, signal_id
                ) values ($1, $2)
                "#,
            )
            .bind(process_def_uuid)
            .bind(signal.name())
            .execute(&mut *tr)
            .await?;
        }
        for escalation in &process_def.escalations {
            sqlx::query(
                r#"insert into fluxpro.process_def_escalations (
                    process_def_uuid, escalation_id, definition
                ) values ($1, $2, $3)
                "#,
            )
            .bind(process_def_uuid)
            .bind(escalation.topic.get_id())
            .bind(json!(escalation))
            .execute(&mut *tr)
            .await?;
        }
        for stage in &process_def.stages {
            match stage {
                StageDef::Obj {
                    id,
                    name,
                    is_initial,
                    is_final,
                } => {
                    sqlx::query(r#"insert into fluxpro.process_stage
                            (process_def_uuid, stage_id, name, is_initial, is_final) VALUES ($1, $2, $3, $4, $5)
                    "#).bind(process_def_uuid)
                        .bind(id.get_id())
                        .bind(name)
                        .bind(is_initial)
                        .bind(is_final)
                        .execute(&mut *tr)
                        .await?;
                }
                StageDef::Name(id) => {
                    sqlx::query(r#"insert into fluxpro.process_stage
                            (process_def_uuid, stage_id, name, is_initial, is_final) VALUES ($1, $2, $3, $4, $5)
                    "#).bind(process_def_uuid)
                        .bind(id.get_id())
                        .bind(id.get_id())
                        .bind(false)
                        .bind(false)
                        .execute(&mut *tr)
                        .await?;
                }
            }
        }
        let _ = tr.commit().await?;
        Ok(process_def_uuid)
    }

    async fn get_current_process_def(
        &self,
        process_def_id: IdField,
        version: Option<VersionId>,
    ) -> Result<ProcessDefDb, sqlx::Error> {
        let version_id = version.map(|version| version.as_str().to_string());
        let process_def_db = sqlx::query_as(
            r#"
                        SELECT *
                        FROM fluxpro.process_definition
                        WHERE key_ = $1
                          AND ( $2::text IS NULL OR version = $2 )
                          AND effective_from <= $3
                          AND (deprecated_at IS NULL OR deprecated_at > $3)
                        ORDER BY index_id DESC
                        LIMIT 1
            "#,
        )
        .bind(process_def_id.get_id())
        .bind(version_id)
        .bind(Utc::now())
        .fetch_one(&self.db_pool)
        .await?;
        Ok(process_def_db)
    }

    async fn get_process_def_by_instance(
        &self,
        process_token: &IdField,
    ) -> Result<ProcessDefDb, Error> {
        let process_def_db = sqlx::query_as(
            r#"
                        SELECT *
                        FROM fluxpro.process_definition
                        WHERE uuid = (select process_def_uuid from fluxpro.process_instance where token = $1)
            "#).bind(process_token.get_id())
            .fetch_one(&self.db_pool)
            .await?;
        Ok(process_def_db)
    }

    async fn create_process_instance(
        &self,
        process_def_uuid: Uuid,
        process_id: IdField,
    ) -> Result<IdField, Error> {
        let token = IdField::generate();
        sqlx::query(
            r#"insert into fluxpro.process_instance (token, process_def_uuid, process_id) values ($1, $2, $3)"#)
            .bind(token.get_id())
            .bind(process_def_uuid)
            .bind(process_id.get_id())
            .execute(&self.db_pool)
            .await?;
        Ok(token)
    }

    async fn check_process_id(
        &self,
        process_def_uuid: Uuid,
        process_id: &IdField,
    ) -> Result<bool, Error> {
        let count: i32 = sqlx::query_scalar(
            r#"select count(uuid)::int from fluxpro.process_instance where
                    process_def_uuid=$1 and process_id=$2"#,
        )
        .bind(process_def_uuid)
        .bind(process_id.get_id())
        .fetch_one(&self.db_pool)
        .await?;
        Ok(count < 1)
    }

    async fn get_stage_uuid(
        &self,
        process_token: &IdField,
        stage_id: &IdField,
    ) -> Result<Uuid, Error> {
        Ok(sqlx::query_scalar(
            r#"select uuid from fluxpro.process_stage where process_def_uuid=
                    (select process_def_uuid from fluxpro.process_instance where token=$1) and stage_id=$2"#
        ).bind(process_token.get_id())
            .bind(stage_id.get_id())
            .fetch_one(&self.db_pool)
            .await?
        )
    }

    async fn set_process_instance_stage(
        &self,
        process_token: &IdField,
        stage_uuid: Uuid,
        reason: Option<String>,
    ) -> Result<(), sqlx::Error> {
        let mut tr = self.db_pool.begin().await?;
        sqlx::query(
            r#"insert into fluxpro.process_instance_stage_log (process_instance_uuid, stage_uuid, reason) VALUES
                               ((select uuid from fluxpro.process_instance where token = $1), $2, $3)"#
        ).bind(process_token.get_id())
            .bind(stage_uuid)
            .bind(&reason)
            .execute(&mut *tr)
            .await?;
        sqlx::query(
            r#"update fluxpro.process_instance set current_stage=$1, current_stage_reason=$2 where token=$3"#
        ).bind(stage_uuid)
            .bind(reason)
            .bind(process_token.get_id())
            .execute(&mut *tr)
            .await?;
        let _ = tr.commit().await?;
        Ok(())
    }

    async fn add_context_variable(
        &self,
        process_token: &IdField,
        scope: &str,
        var_id: &IdField,
        value: &ContextValue,
    ) -> Result<(), Error> {
        info!(
            "[add_context_variable] scope: {}, var_id: {}, value: {:?}",
            scope, var_id, value
        );
        let mut tr = self.db_pool.begin().await?;
        sqlx::query(
            r#"delete from fluxpro.process_instance_context_variable
                    where process_instance_uuid=(select uuid from fluxpro.process_instance where token=$1) and scope=$2 and name=$3"#
        ).bind(process_token.get_id())
            .bind(scope)
            .bind(var_id.get_id())
            .execute(&mut *tr)
            .await
            .map_err(|e| {
                error!("[add_context_variable] failed to delete existing variable: {:?}", e);
                e
            })?;
        sqlx::query(
            r#"insert into fluxpro.process_instance_context_variable (
                        process_instance_uuid,
                        scope,
                        name,
                        value
                    )
                    values (
                        (select uuid from fluxpro.process_instance where token = $1),
                        $2,
                        $3,
                        $4
                    )
                    on conflict (process_instance_uuid, scope, name)
                    do update set
                        value = EXCLUDED.value;"#,
        )
        .bind(process_token.get_id())
        .bind(scope)
        .bind(var_id.get_id())
        .bind(json!(value))
        .execute(&mut *tr)
        .await
        .map_err(|e| {
            error!("[add_context_variable] failed to insert variable: {:?}", e);
            e
        })?;
        let _ = tr.commit().await.map_err(|e| {
            error!(
                "[add_context_variable] failed to commit transaction: {:?}",
                e
            );
            e
        })?;
        Ok(())
    }

    async fn get_process_def_node(
        &self,
        process_token: &IdField,
        node_id: &IdField,
    ) -> Result<Node, Error> {
        let found_node_def: JsonValue = sqlx::query_scalar(
            r#"select definition from fluxpro.process_def_node where
                    process_def_uuid=(select process_def_uuid from fluxpro.process_instance where token = $1) and node_id=$2"#
        ).bind(process_token.get_id())
            .bind(node_id.get_id())
            .fetch_one(&self.db_pool)
            .await?;
        let node =
            serde_json::from_value(found_node_def).map_err(|e| sqlx::Error::ColumnDecode {
                index: "definition".to_string(),
                source: Box::new(e),
            })?;
        Ok(node)
    }

    async fn get_process_def_node_uuid(
        &self,
        process_token: &IdField,
        node_id: &IdField,
    ) -> Result<Uuid, Error> {
        let node_uuid = sqlx::query_scalar(
            r#"select uuid from fluxpro.process_def_node where
                    process_def_uuid=(select process_def_uuid from fluxpro.process_instance where token = $1) and node_id=$2"#
        ).bind(process_token.get_id())
            .bind(node_id.get_id())
            .fetch_one(&self.db_pool)
            .await?;
        Ok(node_uuid)
    }

    async fn set_process_current_node(
        &self,
        process_token: &IdField,
        node: &Node,
    ) -> Result<(), Error> {
        sqlx::query(
            r#"
            update fluxpro.process_instance
                set current_node_ref=
                    (select uuid from fluxpro.process_def_node
                                 where process_def_uuid = (
                        select process_def_uuid from fluxpro.process_instance where token=$2
                    ) and node_id=$1)
                where token=$2
            "#,
        )
        .bind(node.id().get_id())
        .bind(process_token.get_id())
        .execute(&self.db_pool)
        .await?;
        Ok(())
    }

    async fn add_queue_task<'a>(
        &self,
        queue_task: FluxproQueueTask,
        run_after: Option<DateTime<Utc>>,
        cancel_events: Option<Vec<QueueTaskCancel<'a>>>,
    ) -> Result<(), Error> {
        let mut tr = self.db_pool.begin().await?;
        let task_uuid: Uuid = sqlx::query_scalar(
            r#"insert into fluxpro.queue_runner (task, run_after) values ($1, $2) returning uuid"#,
        )
        .bind(serde_json::to_value(&queue_task).unwrap())
        .bind(run_after.unwrap_or_else(|| Utc::now()))
        .fetch_one(&mut *tr)
        .await?;
        if let Some(cancel_events) = cancel_events {
            for cancel_event in cancel_events {
                sqlx::query(
                    r#"insert into fluxpro.queue_cancel_task (process_token, node_id, event_id, task_uuid) values ($1, $2, $3, $4)"#
                )
                    .bind(cancel_event.process_token().get_id())
                    .bind(cancel_event.node().id().get_id())
                    .bind(cancel_event.event_id().get_id())
                    .bind(task_uuid)
                    .execute(&mut *tr)
                    .await?;
            }
        }
        tr.commit().await?;
        Ok(())
    }

    async fn fetch_queue_task(
        &self,
        lock_key: &IdField,
        lease_ms: u64,
    ) -> anyhow::Result<Option<FluxproQueueTaskDefinition>> {
        let lease_ms = i64::try_from(lease_ms).unwrap_or(i64::MAX);
        sqlx::query_as(
            r#"
            with candidate as (
                select pending.uuid
                from fluxpro.queue_runner pending
                where pending.run_after <= now()
                  and (pending.lock_key = '' or pending.locked_by is null or pending.locked_by < now())
                  -- One workflow instance is processed serially. The advisory
                  -- transaction lock closes the race where two workers select
                  -- different ready rows for the same process before either row's
                  -- lease update becomes visible.
                  and pg_try_advisory_xact_lock(hashtextextended(coalesce(
                        pending.task #>> '{process_node,process_token}',
                        pending.task #>> '{ProcessEvent,process_token}',
                        pending.task #>> '{ProcessSignal,process_token}'
                      ), 0))
                  and not exists (
                      select 1
                      from fluxpro.queue_runner active
                      where active.uuid <> pending.uuid
                        and active.lock_key <> ''
                        and active.locked_by >= now()
                        and coalesce(
                              active.task #>> '{process_node,process_token}',
                              active.task #>> '{ProcessEvent,process_token}',
                              active.task #>> '{ProcessSignal,process_token}'
                            ) = coalesce(
                              pending.task #>> '{process_node,process_token}',
                              pending.task #>> '{ProcessEvent,process_token}',
                              pending.task #>> '{ProcessSignal,process_token}'
                            )
                  )
                order by pending.run_after, pending.created_at
                for update skip locked
                limit 1
            )
            update fluxpro.queue_runner q
               set lock_key = $1,
                   locked_at = now(),
                   locked_by = now() + ($2 * interval '1 millisecond'),
                   attempts = attempts + 1
              from candidate
             where q.uuid = candidate.uuid
            returning q.*
            "#,
        )
        .bind(lock_key.get_id())
        .bind(lease_ms)
        .fetch_optional(&self.db_pool)
        .await
        .map_err(|e| {
            error!("error while atomically fetching a queue task: {}", e);
            e.into()
        })
    }

    async fn get_queue_health(&self) -> anyhow::Result<FluxproQueueHealth> {
        sqlx::query_as(
            r#"
            select
                count(*) filter (
                    where run_after <= now()
                      and (lock_key = '' or locked_by is null or locked_by < now())
                ) as ready_tasks,
                count(*) filter (
                    where lock_key <> '' and locked_by >= now()
                ) as locked_tasks,
                min(run_after) filter (
                    where run_after <= now()
                      and (lock_key = '' or locked_by is null or locked_by < now())
                ) as oldest_ready_at
            from fluxpro.queue_runner
            "#,
        )
        .fetch_one(&self.db_pool)
        .await
        .map_err(Into::into)
    }

    async fn renew_queue_task_lock(
        &self,
        task_uuid: Uuid,
        lock_key: &str,
        lease_ms: u64,
    ) -> anyhow::Result<bool> {
        let lease_ms = i64::try_from(lease_ms).unwrap_or(i64::MAX);
        let result = sqlx::query(
            r#"
            update fluxpro.queue_runner
               set locked_by = now() + ($3 * interval '1 millisecond')
             where uuid = $1 and lock_key = $2 and locked_by >= now()
            "#,
        )
        .bind(task_uuid)
        .bind(lock_key)
        .bind(lease_ms)
        .execute(&self.db_pool)
        .await?;
        Ok(result.rows_affected() == 1)
    }

    async fn retry_queue_task(
        &self,
        task: &FluxproQueueTaskDefinition,
        run_after: DateTime<Utc>,
    ) -> anyhow::Result<()> {
        let updated = sqlx::query(
            r#"update fluxpro.queue_runner
                  set run_after = $3, lock_key = '', locked_at = null, locked_by = null
                where uuid = $1 and lock_key = $2 and locked_by >= now()"#,
        )
        .bind(task.uuid)
        .bind(&task.lock_key)
        .bind(run_after)
        .execute(&self.db_pool)
        .await?;
        anyhow::ensure!(
            updated.rows_affected() == 1,
            "queue task {} is no longer owned by lock {}",
            task.uuid,
            task.lock_key
        );
        Ok(())
    }

    async fn kill_queue_task(&self, task: &FluxproQueueTaskDefinition) -> anyhow::Result<()> {
        let mut tr = self.db_pool.begin().await?;
        sqlx::query(
            r#"delete from fluxpro.queue_cancel_task
               where task_uuid=$1
                 and exists (select 1 from fluxpro.queue_runner where uuid=$1 and lock_key=$2)"#,
        )
        .bind(task.uuid)
        .bind(&task.lock_key)
        .execute(&mut *tr)
        .await?;
        let deleted = sqlx::query("delete from fluxpro.queue_runner where uuid=$1 and lock_key=$2")
            .bind(task.uuid)
            .bind(&task.lock_key)
            .execute(&mut *tr)
            .await?;
        if deleted.rows_affected() != 1 {
            return Err(anyhow::anyhow!(
                "queue task {} is no longer owned by lock {}",
                task.uuid,
                task.lock_key
            ));
        }
        tr.commit().await?;
        Ok(())
    }

    async fn create_event_schedule(
        &self,
        process_token: &IdField,
        node: &Node,
        when: DateTime<Utc>,
        on_time: &IdField,
    ) -> anyhow::Result<()> {
        sqlx::query(
            r#"
            insert into fluxpro.event_schedule (
                process_instance_uuid, process_def_node_uuid, scheduled_time, on_time
            ) values ((select uuid from fluxpro.process_instance where token = $1),
                      (select uuid from fluxpro.process_def_node where process_def_uuid = (
                          select process_def_uuid from fluxpro.process_instance where token=$1
                      ) and node_id=$2),
                      $3, $4)"#,
        )
        .bind(process_token.get_id())
        .bind(node.id().get_id())
        .bind(when)
        .bind(on_time.get_id())
        .execute(&self.db_pool)
        .await?;
        Ok(())
    }

    async fn get_process_instance_node(
        &self,
        process_token: &IdField,
    ) -> anyhow::Result<Option<Node>> {
        let node_def = sqlx::query_scalar(
            r#"
            select definition from fluxpro.process_def_node where
                    uuid=(select current_node_ref from fluxpro.process_instance where token = $1)"#,
        )
        .bind(process_token.get_id())
        .fetch_optional(&self.db_pool)
        .await?;
        if let Some(node_def) = node_def {
            if let Ok(node) = serde_json::from_value::<Node>(node_def) {
                return Ok(Some(node));
            }
        }
        Err(anyhow::Error::msg("Node not found"))
    }

    async fn get_process_instance_context(
        &self,
        process_token: &IdField,
    ) -> anyhow::Result<ContextMap> {
        let context_vars: Vec<ProcessInstanceContextVariableDb> = sqlx::query_as(
            r#"select * from fluxpro.process_instance_context_variable
                where process_instance_uuid=(select uuid from fluxpro.process_instance where token = $1)"#
        )
            .bind(process_token.get_id())
            .fetch_all(&self.db_pool)
            .await?;
        let mut context = HashMap::new();
        context_vars.iter().for_each(|var| {
            context.insert(var.name.clone(), var.value.clone());
        });
        Ok(ContextMap(context))
    }

    async fn save_process_instance_context(
        &self,
        process_token: &IdField,
        context: &ContextMap,
    ) -> anyhow::Result<()> {
        let mut tr = self.db_pool.begin().await?;
        // sqlx::query(
        //     r#"delete from fluxpro.process_instance_context_variable where process_instance_uuid=(select uuid from fluxpro.process_instance where token = $1)"#
        // )
        //     .bind(process_token.get_id())
        //     .execute(&mut *tr)
        //     .await?;
        for (key, value) in context.0.iter() {
            sqlx::query(
                r#"delete from fluxpro.process_instance_context_variable where process_instance_uuid=(select uuid from fluxpro.process_instance where token = $1) and scope=$2 and name=$3"#
            )
                .bind(process_token.get_id())
                .bind("_")
                .bind(key.get_id())
                .execute(&mut *tr)
                .await?;
            sqlx::query(
                r#"insert into fluxpro.process_instance_context_variable (process_instance_uuid, scope, name, value)
                    VALUES ((select uuid from fluxpro.process_instance where token=$1), $2, $3, $4)"#
            ).bind(process_token.get_id())
                .bind("_")
                .bind(key.get_id())
                .bind(json!(value))
                .execute(&mut *tr)
                .await?;
        }
        tr.commit().await?;
        Ok(())
    }

    async fn cancel_queue_tasks(
        &self,
        node_id: &IdField,
        signal_id: &IdField,
        process_token: &IdField,
    ) -> anyhow::Result<()> {
        let task_uuid: Vec<Uuid> = sqlx::query_scalar(
            r#"select task_uuid from fluxpro.queue_cancel_task where node_id=$1 and event_id=$2 and process_token=$3"#
        )
            .bind(node_id.get_id())
            .bind(signal_id.get_id())
            .bind(process_token.get_id())
            .fetch_all(&self.db_pool)
            .await?;
        let mut tr = self.db_pool.begin().await?;
        sqlx::query("delete from fluxpro.queue_cancel_task where task_uuid = any($1)")
            .bind(&task_uuid)
            .execute(&mut *tr)
            .await?;
        sqlx::query("delete from fluxpro.queue_runner where uuid = any($1)")
            .bind(&task_uuid)
            .execute(&mut *tr)
            .await?;
        tr.commit().await?;
        Ok(())
    }

    async fn get_process_token_by_process_id(
        &self,
        process_id: &IdField,
    ) -> anyhow::Result<IdField> {
        let found_token: String =
            sqlx::query_scalar(r#"select token from fluxpro.process_instance where process_id=$1"#)
                .bind(process_id.get_id())
                .fetch_one(&self.db_pool)
                .await?;
        Ok(IdField::new(&found_token)?)
    }

    async fn get_escalation_actions(
        &self,
        process_token: &IdField,
        escalation_topic: &IdField,
    ) -> anyhow::Result<Vec<EscalationActionDef>> {
        let definition: Option<JsonValue> = sqlx::query_scalar(
            r#"
        select e.definition
        from fluxpro.process_def_escalations e
        where e.escalation_id = $1
          and e.process_def_uuid = (
              select i.process_def_uuid
              from fluxpro.process_instance i
              where i.token = $2
          )
        "#,
        )
        .bind(escalation_topic.get_id())
        .bind(process_token.get_id())
        .fetch_optional(&self.db_pool)
        .await
        .map_err(|e| {
            error!(
                "[get_escalation_actions] db_error escalation_id={} token={} err={}",
                escalation_topic.get_id(),
                process_token.get_id(),
                e
            );
            anyhow::Error::from(e)
        })?;

        let Some(definition) = definition else {
            return Ok(vec![]);
        };

        let escalation: EscalationDef = serde_json::from_value(definition).with_context(|| {
            format!(
                "Invalid EscalationDef JSON: escalation_id={} token={}",
                escalation_topic.get_id(),
                process_token.get_id(),
            )
        })?;

        Ok(escalation.actions)
    }

    async fn log_signal(&self, process_token: &IdField, signal: &PostSignal) -> anyhow::Result<()> {
        sqlx::query(
            r#"insert into fluxpro.signal_history (process_id, signal_name, payload)
                        values ($1, $2, $3)"#,
        )
        .bind(process_token.get_id())
        .bind(signal.signal.get_id())
        .bind(json!(signal.context.0))
        .execute(&self.db_pool)
        .await
        .map_err(|e| anyhow::Error::msg(format!("failed to log signal: {:?}", e)))?;
        Ok(())
    }

    async fn get_last_signal(&self, process_token: &IdField) -> anyhow::Result<Option<PostSignal>> {
        let row = sqlx::query(
            r#"
                select signal_name, payload
                from fluxpro.signal_history
                where process_id = $1
                order by created_at desc, uuid desc
                limit 1
                "#,
        )
        .bind(process_token.get_id())
        .fetch_optional(&self.db_pool)
        .await?;

        row.map(signal_from_row).transpose()
    }

    async fn enqueue_signal_if_new(
        &self,
        process_token: &IdField,
        signal: &PostSignal,
    ) -> anyhow::Result<bool> {
        let mut transaction = self.db_pool.begin().await?;

        // Serialize signal admission for one process. Without this lock, concurrent
        // callers can all observe the same previous signal and enqueue duplicate paths.
        let process_exists = sqlx::query_scalar::<_, Uuid>(
            r#"select uuid from fluxpro.process_instance where token = $1 for update"#,
        )
        .bind(process_token.get_id())
        .fetch_optional(&mut *transaction)
        .await?;
        anyhow::ensure!(
            process_exists.is_some(),
            "process instance {} not found",
            process_token
        );

        let last_signal = sqlx::query(
            r#"
                select signal_name, payload
                from fluxpro.signal_history
                where process_id = $1
                order by created_at desc, uuid desc
                limit 1
                "#,
        )
        .bind(process_token.get_id())
        .fetch_optional(&mut *transaction)
        .await?
        .map(signal_from_row)
        .transpose()?;

        if is_duplicate_signal(last_signal.as_ref(), signal) {
            transaction.commit().await?;
            return Ok(false);
        }

        sqlx::query(
            r#"insert into fluxpro.signal_history (process_id, signal_name, payload)
               values ($1, $2, $3)"#,
        )
        .bind(process_token.get_id())
        .bind(signal.signal.get_id())
        .bind(json!(signal.context.0))
        .execute(&mut *transaction)
        .await?;

        sqlx::query(r#"insert into fluxpro.queue_runner (task, run_after) values ($1, $2)"#)
            .bind(serde_json::to_value(FluxproQueueTask::ProcessSignal {
                process_token: process_token.clone(),
                signal: signal.clone(),
            })?)
            .bind(Utc::now())
            .execute(&mut *transaction)
            .await?;

        transaction.commit().await?;
        Ok(true)
    }
}

fn signal_from_row(row: sqlx::postgres::PgRow) -> anyhow::Result<PostSignal> {
    let signal_name: String = row.try_get("signal_name")?;
    let signal = IdField::new(signal_name)
        .map_err(|_| anyhow::Error::msg("failed to create IdField from signal_name"))?;
    let payload: sqlx::types::Json<ContextMap> = row.try_get("payload")?;
    Ok(PostSignal {
        signal,
        context: payload.0,
    })
}

fn is_duplicate_signal(last_signal: Option<&PostSignal>, signal: &PostSignal) -> bool {
    last_signal == Some(signal)
}

#[cfg(test)]
mod signal_deduplication_tests {
    use super::*;
    use crate::models::context_map::context_map::ContextValue;

    fn signal(name: &str, otp: &str) -> PostSignal {
        PostSignal {
            signal: IdField::new(name).unwrap(),
            context: ContextMap(HashMap::from([(
                IdField::new("otp").unwrap(),
                ContextValue::string(otp.to_string()),
            )])),
        }
    }

    #[test]
    fn identical_signal_and_context_are_duplicates() {
        let first = signal("otp_verified", "1234");
        let repeated = first.clone();

        assert!(is_duplicate_signal(Some(&first), &repeated));
    }

    #[test]
    fn changed_context_is_a_new_signal() {
        let first = signal("otp_verified", "1234");
        let next = signal("otp_verified", "5678");

        assert!(!is_duplicate_signal(Some(&first), &next));
    }
}