1use gpui::Context;
2use project::TaskSourceKind;
3use remote::ConnectionState;
4use task::{ResolvedTask, TaskContext, TaskTemplate};
5
6use crate::Workspace;
7
8pub fn schedule_task(
9 workspace: &mut Workspace,
10 task_source_kind: TaskSourceKind,
11 task_to_resolve: &TaskTemplate,
12 task_cx: &TaskContext,
13 omit_history: bool,
14 cx: &mut Context<Workspace>,
15) {
16 match workspace.project.read(cx).ssh_connection_state(cx) {
17 None | Some(ConnectionState::Connected) => {}
18 Some(
19 ConnectionState::Connecting
20 | ConnectionState::Disconnected
21 | ConnectionState::HeartbeatMissed
22 | ConnectionState::Reconnecting,
23 ) => {
24 log::warn!("Cannot schedule tasks when disconnected from a remote host");
25 return;
26 }
27 }
28
29 if let Some(spawn_in_terminal) =
30 task_to_resolve.resolve_task(&task_source_kind.to_id_base(), task_cx)
31 {
32 schedule_resolved_task(
33 workspace,
34 task_source_kind,
35 spawn_in_terminal,
36 omit_history,
37 cx,
38 );
39 }
40}
41
42pub fn schedule_resolved_task(
43 workspace: &mut Workspace,
44 task_source_kind: TaskSourceKind,
45 mut resolved_task: ResolvedTask,
46 omit_history: bool,
47 cx: &mut Context<Workspace>,
48) {
49 if let Some(spawn_in_terminal) = resolved_task.resolved.take() {
50 if !omit_history {
51 resolved_task.resolved = Some(spawn_in_terminal.clone());
52 workspace.project().update(cx, |project, cx| {
53 if let Some(task_inventory) =
54 project.task_store().read(cx).task_inventory().cloned()
55 {
56 task_inventory.update(cx, |inventory, _| {
57 inventory.task_scheduled(task_source_kind, resolved_task);
58 })
59 }
60 });
61 }
62
63 cx.emit(crate::Event::SpawnTask {
64 action: Box::new(spawn_in_terminal),
65 });
66 }
67}