py-serverless-kit 2.3.3__py3-none-any.whl → 2.3.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- __version__ = "2.3.3"
1
+ __version__ = "2.3.4"
2
2
  __author__ = "Asif Khan @ Kavion.ai"
@@ -190,6 +190,9 @@ class Project:
190
190
  if wf.timeout:
191
191
  # Enforced by the embedded engine (the on-prem host has no function timeout above it).
192
192
  workflows[name]["timeout"] = wf.timeout
193
+ if wf.options.get("start_on_entry") is False:
194
+ # On premises: running the first task's function on its own does not start the workflow.
195
+ workflows[name]["start_on_entry"] = False
193
196
  return {
194
197
  "service": self.config.service,
195
198
  "stage": self.config.provider.stage,
@@ -56,6 +56,28 @@ SUMMARY_KEYS = (
56
56
  MAX_HISTORY = 1000
57
57
  _ID_RE = re.compile(r"^[0-9a-f]{8,64}$")
58
58
  _SAVE_INTERVAL = 0.25
59
+ NO_ENTRY: Any = object() # Executor.start: no result of the first task was given
60
+
61
+
62
+ def entry_function(definition: Dict[str, Any]) -> Optional[str]:
63
+ """The function of the first state when that state is a task, else ``None``."""
64
+ first = (definition.get("states") or {}).get(definition.get("start_at")) or {}
65
+ if first.get("type") != "task":
66
+ return None
67
+ return first.get("function") or None
68
+
69
+
70
+ def _seeded(runner: TaskRunner, function: Optional[str], result: Any) -> TaskRunner:
71
+ """``runner`` whose first call of ``function`` (the first state: it already ran) returns ``result``."""
72
+ pending = [True]
73
+
74
+ def run(name: str, data: Any) -> Any:
75
+ if pending and name == function:
76
+ pending.clear()
77
+ return result
78
+ return runner(name, data)
79
+
80
+ return run
59
81
 
60
82
 
61
83
  def _iso(ts: float) -> str:
@@ -306,8 +328,13 @@ class Executor:
306
328
  *,
307
329
  wait: bool = False,
308
330
  task_runner: Optional[TaskRunner] = None,
331
+ entry_result: Any = NO_ENTRY,
309
332
  ) -> Dict[str, Any]:
310
- """Start a run. Returns the summary at once, or the finished record with ``wait=True``."""
333
+ """Start a run. Returns the summary at once, or the finished record with ``wait=True``.
334
+
335
+ ``entry_result``: the first state's function already ran (it was called on its own); the run uses this
336
+ result for that state instead of calling the function again.
337
+ """
311
338
  spec = self.workflows.get(workflow)
312
339
  if spec is None:
313
340
  known = ", ".join(sorted(self.workflows)) or "<none>"
@@ -337,6 +364,8 @@ class Executor:
337
364
  self._runs[execution_id] = run
338
365
  self.store.save(self._public(record))
339
366
  runner = task_runner or self.task_runner
367
+ if entry_result is not NO_ENTRY:
368
+ runner = _seeded(runner, entry_function(spec.get("definition") or {}), entry_result)
340
369
  if wait:
341
370
  return self._public(self._execute(run, spec, data, runner))
342
371
  accepted = summary(record) # before the worker can change it
@@ -13,7 +13,11 @@ path you already have instead.
13
13
  ``created`` / ``deleted``. Dropping a file in the bucket directory does the same.
14
14
  * ``POST /__pskit/invoke/<function>`` calls a function.
15
15
  * ``POST /__pskit/workflows/<name>`` starts a workflow and answers ``202`` with its
16
- ``execution_id`` (``?wait=true`` waits and returns the result).
16
+ ``execution_id`` (``?wait=true`` waits and returns the result). With ``?entry=true`` the body is
17
+ ``{"input": ..., "entry_result": ...}``: the first task's function already ran (another group's host).
18
+ * A workflow whose first state is a task also starts when that function runs on its own (HTTP, ``invoke()``,
19
+ a schedule, a queue...): the run uses that call's result for the first state. ``options.start_on_entry: false``
20
+ turns this off.
17
21
  ``GET /__pskit/executions[?workflow=&status=&limit=&offset=]``,
18
22
  ``GET /__pskit/executions/<id>`` and ``POST /__pskit/executions/<id>/cancel``
19
23
  inspect and control runs; records live under ``<data_dir>/executions``.
@@ -75,7 +79,7 @@ from .context import Context
75
79
  from .cron import CronError, next_cron, rate_seconds, resolve_zone
76
80
  from .dispatch import execute
77
81
  from .event import Event, EventType, Headers, HttpRequest, Record
78
- from .executions import Executor, FileExecutionStore, valid_id
82
+ from .executions import NO_ENTRY, Executor, FileExecutionStore, entry_function, valid_id
79
83
  from .invoke import NOT_ROUTED, install_router
80
84
  from .manifest import load_manifest, set_manifest
81
85
  from .peers import HOP_HEADER, Peers, run_peer_call
@@ -879,8 +883,13 @@ class RemoteExecutor:
879
883
  def knows(self, name: str) -> bool:
880
884
  return name in self.workflows
881
885
 
882
- def start(self, name: str, data: Any, wait: bool = False, task_runner: Any = None) -> Dict[str, Any]:
883
- path = f"/__pskit/workflows/{quote(name, safe='')}" + ("?wait=true" if wait else "")
886
+ def start(
887
+ self, name: str, data: Any, wait: bool = False, task_runner: Any = None, entry_result: Any = NO_ENTRY
888
+ ) -> Dict[str, Any]:
889
+ args = (["wait=true"] if wait else []) + (["entry=true"] if entry_result is not NO_ENTRY else [])
890
+ path = f"/__pskit/workflows/{quote(name, safe='')}" + ("?" + "&".join(args) if args else "")
891
+ if entry_result is not NO_ENTRY:
892
+ data = {"input": data, "entry_result": entry_result}
884
893
  status, _headers, raw = main_request(
885
894
  self.main_url, self.token, "POST", path, dumps(data).encode("utf-8"), {"content-type": "application/json"},
886
895
  timeout=24 * 3600 if wait else 30,
@@ -902,6 +911,22 @@ class RemoteExecutor:
902
911
  return None
903
912
 
904
913
 
914
+ def _response_value(resp: Response) -> Any:
915
+ """The body of a function's HTTP answer as a workflow value: parsed JSON, else text."""
916
+ body = resp.body
917
+ if isinstance(body, (bytes, bytearray)):
918
+ try:
919
+ body = bytes(body).decode("utf-8")
920
+ except UnicodeDecodeError:
921
+ return base64.b64encode(bytes(body)).decode("ascii")
922
+ if isinstance(body, str):
923
+ try:
924
+ return json.loads(body)
925
+ except ValueError:
926
+ return body
927
+ return body
928
+
929
+
905
930
  def _filter_match(policy: Optional[Dict[str, Any]], attributes: Dict[str, Any], body: Any) -> bool:
906
931
  if not policy:
907
932
  return True
@@ -1056,6 +1081,58 @@ class Host:
1056
1081
  return name
1057
1082
  return None
1058
1083
 
1084
+ def entry_workflows(self, function: str) -> List[str]:
1085
+ """Workflows whose first state is a task of ``function``: they start when it runs on its own."""
1086
+ manifest = getattr(self, "manifest", None)
1087
+ return [
1088
+ name
1089
+ for name, wf in sorted(((manifest.workflows if manifest else None) or {}).items())
1090
+ if wf.get("start_on_entry", True) and entry_function(wf.get("definition") or {}) == function
1091
+ ]
1092
+
1093
+ def _start_entry_workflows(self, function: str, event: Event, result: Any) -> None:
1094
+ """``function`` ran on its own (not as a workflow step): start each workflow it is the first task of.
1095
+
1096
+ The run takes the call's input as the workflow input and its result as the first state's result, so the
1097
+ function does not run twice. A failed HTTP answer (status >= 400) starts nothing.
1098
+ """
1099
+ names = self.entry_workflows(function)
1100
+ executor = self.executor
1101
+ if not names or executor is None:
1102
+ return
1103
+ output = result
1104
+ if isinstance(result, Response):
1105
+ if result.status >= 400:
1106
+ return
1107
+ output = _response_value(result)
1108
+ elif isinstance(result, BatchResult):
1109
+ if not result.ok:
1110
+ return
1111
+ output = None
1112
+ if event.http is not None:
1113
+ data = event.http.json(default=None) if event.http.body else dict(event.http.query)
1114
+ elif event.records:
1115
+ bodies = [r.json(default=r.text()) for r in event.records]
1116
+ data = bodies[0] if len(bodies) == 1 else bodies
1117
+ else:
1118
+ data = event.data
1119
+ started: List[str] = []
1120
+ for name in names:
1121
+ if not executor.knows(name):
1122
+ continue
1123
+ try:
1124
+ run = executor.start(name, data, entry_result=output)
1125
+ except Exception as exc: # noqa: BLE001 - the function's own answer must not fail because of it
1126
+ self.logs.append("ERROR", "host", f"workflow {name} did not start after {function}: {exc}",
1127
+ function=function)
1128
+ continue
1129
+ execution_id = str((run or {}).get("execution_id") or "")
1130
+ started.append(execution_id)
1131
+ self.logs.append("INFO", "host", f"{function} started workflow {name} (execution {execution_id})",
1132
+ function=function)
1133
+ if isinstance(result, Response) and started:
1134
+ result.headers.setdefault("x-pskit-execution-id", ", ".join(started))
1135
+
1059
1136
  def capture_output(self) -> None:
1060
1137
  """Send this process's logging and stdout/stderr into the log buffer."""
1061
1138
  level = str(self.spec.get("log_level") or os.environ.get("PSKIT_LOG_LEVEL") or "INFO")
@@ -1152,6 +1229,11 @@ class Host:
1152
1229
  raise
1153
1230
  finally:
1154
1231
  self._record_invocation(function, event, t0, result, error)
1232
+ if event.type != EventType.WORKFLOW and self.orchestrated(function) is None:
1233
+ try:
1234
+ self._start_entry_workflows(function, event, result)
1235
+ except Exception as exc: # noqa: BLE001 - never fails the call that already succeeded
1236
+ self.logs.append("ERROR", "host", f"workflows after {function} not started: {exc}", function=function)
1155
1237
  return result
1156
1238
 
1157
1239
  def _invoke_remote(self, function: str, group: str, event: Event) -> Any:
@@ -2239,12 +2321,17 @@ class Host:
2239
2321
  except ValueError:
2240
2322
  raise ValueError("workflow input must be JSON") from None
2241
2323
  else:
2242
- data = {k: v for k, v in req.query.items() if k != "wait"} or None
2324
+ data = {k: v for k, v in req.query.items() if k not in ("wait", "entry")} or None
2325
+ entry: Dict[str, Any] = {}
2326
+ if str(req.query.get("entry") or "").lower() in ("1", "true", "yes") and isinstance(data, dict):
2327
+ # Another group's host ran the first task's function: the run starts with its result.
2328
+ entry = {"entry_result": data.get("entry_result")}
2329
+ data = data.get("input")
2243
2330
  if not wants_wait(Event(type=EventType.HTTP, http=req)):
2244
- started = executor.start(name, data)
2331
+ started = executor.start(name, data, **entry)
2245
2332
  started["status_url"] = f"/__pskit/executions/{started['execution_id']}"
2246
2333
  return self._json(202, started)
2247
- record = executor.start(name, data, wait=True)
2334
+ record = executor.start(name, data, wait=True, **entry)
2248
2335
  body = executor.result_body(record)
2249
2336
  return self._json(200 if record.get("status") == "SUCCEEDED" else 500, body)
2250
2337
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pskit",
3
- "version": "2.3.3",
3
+ "version": "2.3.4",
4
4
  "description": "py-serverless-kit runtime for Node.js and TypeScript handlers (vendored into every bundle).",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: py-serverless-kit
3
- Version: 2.3.3
3
+ Version: 2.3.4
4
4
  Summary: Write a Python serverless function once and deploy it to AWS, Azure, GCP, 7 more clouds, or an on-premises host with one YAML file, powered by Terraform.
5
5
  Project-URL: Homepage, https://github.com/ak9347128658/serverless_kit
6
6
  Project-URL: Documentation, https://github.com/ak9347128658/serverless_kit/tree/main/docs
@@ -1,6 +1,6 @@
1
1
  py_serverless_kit/__init__.py,sha256=S-Cyucn4rdyF0CTv274nSk22vJ6w8PR2yzczIsYUJ_8,1452
2
2
  py_serverless_kit/__main__.py,sha256=efQ6c0zP-ZF8iu2fR2QkuX5LVQTXyn3O-xZlq7cKX5A,38
3
- py_serverless_kit/_version.py,sha256=qBzYFOGddEfQhpo03NIVhsH1GLaiqVeELvIwD86rs-0,59
3
+ py_serverless_kit/_version.py,sha256=EvXHEeUtfGFZss7YPsDtzk7-F9M97MDsrXH30gfMSao,59
4
4
  py_serverless_kit/client.py,sha256=mcphnzVZ3jGoFBgv9cU3A5GEav2YBF6MrqRZiD9hQPE,13747
5
5
  py_serverless_kit/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
6
  py_serverless_kit/cli/main.py,sha256=rvmj20DvkQ7eDSYD4_XwuT5BkBo_NPR0FRWTFM6GVKs,141857
@@ -35,7 +35,7 @@ py_serverless_kit/core/onprem_services.py,sha256=QPJtX7llkEyca_6iUSuFb89g81yn8nW
35
35
  py_serverless_kit/core/onprem_smtp.js,sha256=yU6w1PQJ8MVMiHoDQ4zGWYNqqI_1-oQWz0z4HVsSNPM,10046
36
36
  py_serverless_kit/core/onprem_smtp.py,sha256=SfkKpqz-d-71GfmUumtR4ePf4y3oDYATXXhdGAtRx9Y,22274
37
37
  py_serverless_kit/core/onprem_terminal.py,sha256=VbCHqj9neoVPydptx_6nCVIB9NcUUJ_iZOkFheoYiAA,7556
38
- py_serverless_kit/core/project.py,sha256=TGKNPT7kxdlWTJitf0p4IeL_HkbM25uPS_nOL2b8MGs,10184
38
+ py_serverless_kit/core/project.py,sha256=kTi_0Vzu6MLpjt4-E6FyoXGi_4xlcX7iGocDn3I0qr4,10405
39
39
  py_serverless_kit/core/resource_code.py,sha256=BqIFCl3p9q20u_o08Nqv8E5FGVHTCjRtjCXMH4hYq4s,22062
40
40
  py_serverless_kit/core/resource_usage.py,sha256=Qc_Ig69w0tEXkfIA6TxLsIA8tNzeCAoiMrYMRBPrP88,32498
41
41
  py_serverless_kit/core/retain.py,sha256=Tjhtsgfod2IPCglPLNhWZLCH6jY76NmMbFeEi-eKYYI,8175
@@ -83,13 +83,13 @@ py_serverless_kit/runtime/cron.py,sha256=ElNteRgy5WSNYYQpnJimU2b39APm---HuKR5U1r
83
83
  py_serverless_kit/runtime/dispatch.py,sha256=CxD287YO-bSVRwnb_QIVMcidlI3_Xyy5KwQv8cQzzTQ,11196
84
84
  py_serverless_kit/runtime/errors.py,sha256=oUQlCogy2_CKwsT7EfuogW2X6s4cJNR-LMQ3W95h4zU,2034
85
85
  py_serverless_kit/runtime/event.py,sha256=aT6pM0mLZXVeQBVRo2Hf48RSIhOagTAkL7PeZ1WH6_I,13034
86
- py_serverless_kit/runtime/executions.py,sha256=SFvf1EiiIIpTu-m1SuUWvofvxEbXaelxcedcvsXDBCY,17131
86
+ py_serverless_kit/runtime/executions.py,sha256=oVyn04ZnMwlZYxLXcDtja361kXtWQf7HkJvaQD_gbuw,18314
87
87
  py_serverless_kit/runtime/hostlog.py,sha256=oVaGvhXQA3-7MSahkd4QWwyghczYae6AB9569bvTb1E,15979
88
88
  py_serverless_kit/runtime/invoke.py,sha256=bDAkAUuXA4DPi_vltwVm-l2xIyahai0Xw853xLWhFeQ,7457
89
89
  py_serverless_kit/runtime/log.py,sha256=t1pv2gFquyMoDCPVTJdrXgmv8iI6r0QbWCKNSNSkWoI,2874
90
90
  py_serverless_kit/runtime/manifest.py,sha256=ZEYqytO1eay2kOFfdl1lgLGDqLnMteMJm6ZdzO_aG04,4126
91
91
  py_serverless_kit/runtime/node_worker.py,sha256=zoDbNRJ3hdgSNXYXJv4azfdgyWISrYtRkaoItv0bgmM,11209
92
- py_serverless_kit/runtime/onprem_host.py,sha256=bvjVivZFeqNMT9k6yfhd5avjuUxbz3cs2VTtn8CVWEg,133882
92
+ py_serverless_kit/runtime/onprem_host.py,sha256=WLDs5cNvIpxXTKxFmdjF9p0jMf2hJ05nqbiKHWkyj9Q,138417
93
93
  py_serverless_kit/runtime/peers.py,sha256=R2c_rMo6ssF3OJobJYwWdvsXJV2cdigyHZhNFlCMhQM,12164
94
94
  py_serverless_kit/runtime/resources.py,sha256=bEtpeAbjRWDxlA1hF4IPKNwTGW8yVwpPCM-6O62bxsI,3853
95
95
  py_serverless_kit/runtime/response.py,sha256=uybQq5Bdfxndx3iI0yd6uaVcC9Xjg9Lx50k8wrTX61U,6582
@@ -135,7 +135,7 @@ py_serverless_kit/runtime_js/invoke.js,sha256=DHA8vtnzWvP_G49GWT6TCljcUUg0aSOmAE
135
135
  py_serverless_kit/runtime_js/log.js,sha256=VLPNXkumduA1zaG4gsKLBORVlI-wlOtIdSB00p-K0n4,5114
136
136
  py_serverless_kit/runtime_js/manifest.js,sha256=4P_NS3yFc9gn-TTEd2wwhKRqDwXDW4YDaw8ETHIUzFA,4578
137
137
  py_serverless_kit/runtime_js/onprem.js,sha256=PWuQz6xU_boUzrJ75fi00kwkr4IeaIhBHnchDqndmiE,2798
138
- py_serverless_kit/runtime_js/package.json,sha256=2bHHkobkR3UGty0VIcAKdO6ISnKZ3pnk8wtF9mvObvw,284
138
+ py_serverless_kit/runtime_js/package.json,sha256=Nq9kiAUqV0qzU74UXvMcYhV6w8bI9Tq4j72cI_BpUz8,284
139
139
  py_serverless_kit/runtime_js/resources.js,sha256=vqRjf4LhXYTiVMsOBA6SXnBhOvMwhrS2q38i5LqKeP8,3297
140
140
  py_serverless_kit/runtime_js/response.js,sha256=RagfQCspN3jdtyvOKt2F52Rwq8FBT4k_0OKWQYH6Wu4,5800
141
141
  py_serverless_kit/runtime_js/routing.js,sha256=IM8zv0SIYU-d2lXZiup3KPL4Wfqtcec9PEdpSOwKSp4,2084
@@ -275,8 +275,8 @@ py_serverless_kit/studio/guides/tencent/database.md,sha256=i-ru3zul8msV8Ad2lZyOn
275
275
  py_serverless_kit/studio/guides/tencent/queue.md,sha256=Mn3P_E2i-ubCZL17V2vtAJXKsX6yZsI0Rv3seP6T-Mo,3556
276
276
  py_serverless_kit/studio/guides/tencent/storage.md,sha256=_JarrP9lnwP2TGc7UlWgQ17BbR7-X4A7ERP-W3oeowQ,4074
277
277
  py_serverless_kit/studio/guides/tencent/topic.md,sha256=ZSQ4JhOFLa_MgSoQBT0b0MryFXiJn9YWoeUidluhlYo,3354
278
- py_serverless_kit-2.3.3.dist-info/METADATA,sha256=bH6eXIdBPEXKNxr5BQmTLouhiWCkPzLYKVhYtJLxkhg,21451
279
- py_serverless_kit-2.3.3.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
280
- py_serverless_kit-2.3.3.dist-info/entry_points.txt,sha256=I-fW8cddXp3EkwMyijn1KuLGZ0MungQBPpjJ5Ey7xuY,110
281
- py_serverless_kit-2.3.3.dist-info/licenses/LICENSE,sha256=GhfSg8Bmq_7wlksflc7XI72zY2Z03FPpPa0GUUHz-8s,1099
282
- py_serverless_kit-2.3.3.dist-info/RECORD,,
278
+ py_serverless_kit-2.3.4.dist-info/METADATA,sha256=TFe5EAh96f8dH-CIIfhifrD6uV71pJ76xkyLTfgEuxQ,21451
279
+ py_serverless_kit-2.3.4.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
280
+ py_serverless_kit-2.3.4.dist-info/entry_points.txt,sha256=I-fW8cddXp3EkwMyijn1KuLGZ0MungQBPpjJ5Ey7xuY,110
281
+ py_serverless_kit-2.3.4.dist-info/licenses/LICENSE,sha256=GhfSg8Bmq_7wlksflc7XI72zY2Z03FPpPa0GUUHz-8s,1099
282
+ py_serverless_kit-2.3.4.dist-info/RECORD,,