Skip to content

Commit

Permalink
check_timeout
Browse files Browse the repository at this point in the history
class GSAHandler
  • Loading branch information
drernie committed Dec 12, 2023
1 parent 68fab47 commit eb37466
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Constants {
MANIFEST_SUFFIX: '.json',
QUILT_METADATA: 'quilt_metadata.json',
FASTQ_SENTINEL: 'out/bqsr_report/NA12878.hg38.recal_data.csv',
TIMEOUT: 15*60,
};

public static GET(key: string): any {
Expand Down
5 changes: 3 additions & 2 deletions src/omics-quilt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class OmicsQuiltStack extends Stack {
return new NodejsFunction(this, name, {
runtime: Runtime.NODEJS_18_X,
role: this.lambdaRole,
timeout: Duration.seconds(60),
timeout: Duration.seconds(this.cc.get('TIMEOUT')),
retryAttempts: 1,
environment: this.makeLambdaEnv(env),
});
Expand All @@ -216,7 +216,7 @@ export class OmicsQuiltStack extends Stack {
index: PYTHON_INDEX,
runtime: Runtime.PYTHON_3_11,
role: this.lambdaRole,
timeout: Duration.seconds(900),
timeout: Duration.seconds(this.cc.get('TIMEOUT')),
retryAttempts: 1,
environment: this.makeLambdaEnv(env),
bundling: {
Expand All @@ -237,6 +237,7 @@ export class OmicsQuiltStack extends Stack {
SENTINEL_FILE: this.packager_sentinel,
QUILT_METADATA: this.cc.get('QUILT_METADATA'),
WORKFLOW_ID: this.cc.get('READY2RUN_WORKFLOW_ID'),
TIMEOUT: this.cc.get('TIMEOUT'),
...env,
};
return final_env;
Expand Down
2 changes: 1 addition & 1 deletion src/packager/packager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .types import KEYED, PseudoContext # noqa: F401
from .constants import Constants # noqa: F401
from .index import handler # noqa: F401
from .handler import Handler # noqa: F401
from .gsa_handler import GSAHandler # noqa: F401
from .ssm_parameter_store import SSMParameterStore # noqa: F401
18 changes: 16 additions & 2 deletions src/packager/packager/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import yaml
import os

from datetime import datetime
from dotenv import load_dotenv
from tempfile import TemporaryDirectory
from typing import Any, Generator
Expand Down Expand Up @@ -115,6 +116,7 @@ def __init__(self, context: KEYED = {}) -> None:
self.app = self.get("APP_NAME")
self.account = self.get("CDK_DEFAULT_ACCOUNT", "AWS_ACCOUNT_ID")
self.region = self.get("CDK_DEFAULT_REGION", "AWS_DEFAULT_REGION")
self.ssm = SSMParameterStore(self.app, self.region)

def to_dict(self) -> KEYED:
return {
Expand Down Expand Up @@ -159,5 +161,17 @@ def get_bucket_name(self, type: str) -> str:
def get_ecr_registry(self) -> str:
return f"{self.account}.dkr.ecr.{self.region}.amazonaws.com"

def get_parameter_name(self, name: str) -> str:
return f"/vivos/{self.app}/{name}"
def timeout(self) -> int:
return int(self.get("TIMEOUT"))

def check_time(self, key: str) -> bool:
now = round(datetime.now().timestamp())
if key in self.ssm:
prior = int(self.ssm[key])
delta = now - prior
timeout = self.timeout()
if delta < timeout:
print(f"Too soon: {delta} < {timeout}")
return False
self.ssm[key] = str(now)
return True
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
LOG_STREAM = "OmicsQuiltDemo-{:0>4d}{:0>2d}{:0>2d}"


class Handler:
class GSAHandler:
@staticmethod
def ReportRoot(report_uri: str) -> Path:
report_path = Constants.ToPath(report_uri)
Expand Down Expand Up @@ -48,34 +48,43 @@ def __init__(self, context: Any):

def handleEvent(self, event: KEYED) -> KEYED:
opts = self.parseEvent(event)
body = {
"message": "N/A",
"event": event,
"opts": opts,
}
print(f"handleEvent.opts: {opts}")
ready = self.cc.check_time(opts["key"])
if not ready:
body["message"] = "Not ready"
return {
"statusCode": 200,
"body": body,
}
if not opts.get("type"):
body["message"] = "ERROR: No type"
return {
"statusCode": 400, # Bad Request
"body": "No type",
"body": body,
}
if opts["type"] != "ObjectCreated:Put":
body["message"] = f"ERROR: Bad type: {opts['type']}"
return {
"statusCode": 404, # Not Found
"body": "No action",
"body": body,
}

report_uri = f"s3://{opts['bucket']}/{opts['key']}"
root = self.ReportRoot(report_uri)
print(f"handleEvent.root: {root}")
meta = opts
if not opts.get("debug"):
tables = self.downloadReport(report_uri, root)
self.summarizeTables(tables, root)
meta = self.packageFolder(root, opts)
body["opts"] = self.packageFolder(root, opts)
body["message"] = f"{report_uri} @ {root}"
return {
"statusCode": 200,
"body": {
"root": str(root),
"report": report_uri,
"meta": meta,
"event": event,
},
"statusCode": 201,
"body": body,
}

def parseEvent(self, event: KEYED) -> KEYED:
Expand Down
4 changes: 2 additions & 2 deletions src/packager/packager/index.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Any

from .handler import Handler
from .gsa_handler import GSAHandler
from .types import KEYED


def handler(event: KEYED, context: Any) -> KEYED:
handler = Handler(context)
handler = GSAHandler(context)
return handler.handleEvent(event)
1 change: 1 addition & 0 deletions src/packager/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
"EVENT": "../../test/events/event-params.json",
"S3_URI": "s3://data-yaml-spec-tests/quilt_metadata.json",
"S3_BUCKET": "data-yaml-spec-tests",
"TIMEOUT": "1",
}
9 changes: 9 additions & 0 deletions src/packager/tests/test_constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from packager import Constants
from .conftest import CTX
import pytest
import time


@pytest.fixture
Expand All @@ -20,3 +21,11 @@ def test_download_object(cc):
assert filename
assert filename.exists()
assert filename.is_file()


def test_check_time(cc):
key = "timer"
assert cc.check_time(key)
assert not cc.check_time(key)
time.sleep(1.5 * cc.timeout())
assert cc.check_time(key)
4 changes: 2 additions & 2 deletions src/packager/tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from datetime import datetime

from packager import Handler
from packager import GSAHandler
from tempfile import TemporaryDirectory

# from unittest.mock import MagicMock, patch
Expand All @@ -13,7 +13,7 @@

@pytest.fixture
def handler():
return Handler({})
return GSAHandler({})


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion src/packager/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_valid_type(ctx, event):
result = handler(event, ctx)
assert result
print(record)
assert result["statusCode"] == 200
assert result["statusCode"] == 201
body = result["body"]
assert body
print(body)
7 changes: 7 additions & 0 deletions src/packager/tests/test_ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ def store():
def test_parameter(store):
store["key"] = "value"
assert "value" == store["key"]


def test_slashes(store):
store["key"] = "value"
assert "value" == store["key"]
store["key/2"] = "value2"
assert "value2" == store["key/2"]

0 comments on commit eb37466

Please sign in to comment.