@@ -3,83 +3,109 @@ package main
33import (
44 "context"
55 "fmt"
6- "github.com/alitto/pond/v2"
6+ "github.com/morebec/go-misas/misas"
7+ "github.com/morebec/go-misas/mpostgres"
78 "github.com/morebec/go-misas/muuid"
89 "github.com/morebec/go-misas/mx"
9- "github.com/morebec/smallflow/internal/core"
10- "github.com/morebec/smallflow/internal/core/adapters"
1110 "github.com/morebec/smallflow/internal/orchestrator"
1211 adapters2 "github.com/morebec/smallflow/internal/orchestrator/adapters"
12+ "github.com/morebec/smallflow/internal/workflowmgmt"
13+ "github.com/morebec/smallflow/internal/workflowmgmt/adapters"
1314 "time"
1415)
1516
1617func main () {
1718 fmt .Println ("Build, run, and observe workflows without the overhead!" )
1819
19- eventStore := & adapters.InMemoryEventStore {}
2020 clock := mx .NewRealTimeClock (time .UTC )
21+
22+ dbConn , err := mpostgres .OpenConn ("postgres://smallflow:smallflow@localhost:5432/postgres?sslmode=disable" )
23+ if err != nil {
24+ panic (err )
25+ }
26+
27+ var eventStore misas.EventStore
28+ eventStore , err = mpostgres .NewEventStore (clock , dbConn )
29+ if err != nil {
30+ panic (err )
31+ }
32+
33+ eventRegistry := mx .NewMessageRegistry [misas.EventTypeName , misas.Event ]()
34+
35+ eventRegistry .Register (workflowmgmt .WorkflowEnabledEventTypeName , workflowmgmt.WorkflowEnabledEvent {})
36+ eventRegistry .Register (workflowmgmt .WorkflowDisabledEventTypeName , workflowmgmt.WorkflowDisabledEvent {})
37+ eventRegistry .Register (workflowmgmt .WorkflowTriggeredEventTypeName , workflowmgmt.WorkflowTriggeredEvent {})
38+ eventRegistry .Register (workflowmgmt .WorkflowStartedEventTypeName , workflowmgmt.WorkflowStartedEvent {})
39+ eventRegistry .Register (workflowmgmt .WorkflowEndedEventTypeName , workflowmgmt.WorkflowEndedEvent {})
40+ eventRegistry .Register (workflowmgmt .StepStartedEventTypeName , workflowmgmt.StepStartedEvent {})
41+ eventRegistry .Register (workflowmgmt .StepEndedEventTypeName , workflowmgmt.StepEndedEvent {})
42+
43+ eventStore = mx .NewEventStoreDeserializerDecorator (eventStore , eventRegistry )
44+
2145 workflowRepo := & adapters.EventStoreWorkflowRepository {
22- EventStore : eventStore ,
46+ EventStore : eventStore ,
47+ EventRegistry : eventRegistry ,
48+ UUIDGenerator : muuid .NewRandomUUIDGenerator (),
2349 }
2450 runRepo := & adapters.EventStoreRunRepository {
25- EventStore : eventStore ,
51+ EventStore : eventStore ,
52+ EventRegistry : eventRegistry ,
53+ UUIDGenerator : muuid .NewRandomUUIDGenerator (),
2654 }
2755
28- api := core .NewSubsystem (clock , workflowRepo , runRepo , muuid .NewRandomUUIDGenerator ()).API
29- orch := & orchestrator.WorkflowOrchestrator {
30- Clock : clock ,
31- API : api ,
32- LeaseManager : orchestrator.WorkflowLeaseManager {
33- Clock : clock ,
34- Repository : adapters2 .NewInMemoryWorkflowLeaseRepository (),
35- },
36- Pool : pond .NewPool (10 ),
56+ api := workflowmgmt .NewSubsystem (clock , workflowRepo , runRepo , muuid .NewRandomUUIDGenerator ()).API
57+ workflowLeaseRepository , err := adapters2 .NewPostgresWorkflowLeaseRepository (dbConn )
58+ if err != nil {
59+ panic (err )
3760 }
3861
39- ctx := context .Background ()
40-
41- fmt .Println ("Enabling workflow..." )
42- if result := api .HandleCommand (ctx , core.EnableWorkflowCommand {
43- WorkflowID : "my-workflow" ,
44- }); result .Error != nil {
45- panic (result .Error )
62+ leaseManager := orchestrator.WorkflowLeaseManager {
63+ Clock : clock ,
64+ Repository : workflowLeaseRepository ,
4665 }
4766
48- fmt .Println ("Triggering workflow..." )
49- if result := api .HandleCommand (ctx , core.TriggerWorkflowCommand {
50- WorkflowID : "my-workflow" ,
51- RunID : muuid .NewRandomUUIDGenerator ().Generate ().String (),
52- }); result .Error != nil {
53- panic (result .Error )
67+ checkpointStore , err := mpostgres .NewPostgreSQLCheckpointStore (dbConn )
68+ if err != nil {
69+ panic (err )
5470 }
71+ orch := orchestrator .NewWorkflowOrchestrator (
72+ clock ,
73+ api ,
74+ leaseManager ,
75+ muuid .NewRandomUUIDGenerator (),
76+ eventStore ,
77+ checkpointStore ,
78+ )
79+ orch .Start ()
80+ defer orch .Stop ()
81+
82+ ctx := context .Background ()
5583
56- fmt .Println ("Disabling workflow..." )
57- if result := api .HandleCommand (ctx , core. DisableWorkflowCommand {
84+ fmt .Println ("Enabling workflow..." )
85+ if result := api .HandleCommand (ctx , workflowmgmt. EnableWorkflowCommand {
5886 WorkflowID : "my-workflow" ,
5987 }); result .Error != nil {
6088 panic (result .Error )
6189 }
6290
63- fmt . Println ( "Dispatching events through the orchestrator..." )
64- orch . Start ( )
65- defer orch . Stop ()
66-
67- for _ , event := range eventStore . Events () {
68- if err := orch . HandleEvent ( ctx , event ); err != nil {
69- panic (err )
91+ for i := range 1 {
92+ fmt . Printf ( "Triggering workflow #%d... \n " , i + 1 )
93+ if result := api . HandleCommand ( ctx , workflowmgmt. TriggerWorkflowCommand {
94+ WorkflowID : "my-workflow" ,
95+ RunID : muuid . NewRandomUUIDGenerator (). Generate (). String (),
96+ } ); result . Error != nil {
97+ panic (result . Error )
7098 }
7199 }
72100
73- for orch .IsRunning () {
74- select {
75- case <- time .After (15 * time .Second ):
76- fmt .Println ("Stopping orchestrator after 15 seconds..." )
77- orch .Stop ()
78- }
101+ <- time .After (30 * time .Second )
102+ fmt .Println ("Current events in the event store:" )
103+ stream , err := eventStore .ReadFromStream (ctx , eventStore .GlobalStreamID (), misas.ReadFromEventStreamOptions {}.FromStart ().Forward ())
104+ if err != nil {
105+ panic (err )
79106 }
80107
81- fmt .Println ("Current events in the event store:" )
82- for i , event := range eventStore .Events () {
108+ for i , event := range stream .Events {
83109 fmt .Printf ("Event %d: %T → %+v\n " , i , event , event )
84110 }
85111}
0 commit comments