MCPcopy Create free account
hub / github.com/Botloader/botloader / handle_create_scripts_vm

Method handle_create_scripts_vm

components/vmworker/src/lib.rs:277–351  ·  view source on GitHub ↗
(
        &mut self,
        req: CreateScriptsVmReq,
    )

Source from the content-addressed store, hash-verified

275
276 #[instrument(skip_all)]
277 async fn handle_create_scripts_vm(
278 &mut self,
279 req: CreateScriptsVmReq,
280 ) -> anyhow::Result<ContinueState> {
281 if self.current_state.is_some() {
282 self.wait_shutdown_current_vm().await?;
283 };
284
285 {
286 // update premium tier
287 let mut w = self.premium_tier.write().unwrap();
288 *w = req.premium_tier;
289 }
290
291 // if let Some(current) = &self.current_state {
292 // // we were already running a vm for this guild, issue a restart command with the new scripts instead
293 // // TODO: there is a possibility of a race condition here
294 // // we could receive a "completed" event here we handle after this and since we send a ack back
295 // // stuff could go wrong...
296 // let _ = current.scripts_vm.send(VmCommand::Restart(req.scripts));
297 // self.write_message(WorkerMessage::Ack(req.seq)).await?;
298 // return Ok(ContinueState::Continue);
299 // }
300
301 let (vm_cmd_tx, vm_cmd_rx) = mpsc::unbounded_channel();
302 let (vm_evt_tx, vm_evt_rx) = mpsc::unbounded_channel();
303
304 let rt_ctx = CreateRuntimeContext {
305 bot_state: self.broker_client.clone(),
306 discord_config: self.discord_config.clone(),
307 guild_id: Some(req.guild_id),
308 guild_logger: self.guild_logger.with_guild(req.guild_id),
309 script_http_client_proxy: self.user_http_proxy.clone(),
310 premium_tier: self.premium_tier.clone(),
311 main_tokio_runtime: tokio::runtime::Handle::current(),
312
313 settings_values: req
314 .scripts
315 .iter()
316 .filter(|v| !v.settings_values.is_empty())
317 .map(|v| ScriptSettingsValues {
318 script_id: v.id,
319 settings_values: v.settings_values.clone(),
320 })
321 .collect(),
322
323 db: self.stores.clone(),
324 event_tx: self.runtime_evt_tx.clone(),
325 };
326
327 let vmthread = vm::vmthread::spawn_vm_thread(
328 CreateRt {
329 guild_logger: self.guild_logger.with_guild(req.guild_id),
330 rx: vm_cmd_rx,
331 tx: vm_evt_tx,
332 load_scripts: req.scripts,
333
334 extension_factory: Box::new(move || runtime::create_extensions(rt_ctx.clone())),

Callers 1

handle_scheduler_cmdMethod · 0.80

Calls 7

spawn_vm_threadFunction · 0.85
create_extensionsFunction · 0.85
create_module_mapFunction · 0.85
with_guildMethod · 0.80
write_messageMethod · 0.80
writeMethod · 0.65

Tested by

no test coverage detected