Skip to content

Commit

Permalink
feat: combine ops and workers into one svc type
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterPtato committed Jul 5, 2024
1 parent b0e67b0 commit 1234ce5
Show file tree
Hide file tree
Showing 373 changed files with 89 additions and 82 deletions.
9 changes: 5 additions & 4 deletions lib/bolt/config/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ pub enum ServiceKind {
#[serde(rename = "operation")]
Operation {},

#[serde(rename = "operations")]
Operations {},
/// A collection of operations and workflows/workers.
#[serde(rename = "package")]
Package {},

// TODO: Rename to worker
#[serde(rename = "consumer")]
Expand Down Expand Up @@ -341,7 +342,7 @@ impl ServiceKind {
ServiceKind::Static { .. } => "static",
ServiceKind::Database { .. } => "database",
ServiceKind::Cache { .. } => "cache",
ServiceKind::Operations { .. } => "operations",
ServiceKind::Package { .. } => "package",
}
}

Expand All @@ -354,7 +355,7 @@ impl ServiceKind {
| ServiceKind::Api { .. } => ComponentClass::Executable,

ServiceKind::Operation { .. }
| ServiceKind::Operations { .. }
| ServiceKind::Package { .. }
| ServiceKind::Consumer { .. }
| ServiceKind::ApiRoutes { .. } => ComponentClass::NonExecutable,
ServiceKind::Database { .. } => ComponentClass::Database,
Expand Down
56 changes: 26 additions & 30 deletions lib/bolt/core/src/context/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,51 +392,47 @@ impl ProjectContextData {
let pkgs_path = workspace_path.join("pkg");
let mut pkg_dir = fs::read_dir(&pkgs_path).await.unwrap();
while let Some(pkg) = pkg_dir.next_entry().await.unwrap() {
// Read worker
let worker_path = pkg.path().join("worker");
if fs::metadata(&worker_path.join("Service.toml"))
.await
.is_ok()
{
// Load the service
// Check if pkg-level service config exists
if fs::metadata(pkg.path().join("Service.toml")).await.is_ok() {
// Load the directory as a single crate
let svc_ctx = context::service::ServiceContextData::from_path(
Weak::new(),
svc_ctxs_map,
&workspace_path,
&worker_path,
)
.await
.unwrap();
svc_ctxs_map.insert(svc_ctx.name(), svc_ctx.clone());
}

// Read standalone
Self::load_services_dir(svc_ctxs_map, &workspace_path, pkg.path().join("standalone"))
.await;

// Read ops
// Check if service config exists
if fs::metadata(pkg.path().join("ops").join("Service.toml"))
.await
.is_ok()
{
// Load the ops directory as a single service
let svc_ctx = context::service::ServiceContextData::from_path(
Weak::new(),
svc_ctxs_map,
&workspace_path,
&pkg.path().join("ops"),
&pkg.path(),
)
.await
.unwrap();

svc_ctxs_map.insert(svc_ctx.name(), svc_ctx.clone());
} else {
// Read worker
let worker_path = pkg.path().join("worker");
if fs::metadata(&worker_path.join("Service.toml"))
.await
.is_ok()
{
// Load the service
let svc_ctx = context::service::ServiceContextData::from_path(
Weak::new(),
svc_ctxs_map,
&workspace_path,
&worker_path,
)
.await
.unwrap();
svc_ctxs_map.insert(svc_ctx.name(), svc_ctx.clone());
}

// Load all individual ops
Self::load_services_dir(svc_ctxs_map, &workspace_path, pkg.path().join("ops"))
.await;
}

// Read standalone
Self::load_services_dir(svc_ctxs_map, &workspace_path, pkg.path().join("standalone"))
.await;

// Read dbs
Self::load_services_dir(svc_ctxs_map, &workspace_path, pkg.path().join("db")).await;

Expand Down
6 changes: 3 additions & 3 deletions lib/bolt/core/src/context/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,15 +516,15 @@ impl ServiceContextData {
dep.config().kind,
ServiceKind::Database { .. }
| ServiceKind::Cache { .. } | ServiceKind::Operation { .. }
| ServiceKind::Operations { .. }
| ServiceKind::Package { .. }
| ServiceKind::Consumer { .. }
)
} else if matches!(self.config().kind, ServiceKind::Api { .. }) {
matches!(
dep.config().kind,
ServiceKind::Database { .. }
| ServiceKind::Cache { .. } | ServiceKind::Operation { .. }
| ServiceKind::Operations { .. }
| ServiceKind::Package { .. }
| ServiceKind::ApiRoutes { .. }
| ServiceKind::Consumer { .. }
)
Expand All @@ -533,7 +533,7 @@ impl ServiceContextData {
dep.config().kind,
ServiceKind::Database { .. }
| ServiceKind::Cache { .. } | ServiceKind::Operation { .. }
| ServiceKind::Operations { .. }
| ServiceKind::Package { .. }
| ServiceKind::Consumer { .. }
)
};
Expand Down
2 changes: 1 addition & 1 deletion lib/bolt/core/src/dep/k8s/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub async fn gen_svc(exec_ctx: &ExecServiceContext) -> Vec<serde_json::Value> {
ServiceKind::Oneshot { .. } => SpecType::Job,
ServiceKind::Periodic { .. } => SpecType::CronJob,
ServiceKind::Operation { .. }
| ServiceKind::Operations { .. }
| ServiceKind::Package { .. }
| ServiceKind::Database { .. }
| ServiceKind::Cache { .. }
| ServiceKind::ApiRoutes { .. } => {
Expand Down
57 changes: 32 additions & 25 deletions lib/bolt/core/src/tasks/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,6 @@ async fn generate_root(path: &Path) {
let _ = fs::remove_file(pkg.path().join("Cargo.lock")).await;
let _ = fs::remove_file(pkg.path().join("ops").join("Cargo.lock")).await;

// Check worker
let worker_path = pkg.path().join("worker");
if fs::metadata(&worker_path).await.is_ok() {
workspace_members.push(format!(
r#""pkg/{pkg}/worker""#,
pkg = pkg.file_name().into_string().unwrap(),
));

// Remove services' Cargo.lock files in favor of the shared svc
// Cargo.toml
let _ = fs::remove_file(worker_path.join("Cargo.lock")).await;

set_license(&worker_path.join("Cargo.toml")).await;
}

// Iterate through `standalone` folder
let standalone_path = pkg.path().join("standalone");
if fs::metadata(&standalone_path).await.is_ok() {
Expand All @@ -131,21 +116,37 @@ async fn generate_root(path: &Path) {
}

// Check if service config exists
if fs::metadata(pkg.path().join("ops").join("Service.toml"))
.await
.is_ok()
{
if fs::metadata(pkg.path().join("Service.toml")).await.is_ok() {
workspace_members.push(format!(
r#""pkg/{pkg}/ops""#,
r#""pkg/{pkg}""#,
pkg = pkg.file_name().into_string().unwrap(),
));

let _ = fs::remove_file(pkg.path().join("ops").join("Cargo.lock")).await;
let _ = fs::remove_file(pkg.path().join("Cargo.lock")).await;

set_license(&pkg.path().join("ops").join("Cargo.toml")).await;
}
// Iterate through `ops` folder
else {
set_license(&pkg.path().join("Cargo.toml")).await;

let types_path = pkg.path().join("types");
if fs::metadata(&types_path).await.is_ok() {
set_license(&types_path.join("Cargo.toml")).await;
}
} else {
// Check worker
let worker_path = pkg.path().join("worker");
if fs::metadata(&worker_path).await.is_ok() {
workspace_members.push(format!(
r#""pkg/{pkg}/worker""#,
pkg = pkg.file_name().into_string().unwrap(),
));

// Remove services' Cargo.lock files in favor of the shared svc
// Cargo.toml
let _ = fs::remove_file(worker_path.join("Cargo.lock")).await;

set_license(&worker_path.join("Cargo.toml")).await;
}

// Iterate through `ops` folder
let ops_path = pkg.path().join("ops");
if fs::metadata(&ops_path).await.is_ok() {
let mut dir = fs::read_dir(ops_path).await.unwrap();
Expand All @@ -166,6 +167,12 @@ async fn generate_root(path: &Path) {
}
}
}

// Utils lib
let util_path = pkg.path().join("util");
if fs::metadata(&util_path).await.is_ok() {
set_license(&util_path.join("Cargo.toml")).await;
}
}
}

Expand Down
34 changes: 18 additions & 16 deletions lib/bolt/core/src/tasks/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,22 @@ pub async fn generate(ctx: &mut ProjectContext, opts: TemplateOpts) -> Result<()
);
}

// Check for new operations service type
if matches!(template_type, TemplateType::Operation)
&& fs::metadata(
base_path
.join("svc")
.join("pkg")
.join(&pkg_name)
.join("Service.toml"),
)
.await
.is_ok()
// Check for package service type
if matches!(
template_type,
TemplateType::Operation | TemplateType::Worker
) && fs::metadata(
base_path
.join("svc")
.join("pkg")
.join(&pkg_name)
.join("Service.toml"),
)
.await
.is_ok()
{
bail!(
"Creating operations in new `operations` service type ({pkg_name}/ops) not yet supported.",
"Creating operations or workers in the new refactored packages ({pkg_name}) is not yet supported.",
);
}

Expand Down Expand Up @@ -193,7 +195,7 @@ pub async fn generate(ctx: &mut ProjectContext, opts: TemplateOpts) -> Result<()
.join("svc")
.join("pkg")
.join(pkg_name)
.join("types")
.join("proto")
.join("msg");
let proto_file_path = proto_path.join(format!("{}.proto", service_name));

Expand Down Expand Up @@ -227,7 +229,7 @@ pub async fn generate(ctx: &mut ProjectContext, opts: TemplateOpts) -> Result<()
base_path
.join("svc")
.join("templates")
.join("types")
.join("proto")
.join("msg")
.join("{{ name }}.proto"),
proto_path,
Expand All @@ -240,7 +242,7 @@ pub async fn generate(ctx: &mut ProjectContext, opts: TemplateOpts) -> Result<()
.join("svc")
.join("pkg")
.join(pkg_name)
.join("types");
.join("proto");
let proto_file_path = proto_path.join(format!("{}.proto", service_name));
if fs::metadata(&proto_file_path).await.is_ok() {
bail!(
Expand All @@ -260,7 +262,7 @@ pub async fn generate(ctx: &mut ProjectContext, opts: TemplateOpts) -> Result<()
base_path
.join("svc")
.join("templates")
.join("types")
.join("proto")
.join("{{ name }}.proto"),
proto_path,
)
Expand Down
2 changes: 1 addition & 1 deletion lib/types/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where
for project_root in &project_roots {
for entry in fs::read_dir(project_root.join("svc").join("pkg"))? {
let entry = entry?;
let proto_path = entry.path().join("types");
let proto_path = entry.path().join("proto");

if proto_path.is_dir() {
println!("cargo:rerun-if-changed={}", proto_path.display());
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion svc/pkg/foo/worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use chirp_workflow::prelude::*;
pub mod workflows;

pub fn registry() -> Registry {
use workflows::*;

let mut registry = Registry::new();
registry.register_workflow::<workflows::test::Test>();
registry.register_workflow::<test::Test>();

registry
}
1 change: 0 additions & 1 deletion svc/pkg/foo/worker/src/workflows/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use chirp_workflow::prelude::*;
use serde_json::json;

#[derive(Debug, Serialize, Deserialize)]
pub struct TestInput {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 1234ce5

Please sign in to comment.