Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Commit 775ff5c

Browse files
committed
feat: add fal-serverless runner
1 parent c3f6fdf commit 775ff5c

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

projects/fal/src/fal/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
DbtSingularTest,
1010
)
1111
from fal.fal_script import Context, CurrentModel
12+
from fal.serverless.runner import FalServerlessRunner
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from dataclasses import dataclass, field
2+
import importlib
3+
from pathlib import Path
4+
from typing import List
5+
from fal_serverless import isolated, sync_dir
6+
import os
7+
8+
9+
@isolated()
10+
def serverless_runner(project_dir, profiles_dir, command, args: List[str] = []):
11+
from dbt.cli.main import dbtRunner
12+
13+
runner = dbtRunner()
14+
15+
cli_args = [
16+
command,
17+
"--project-dir",
18+
project_dir,
19+
"--profiles-dir",
20+
profiles_dir,
21+
]
22+
cli_args.extend(args)
23+
24+
runner.invoke(cli_args)
25+
26+
27+
@dataclass
28+
class FalServerlessRunner:
29+
project_dir: str
30+
profiles_dir: str
31+
dbt_version: str = field(init=False)
32+
data_project_dir: str = field(init=False)
33+
data_profiles_dir: str = field(init=False)
34+
35+
def _sync_directories(self):
36+
sync_dir(
37+
self.project_dir,
38+
self.data_project_dir,
39+
)
40+
sync_dir(self.profiles_dir, self.data_profiles_dir)
41+
42+
def __post_init__(self):
43+
from dbt.cli.main import dbtRunner
44+
45+
cli_args = [
46+
"--project-dir",
47+
self.project_dir,
48+
"--profiles-dir",
49+
self.profiles_dir,
50+
]
51+
runner = dbtRunner()
52+
metadata = runner.invoke(["parse"] + cli_args).result.metadata
53+
plugin_name = metadata.adapter_type
54+
55+
try:
56+
mod = importlib.import_module(f"dbt.adapters.{plugin_name}.__version__")
57+
except ImportError:
58+
raise ValueError(
59+
f"Could not determine which adapter version is being used: {plugin_name}"
60+
)
61+
62+
# easiest way to get the version, there might be better ways in the future
63+
dbt_version = f"dbt-{plugin_name}=={mod.version}"
64+
65+
self.data_project_dir = str(Path("/data") / os.path.basename(self.project_dir))
66+
self.data_profiles_dir = str(
67+
Path("/data") / os.path.basename(self.profiles_dir)
68+
)
69+
self.dbt_version = dbt_version
70+
71+
def seed(self, args: List[str] = [], sync: bool = True):
72+
if sync:
73+
self._sync_directories()
74+
75+
serverless_runner.on(requirements=[self.dbt_version])(
76+
self.data_project_dir, self.data_profiles_dir, "seed", args
77+
)
78+
79+
def run(self, args: List[str] = [], sync: bool = True):
80+
if sync:
81+
self._sync_directories()
82+
83+
serverless_runner.on(requirements=[self.dbt_version])(
84+
self.data_project_dir, self.data_profiles_dir, "run", args
85+
)

0 commit comments

Comments
 (0)