Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
R
RoboPLC
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄新宇
RoboPLC
Commits
79fb7822
提交
79fb7822
authored
3月 15, 2024
作者:
Serhij S
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
graceful shutdown example
上级
4d542bdf
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
123 行增加
和
16 行删除
+123
-16
Cargo.toml
Cargo.toml
+5
-0
shutdown.rs
examples/shutdown.rs
+101
-0
controller.rs
src/controller.rs
+17
-16
没有找到文件。
Cargo.toml
浏览文件 @
79fb7822
...
...
@@ -38,6 +38,7 @@ tracing = "0.1.40"
env_logger
=
"0.11.3"
insta
=
"1.36.1"
log
=
"0.4.21"
signal-hook
=
"0.3.17"
tokio
=
{
version
=
"1.36.0"
,
features
=
[
"rt"
,
"macros"
,
"time"
]
}
tracing
=
{
version
=
"0.1.40"
,
features
=
["log"]
}
...
...
@@ -48,3 +49,7 @@ path = "examples/plc-modbus.rs"
[[example]]
name
=
"raw-udp"
path
=
"examples/raw-udp.rs"
[[example]]
name
=
"shutdown"
path
=
"examples/shutdown.rs"
examples/shutdown.rs
0 → 100644
浏览文件 @
79fb7822
// The example provides a graceful shutdown of the controller.
use
roboplc
::{
prelude
::
*
,
time
::
interval
};
use
signal_hook
::{
consts
::{
SIGINT
,
SIGTERM
},
iterator
::
Signals
,
};
use
tracing
::
info
;
#[derive(DataPolicy,
Clone)]
enum
Message
{
Data
(
u8
),
// Terminate signal for workers which listen on hub events
Terminate
,
}
#[derive(WorkerOpts)]
#[worker_opts(name
=
"parser"
)]
struct
DataParser
{}
impl
Worker
<
Message
,
()
>
for
DataParser
{
fn
run
(
&
mut
self
,
context
:
&
Context
<
Message
,
()
>
)
->
WResult
{
let
hc
=
context
.hub
()
.register
(
self
.worker_name
(),
event_matches!
(
Message
::
Data
(
_
)
|
Message
::
Terminate
),
)
?
;
for
msg
in
hc
{
match
msg
{
Message
::
Data
(
data
)
=>
{
info!
(
worker
=
self
.worker_name
(),
data
=
data
);
}
// This worker terminates itself when it receives the Terminate message
Message
::
Terminate
=>
{
break
;
}
}
}
Ok
(())
}
}
#[derive(WorkerOpts)]
#[worker_opts(name
=
"generator"
)]
struct
DataGenerator
{}
impl
Worker
<
Message
,
()
>
for
DataGenerator
{
fn
run
(
&
mut
self
,
context
:
&
Context
<
Message
,
()
>
)
->
WResult
{
for
_
in
interval
(
Duration
::
from_secs
(
1
))
{
context
.hub
()
.send
(
Message
::
Data
(
42
));
// This worker terminates itself when the controller goes to the stopping state
if
!
context
.is_online
()
{
break
;
}
}
Ok
(())
}
}
#[derive(WorkerOpts)]
#[worker_opts(name
=
"sighandle"
)]
struct
SignalHandler
{}
impl
Worker
<
Message
,
()
>
for
SignalHandler
{
// this worker listens to SIGINT and SIGTERM signals, sends a Terminate message to the hub and
// sets the controller state to Stopping
fn
run
(
&
mut
self
,
context
:
&
Context
<
Message
,
()
>
)
->
WResult
{
let
mut
signals
=
Signals
::
new
([
SIGTERM
,
SIGINT
])
?
;
if
let
Some
(
sig
)
=
signals
.forever
()
.next
()
{
match
sig
{
SIGTERM
|
SIGINT
=>
{
info!
(
"terminating"
);
// it is really important to set max shutdown timeout for the controller if the
// controller does not terminate in the given time, the process and all its
// sub-processes are forcibly killed
suicide
(
Duration
::
from_secs
(
2
),
true
);
// set controller state to terminating
context
.set_state
(
ControllerStateKind
::
Stopping
);
// send Terminate message to workers who listen to the hub
context
.hub
()
.send
(
Message
::
Terminate
);
}
_
=>
unreachable!
(),
}
}
Ok
(())
}
}
fn
main
()
->
Result
<
(),
Box
<
dyn
std
::
error
::
Error
>>
{
env_logger
::
builder
()
.filter_level
(
log
::
LevelFilter
::
Info
)
.init
();
let
mut
controller
=
Controller
::
<
Message
,
()
>
::
new
();
controller
.spawn_worker
(
DataGenerator
{})
?
;
controller
.spawn_worker
(
DataParser
{})
?
;
controller
.spawn_worker
(
SignalHandler
{})
?
;
info!
(
"controller started"
);
controller
.block
();
info!
(
"controller terminated"
);
Ok
(())
}
src/controller.rs
浏览文件 @
79fb7822
...
...
@@ -35,20 +35,20 @@ pub struct State {
impl
State
{
pub
fn
new
()
->
Self
{
Self
{
state
:
AtomicI8
::
new
(
StateKind
::
Starting
as
i8
)
.into
(),
state
:
AtomicI8
::
new
(
Controller
StateKind
::
Starting
as
i8
)
.into
(),
}
}
/// Set controller state
pub
fn
set
(
&
self
,
state
:
StateKind
)
{
pub
fn
set
(
&
self
,
state
:
Controller
StateKind
)
{
self
.state
.store
(
state
as
i8
,
Ordering
::
SeqCst
);
}
/// Get controller state
pub
fn
get
(
&
self
)
->
StateKind
{
StateKind
::
from
(
self
.state
.load
(
Ordering
::
SeqCst
))
pub
fn
get
(
&
self
)
->
Controller
StateKind
{
Controller
StateKind
::
from
(
self
.state
.load
(
Ordering
::
SeqCst
))
}
/// Is the controller online (starting or running)
pub
fn
is_online
(
&
self
)
->
bool
{
self
.get
()
>=
StateKind
::
Starting
self
.get
()
>=
Controller
StateKind
::
Starting
}
}
...
...
@@ -61,7 +61,8 @@ impl Default for State {
/// Controller state kind
#[derive(Default,
Eq,
PartialEq,
Clone,
Copy,
Ord,
PartialOrd)]
#[repr(i8)]
pub
enum
StateKind
{
#[allow(clippy
::
module_name_repetitions)]
pub
enum
ControllerStateKind
{
#[default]
Starting
=
0
,
Active
=
1
,
...
...
@@ -71,14 +72,14 @@ pub enum StateKind {
Unknown
=
-
128
,
}
impl
From
<
i8
>
for
StateKind
{
impl
From
<
i8
>
for
Controller
StateKind
{
fn
from
(
v
:
i8
)
->
Self
{
match
v
{
0
=>
StateKind
::
Starting
,
1
=>
StateKind
::
Active
,
2
=>
StateKind
::
Running
,
-
100
=>
StateKind
::
Stopped
,
_
=>
StateKind
::
Unknown
,
0
=>
Controller
StateKind
::
Starting
,
1
=>
Controller
StateKind
::
Active
,
2
=>
Controller
StateKind
::
Running
,
-
100
=>
Controller
StateKind
::
Stopped
,
_
=>
Controller
StateKind
::
Unknown
,
}
}
}
...
...
@@ -173,14 +174,14 @@ where
/// Blocks until all tasks/workers are finished
pub
fn
block
(
&
mut
self
)
{
self
.supervisor
.join_all
();
self
.state
.set
(
StateKind
::
Stopped
);
self
.state
.set
(
Controller
StateKind
::
Stopped
);
}
/// Blocks until the controller goes into stopping/stopped
pub
fn
block_while_online
(
&
self
)
{
while
self
.state
.is_online
()
{
thread
::
sleep
(
SLEEP_SLEEP
);
}
self
.state
.set
(
StateKind
::
Stopped
);
self
.state
.set
(
Controller
StateKind
::
Stopped
);
}
/// Is the controller online (starting or running)
pub
fn
is_online
(
&
self
)
{
...
...
@@ -240,11 +241,11 @@ where
&
self
.variables
}
/// Controller's state
pub
fn
get_state
(
&
self
)
->
StateKind
{
pub
fn
get_state
(
&
self
)
->
Controller
StateKind
{
self
.state
.get
()
}
/// Set controller's state
pub
fn
set_state
(
&
self
,
state
:
StateKind
)
{
pub
fn
set_state
(
&
self
,
state
:
Controller
StateKind
)
{
self
.state
.set
(
state
);
}
/// Is the controller online (starting or running)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论