1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2024 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
use std::io;
19

            
20
use crate::parallel::job::{Job, JobResult};
21
use crate::parallel::worker::WorkerId;
22
use crate::util::term::{Stderr, Stdout};
23

            
24
/// Represents a message sent from the worker to the runner (running on the main thread).
25
pub enum WorkerMessage {
26
    /// Error raised when the file can't be read.
27
    IOError(IOErrorMsg),
28
    /// Error raised when the file isn't a valid Hurl content.
29
    ParsingError(ParsingErrorMsg),
30
    /// Sent when the Hurl file is in progress (file has been parsed and HTTP exchanges have started).
31
    Running(RunningMsg),
32
    /// Sent when the Hurl file is completed, whether successful or failed.
33
    Completed(CompletedMsg),
34
    /// Sent when worker thread is disconnected (for graceful shutdown).
35
    ShutDown,
36
}
37

            
38
/// A message sent from worker to runner when the input file can't be read.
39
pub struct IOErrorMsg {
40
    /// Identifier of the worker sending this message.
41
    #[allow(dead_code)]
42
    pub worker_id: WorkerId,
43
    /// Job originator of this message.
44
    pub job: Job,
45
    /// Inner error that has triggered this message.
46
    pub error: io::Error,
47
}
48

            
49
impl IOErrorMsg {
50
    /// Creates a new I/O error message.
51
3
    pub fn new(worker_id: WorkerId, job: &Job, error: io::Error) -> Self {
52
3
        IOErrorMsg {
53
3
            worker_id,
54
3
            job: job.clone(),
55
3
            error,
56
        }
57
    }
58
}
59

            
60
/// A message sent from worker to runner when the input file can't be parsed.
61
pub struct ParsingErrorMsg {
62
    /// Identifier of the worker sending this message.
63
    #[allow(dead_code)]
64
    pub worker_id: WorkerId,
65
    /// Job originator of this message.
66
    #[allow(dead_code)]
67
    pub job: Job,
68
    /// Standard error of the worker for this job.
69
    pub stderr: Stderr,
70
}
71

            
72
impl ParsingErrorMsg {
73
    /// Creates a new parsing error message.
74
3
    pub fn new(worker_id: WorkerId, job: &Job, stderr: &Stderr) -> Self {
75
3
        ParsingErrorMsg {
76
3
            worker_id,
77
3
            job: job.clone(),
78
3
            stderr: stderr.clone(),
79
        }
80
    }
81
}
82

            
83
/// A message sent from worker to runner at regular time to inform that the job is being run.
84
pub struct RunningMsg {
85
    /// Identifier of the worker sending this message.
86
    pub worker_id: WorkerId,
87
    /// Job originator of this message.
88
    pub job: Job,
89
    /// 0-based index of the current entry.
90
    pub entry_index: usize,
91
    /// Number of entries
92
    pub entry_count: usize,
93
}
94

            
95
impl RunningMsg {
96
    /// Creates a new running message: the job is in progress.
97
1626
    pub fn new(worker_id: WorkerId, job: &Job, entry_index: usize, entry_count: usize) -> Self {
98
1626
        RunningMsg {
99
1626
            worker_id,
100
1626
            job: job.clone(),
101
1626
            entry_index,
102
1626
            entry_count,
103
        }
104
    }
105
}
106

            
107
/// A message sent from worker to runner when a Hurl file has completed, whether successful or not.
108
pub struct CompletedMsg {
109
    /// Identifier of the worker sending this message.
110
    pub worker_id: WorkerId,
111
    /// Result execution of the originator job, can successful or failed.
112
    pub result: JobResult,
113
    /// Standard output of the worker for this job.
114
    pub stdout: Stdout,
115
    /// Standard error of the worker for this job.
116
    pub stderr: Stderr,
117
}
118

            
119
impl CompletedMsg {
120
    /// Creates a new completed message: the job has completed, successfully or not.
121
780
    pub fn new(worker_id: WorkerId, result: JobResult, stdout: Stdout, stderr: Stderr) -> Self {
122
780
        CompletedMsg {
123
780
            worker_id,
124
780
            result,
125
780
            stdout,
126
780
            stderr,
127
        }
128
    }
129
}