@@ -35,7 +35,6 @@ impl ComputeHandler for WorkflowHandler {
35
35
node : & mut DriaComputeNode ,
36
36
message : DKNMessage ,
37
37
) -> Result < MessageAcceptance > {
38
- let config = & node. config ;
39
38
let task = message
40
39
. parse_payload :: < TaskRequestPayload < WorkflowPayload > > ( true )
41
40
. wrap_err ( "Could not parse workflow task" ) ?;
@@ -55,7 +54,7 @@ impl ComputeHandler for WorkflowHandler {
55
54
}
56
55
57
56
// check task inclusion via the bloom filter
58
- if !task. filter . contains ( & config. address ) ? {
57
+ if !task. filter . contains ( & node . config . address ) ? {
59
58
log:: info!(
60
59
"Task {} does not include this node within the filter." ,
61
60
task. task_id
@@ -66,16 +65,19 @@ impl ComputeHandler for WorkflowHandler {
66
65
}
67
66
68
67
// read model / provider from the task
69
- let ( model_provider, model) = config. workflows . get_any_matching_model ( task. input . model ) ?;
68
+ let ( model_provider, model) = node
69
+ . config
70
+ . workflows
71
+ . get_any_matching_model ( task. input . model ) ?;
70
72
let model_name = model. to_string ( ) ; // get model name, we will pass it in payload
71
73
log:: info!( "Using model {} for task {}" , model_name, task. task_id) ;
72
74
73
75
// prepare workflow executor
74
76
let executor = if model_provider == ModelProvider :: Ollama {
75
77
Executor :: new_at (
76
78
model,
77
- & config. workflows . ollama . host ,
78
- config. workflows . ollama . port ,
79
+ & node . config . workflows . ollama . host ,
80
+ node . config . workflows . ollama . port ,
79
81
)
80
82
} else {
81
83
Executor :: new ( model)
@@ -93,13 +95,14 @@ impl ComputeHandler for WorkflowHandler {
93
95
log:: info!( "Received cancellation, quitting all tasks." ) ;
94
96
return Ok ( MessageAcceptance :: Accept ) ;
95
97
} ,
96
- exec_result_inner = executor. execute( entry. as_ref( ) , task. input. workflow, & mut memory) => {
98
+ exec_result_inner = executor. execute( entry. as_ref( ) , & task. input. workflow, & mut memory) => {
97
99
exec_result = exec_result_inner. map_err( |e| eyre!( "Execution error: {}" , e. to_string( ) ) ) ;
98
100
}
99
101
}
100
102
101
- match exec_result {
103
+ let ( publish_result , acceptance ) = match exec_result {
102
104
Ok ( result) => {
105
+ log:: warn!( "Task {} result:" , result) ;
103
106
// obtain public key from the payload
104
107
let task_public_key_bytes =
105
108
hex:: decode ( & task. public_key ) . wrap_err ( "Could not decode public key" ) ?;
@@ -110,36 +113,55 @@ impl ComputeHandler for WorkflowHandler {
110
113
result,
111
114
& task. task_id ,
112
115
& task_public_key,
113
- & config. secret_key ,
116
+ & node . config . secret_key ,
114
117
model_name,
115
118
) ?;
116
119
let payload_str = serde_json:: to_string ( & payload)
117
120
. wrap_err ( "Could not serialize response payload" ) ?;
118
121
119
122
// publish the result
120
- let message = DKNMessage :: new ( payload_str, Self :: RESPONSE_TOPIC ) ;
121
- node. publish ( message) ?;
122
-
123
123
// accept so that if there are others included in filter they can do the task
124
- Ok ( MessageAcceptance :: Accept )
124
+ let message = DKNMessage :: new ( payload_str, Self :: RESPONSE_TOPIC ) ;
125
+ ( node. publish ( message) , MessageAcceptance :: Accept )
125
126
}
126
127
Err ( err) => {
127
128
// use pretty display string for error logging with causes
128
129
let err_string = format ! ( "{:#}" , err) ;
129
130
log:: error!( "Task {} failed: {}" , task. task_id, err_string) ;
130
131
131
132
// prepare error payload
132
- let error_payload = TaskErrorPayload :: new ( task. task_id , err_string, model_name) ;
133
+ let error_payload =
134
+ TaskErrorPayload :: new ( task. task_id . clone ( ) , err_string, model_name) ;
133
135
let error_payload_str = serde_json:: to_string ( & error_payload)
134
136
. wrap_err ( "Could not serialize error payload" ) ?;
135
137
136
138
// publish the error result for diagnostics
137
- let message = DKNMessage :: new ( error_payload_str, Self :: RESPONSE_TOPIC ) ;
138
- node. publish ( message) ?;
139
-
140
139
// ignore just in case, workflow may be bugged
141
- Ok ( MessageAcceptance :: Ignore )
140
+ let message = DKNMessage :: new_signed (
141
+ error_payload_str,
142
+ Self :: RESPONSE_TOPIC ,
143
+ & node. config . secret_key ,
144
+ ) ;
145
+ ( node. publish ( message) , MessageAcceptance :: Ignore )
142
146
}
147
+ } ;
148
+
149
+ // if for some reason we couldnt publish the result, publish the error itself so that RPC doesnt hang
150
+ if let Err ( publish_err) = publish_result {
151
+ let err_msg = format ! ( "Could not publish result: {:?}" , publish_err) ;
152
+ log:: error!( "{}" , err_msg) ;
153
+ let payload = serde_json:: json!( {
154
+ "taskId" : task. task_id,
155
+ "error" : err_msg
156
+ } ) ;
157
+ let message = DKNMessage :: new_signed (
158
+ payload. to_string ( ) ,
159
+ Self :: RESPONSE_TOPIC ,
160
+ & node. config . secret_key ,
161
+ ) ;
162
+ node. publish ( message) ?;
143
163
}
164
+
165
+ Ok ( acceptance)
144
166
}
145
167
}
0 commit comments