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
//  SPEC.rs
//    by Lut99
//
//  Created:
//    24 Oct 2022, 16:42:17
//  Last edited:
//    12 Apr 2023, 12:57:54
//  Auto updated?
//    Yes
//
//  Description:
//!   Defines (public) interfaces and structs for the `brane-tsk` crate.
//

use std::fmt::{Display, Formatter, Result as FResult};
use std::str::FromStr;

use brane_ast::Workflow;
use brane_exe::FullValue;
use log::warn;
use specifications::working::TaskStatus;
use uuid::Uuid;

use crate::errors::{ExecuteError, IdError, PlanError};


/***** HELPER MACROS *****/
/// Defines a helper macro that checks if the string is actually None before returning the value.
macro_rules! return_status {
    (JobStatus:: $status:ident, $str:ident) => {{
        if !$str.is_none() {
            warn!("Given string is not None (but it isn't used)");
        }
        Ok(JobStatus::$status)
    }};
}

/// Defines a helper macro that takes a string for a JobStatus before returning it.
macro_rules! return_status_str {
    (JobStatus:: $status:ident, $str:ident) => {{ if let Some(s) = $str { Ok(JobStatus::$status(s)) } else { Err(ExecuteError::StatusEmptyStringError { status: TaskStatus::$status }) } }};
}

/// Defines a helper macro that parses a value for a JobStatus before returning it.
macro_rules! return_status_val {
    (JobStatus:: $status:ident, $str:ident) => {{
        if let Some(s) = $str {
            match serde_json::from_str(&s) {
                Ok(val) => Ok(JobStatus::$status(val)),
                Err(err) => Err(ExecuteError::StatusValueParseError { status: TaskStatus::$status, raw: s, err }),
            }
        } else {
            Err(ExecuteError::StatusEmptyStringError { status: TaskStatus::$status })
        }
    }};
}

/// Defines a helper macro that parses a code, stdout, stderr triplet for a JobStatus before returning it.
macro_rules! return_status_failed {
    (JobStatus:: $status:ident, $str:ident) => {{
        if let Some(s) = $str {
            match serde_json::from_str::<(i32, String, String)>(&s) {
                Ok((code, stdout, stderr)) => Ok(JobStatus::$status(code, stdout, stderr)),
                Err(err) => Err(ExecuteError::StatusTripletParseError { status: TaskStatus::$status, raw: s, err }),
            }
        } else {
            Err(ExecuteError::StatusEmptyStringError { status: TaskStatus::$status })
        }
    }};
}





/***** LIBRARY *****/
/// Special constant that marks it needs to be run on the local machine.
pub const LOCALHOST: &str = "localhost";



/// Defines an application identifier, which is used to identify... applications... (wow)
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct AppId(Uuid);

impl AppId {
    /// Generate a new AppId.
    ///
    /// # Returns
    /// A new instance of a AppId that is practically unique.
    pub fn generate() -> Self { Self(Uuid::new_v4()) }
}

impl From<&AppId> for AppId {
    #[inline]
    fn from(value: &AppId) -> Self { value.clone() }
}
impl AsRef<AppId> for AppId {
    #[inline]
    fn as_ref(&self) -> &AppId { self }
}

impl From<AppId> for String {
    #[inline]
    fn from(value: AppId) -> Self { Self::from(&value) }
}
impl From<&AppId> for String {
    #[inline]
    fn from(value: &AppId) -> Self { value.0.to_string() }
}
impl Display for AppId {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult { write!(f, "{}", self.0) }
}

impl FromStr for AppId {
    type Err = IdError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match Uuid::from_str(value) {
            Ok(uuid) => Ok(Self(uuid)),
            Err(err) => Err(IdError::ParseError { what: "AppId", raw: value.into(), err }),
        }
    }
}



/// Defines a unique identifier used to distinguish individual task submissions within a coherent workflow.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct TaskId(Uuid);

impl TaskId {
    /// Generate a new AppId.
    ///
    /// # Returns
    /// A new instance of a AppId that is practically unique.
    pub fn generate() -> Self { Self(Uuid::new_v4()) }
}

impl From<&TaskId> for TaskId {
    #[inline]
    fn from(value: &TaskId) -> Self { value.clone() }
}
impl AsRef<TaskId> for TaskId {
    #[inline]
    fn as_ref(&self) -> &TaskId { self }
}

impl From<TaskId> for String {
    #[inline]
    fn from(value: TaskId) -> Self { Self::from(&value) }
}
impl From<&TaskId> for String {
    #[inline]
    fn from(value: &TaskId) -> Self { value.0.to_string() }
}
impl Display for TaskId {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult { write!(f, "{}", self.0) }
}

impl FromStr for TaskId {
    type Err = IdError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match Uuid::from_str(value) {
            Ok(uuid) => Ok(Self(uuid)),
            Err(err) => Err(IdError::ParseError { what: "TaskId", raw: value.into(), err }),
        }
    }
}



/// Defines a common interface for planners. This is mostly for software engineering reasons, and not really due to the need to have them interchangeable.
#[async_trait::async_trait]
pub trait Planner {
    /// Plans the given workflow by:
    /// - resolving every `at` at every Node to have a location that makes sense for this instance; and
    /// - populating the matching RuntimeDataIndex, that hosts information on accessing both datasets and intermediate results.
    ///
    /// # Arguments
    /// - `workflow`: The workflow to plan.
    ///
    /// # Returns
    /// A tuple of same workflow, but now with planned nodes, and the new RuntimeDataIndex.
    async fn plan(&self, workflow: Workflow) -> Result<Workflow, PlanError>;
}



/// Defines the possible states a job can have.
#[derive(Clone, Debug)]
pub enum JobStatus {
    // Meta events
    /// No status yet / unknown
    Unknown,

    // Job events
    /// The job has been received by the job node.
    Received,

    // Checker events
    /// The job has been authorized by the job's checker(s).
    Authorized,
    /// The job has been denied by the job's checker(s).
    Denied,
    /// Authorization has failed.
    AuthorizationFailed(String),

    // Creation events
    /// The job container has been created.
    Created,
    /// We failed to create the job container.
    CreationFailed(String),

    // Initialization events
    /// The branelet has been booted (first event it sends).
    Ready,
    /// The branelet node has been initialized; now only to spawn the job itself.
    Initialized,
    /// We failed to initialize branelet.
    InitializationFailed(String),
    /// The actual subcall executeable / script has started
    Started,
    /// The subprocess executable did not want to start (calling it failed)
    StartingFailed(String),

    // Progress events
    /// Occassional message to let the user know the container is alive and running
    Heartbeat,
    /// The package call went successfully from the branelet's side
    Completed,
    /// The package call went wrong from the branelet's side
    CompletionFailed(String),

    // Finish events
    /// The container has exited with a zero status code (and returned the given value, which may be Void)
    Finished(FullValue),
    /// The container was interrupted by the Job node
    Stopped,
    /// brane-let could not decode the output from the package call
    DecodingFailed(String),
    /// The container has exited with a non-zero status code
    Failed(i32, String, String),
}

impl JobStatus {
    /// Attempts to parse the given status & value into a JobStatus.
    ///
    /// # Arguments
    /// - `status`: The ExecuteState that provides the wire status.
    /// - `value`: The optional String value that we will parse to values or errors.
    ///
    /// # Returns
    /// A new JobStatus instance.
    ///
    /// # Errors
    /// This function errors if we failed to parse the string, or it was None when we expected Some.
    pub fn from_status(status: TaskStatus, value: Option<String>) -> Result<Self, ExecuteError> {
        // Match on the status
        use TaskStatus::*;
        match status {
            Unknown => {
                return_status!(JobStatus::Unknown, value)
            },

            Received => {
                return_status!(JobStatus::Received, value)
            },

            Authorized => {
                return_status!(JobStatus::Authorized, value)
            },
            Denied => {
                return_status!(JobStatus::Denied, value)
            },
            AuthorizationFailed => {
                return_status_str!(JobStatus::AuthorizationFailed, value)
            },

            Created => {
                return_status!(JobStatus::Created, value)
            },
            CreationFailed => {
                return_status_str!(JobStatus::CreationFailed, value)
            },

            Ready => {
                return_status!(JobStatus::Ready, value)
            },
            Initialized => {
                return_status!(JobStatus::Initialized, value)
            },
            InitializationFailed => {
                return_status_str!(JobStatus::InitializationFailed, value)
            },
            Started => {
                return_status!(JobStatus::Started, value)
            },
            StartingFailed => {
                return_status_str!(JobStatus::StartingFailed, value)
            },

            Heartbeat => {
                return_status!(JobStatus::Heartbeat, value)
            },
            Completed => {
                return_status!(JobStatus::Completed, value)
            },
            CompletionFailed => {
                return_status_str!(JobStatus::CompletionFailed, value)
            },

            Finished => {
                return_status_val!(JobStatus::Finished, value)
            },
            Stopped => {
                return_status!(JobStatus::Stopped, value)
            },
            DecodingFailed => {
                return_status_str!(JobStatus::DecodingFailed, value)
            },
            Failed => {
                return_status_failed!(JobStatus::Failed, value)
            },
        }
    }

    /// Returns whether this status is a heartbeat.
    #[inline]
    pub fn is_heartbeat(&self) -> bool { matches!(self, Self::Heartbeat) }

    /// Converts the JobStatus into some 'progress index', which is a number that can be used to determine if some JobStatus logically should be send after another.
    ///
    /// # Returns
    /// A number representing the progress index. If it's higher than that of another JobStatus, this indicates its part of a later 'step' in the process.
    pub fn progress_index(&self) -> u32 {
        use JobStatus::*;
        match self {
            Unknown => 0,

            Received => 1,

            Authorized => 2,
            Denied => 2,
            AuthorizationFailed(_) => 2,

            Created => 3,
            CreationFailed(_) => 3,

            Ready => 4,
            Initialized => 5,
            InitializationFailed(_) => 5,
            Started => 6,
            StartingFailed(_) => 6,

            Heartbeat => 7,
            Completed => 8,
            CompletionFailed(_) => 8,

            DecodingFailed(_) => 9,
            Finished(_) => 10,
            Stopped => 10,
            Failed(_, _, _) => 10,
        }
    }
}

impl PartialEq for JobStatus {
    fn eq(&self, other: &Self) -> bool { TaskStatus::from(self) as i32 == TaskStatus::from(other) as i32 }
}

impl From<JobStatus> for TaskStatus {
    fn from(value: JobStatus) -> Self { Self::from(&value) }
}
impl From<&JobStatus> for TaskStatus {
    fn from(value: &JobStatus) -> Self {
        use JobStatus::*;
        match value {
            Unknown => Self::Unknown,

            Received => Self::Received,

            Authorized => Self::Authorized,
            Denied => Self::Denied,
            AuthorizationFailed(_) => Self::AuthorizationFailed,

            Created => Self::Created,
            CreationFailed(_) => Self::CreationFailed,

            Ready => Self::Ready,
            Initialized => Self::Initialized,
            InitializationFailed(_) => Self::InitializationFailed,
            Started => Self::Started,
            StartingFailed(_) => Self::StartingFailed,

            Heartbeat => Self::Heartbeat,
            Completed => Self::Completed,
            CompletionFailed(_) => Self::CompletionFailed,

            Finished(_) => Self::Finished,
            Stopped => Self::Stopped,
            DecodingFailed(_) => Self::DecodingFailed,
            Failed(_, _, _) => Self::Failed,
        }
    }
}

impl From<JobStatus> for (TaskStatus, Option<String>) {
    fn from(value: JobStatus) -> Self { Self::from(&value) }
}
impl From<&JobStatus> for (TaskStatus, Option<String>) {
    fn from(value: &JobStatus) -> Self {
        use JobStatus::*;
        match value {
            Unknown => (TaskStatus::Unknown, None),

            Received => (TaskStatus::Received, None),

            Authorized => (TaskStatus::Authorized, None),
            Denied => (TaskStatus::Denied, None),
            AuthorizationFailed(err) => (TaskStatus::AuthorizationFailed, Some(err.clone())),

            Created => (TaskStatus::Created, None),
            CreationFailed(err) => (TaskStatus::CreationFailed, Some(err.clone())),

            Ready => (TaskStatus::Ready, None),
            Initialized => (TaskStatus::Initialized, None),
            InitializationFailed(err) => (TaskStatus::InitializationFailed, Some(err.clone())),
            Started => (TaskStatus::Started, None),
            StartingFailed(err) => (TaskStatus::StartingFailed, Some(err.clone())),

            Heartbeat => (TaskStatus::Heartbeat, None),
            Completed => (TaskStatus::Completed, None),
            CompletionFailed(err) => (TaskStatus::CompletionFailed, Some(err.clone())),

            Finished(val) => (TaskStatus::Finished, Some(serde_json::to_string(&val).unwrap())),
            Stopped => (TaskStatus::Stopped, None),
            DecodingFailed(err) => (TaskStatus::DecodingFailed, Some(err.clone())),
            Failed(code, stdout, stderr) => (TaskStatus::Failed, Some(serde_json::to_string(&(code, stdout, stderr)).unwrap())),
        }
    }
}