bedrock.workers.worker

 1import json
 2import os
 3import subprocess
 4
 5from bedrock.log import log_config
 6
 7
 8class Worker:  # pragma: unit
 9    """
10    Base class for python workers.
11    Workers are other processes that run in their individual lambdas and are triggered by endpoint or other lambdas.
12    They can get invoked using the `invoke` method of the `invoker` module (see `bedrock.workers.invoker`).
13
14    A python worker benefits from the whole bedrock ecosystem, including configuration, logging, ORM, etc.
15
16    This class also has a javascript version. See `bedrock/workers/worker.js`.
17    """
18    __worker_type__ = "python"
19
20    def __init__(self):
21        self.logger = log_config(self.__class__.__name__)
22
23    def handle(self, event, context):
24        try:
25            return self.execute(event, context)
26        except Exception as e:
27            self.logger.error(f"Error executing python worker {self.__class__.__name__}: {e}")
28            return False
29
30    def execute(self, event, context):
31        """
32        Overridable method. Your logic should be here.
33
34        This method is called by the `handle` method.
35        """
36        raise NotImplementedError()
37
38
39class JavascriptWorker(Worker):
40    """
41    This is a class that wraps javascript workers for local execution.
42    """
43    __worker_type__ = "javascript"
44
45    def __init__(self):
46        super().__init__()
47        self.file_name = None
48        self.class_name = None
49
50    def set_properties(self, file_name, class_name):  # pragma: unit
51        self.file_name = file_name
52        self.class_name = class_name
53
54    def _get_file_path(self):  # pragma: unit
55        return f"./workers/{self.file_name}/main.runner.tmp.js"
56
57    def execute(self, event, context):  # pragma: integration
58        tmp_file_executor = self._get_file_path()
59        with open(tmp_file_executor, "w") as f:
60            f.write(f"""
61const {{lambda_handler}} = require('./main');
62lambda_handler({json.dumps(event)}, {json.dumps(context)})
63    .then(result => console.log(JSON.stringify(result)));""")
64        p = subprocess.Popen(['node', tmp_file_executor], stdout=subprocess.PIPE)
65        string_result = p.stdout.read().decode("utf-8")
66        json_result = json.loads(string_result.strip().split("\n")[-1])
67        os.unlink(tmp_file_executor)
68        return json_result
class Worker:
 9class Worker:  # pragma: unit
10    """
11    Base class for python workers.
12    Workers are other processes that run in their individual lambdas and are triggered by endpoint or other lambdas.
13    They can get invoked using the `invoke` method of the `invoker` module (see `bedrock.workers.invoker`).
14
15    A python worker benefits from the whole bedrock ecosystem, including configuration, logging, ORM, etc.
16
17    This class also has a javascript version. See `bedrock/workers/worker.js`.
18    """
19    __worker_type__ = "python"
20
21    def __init__(self):
22        self.logger = log_config(self.__class__.__name__)
23
24    def handle(self, event, context):
25        try:
26            return self.execute(event, context)
27        except Exception as e:
28            self.logger.error(f"Error executing python worker {self.__class__.__name__}: {e}")
29            return False
30
31    def execute(self, event, context):
32        """
33        Overridable method. Your logic should be here.
34
35        This method is called by the `handle` method.
36        """
37        raise NotImplementedError()

Base class for python workers. Workers are other processes that run in their individual lambdas and are triggered by endpoint or other lambdas. They can get invoked using the invoke method of the invoker module (see bedrock.workers.invoker).

A python worker benefits from the whole bedrock ecosystem, including configuration, logging, ORM, etc.

This class also has a javascript version. See bedrock/workers/worker.js.

logger
def handle(self, event, context):
24    def handle(self, event, context):
25        try:
26            return self.execute(event, context)
27        except Exception as e:
28            self.logger.error(f"Error executing python worker {self.__class__.__name__}: {e}")
29            return False
def execute(self, event, context):
31    def execute(self, event, context):
32        """
33        Overridable method. Your logic should be here.
34
35        This method is called by the `handle` method.
36        """
37        raise NotImplementedError()

Overridable method. Your logic should be here.

This method is called by the handle method.

class JavascriptWorker(Worker):
40class JavascriptWorker(Worker):
41    """
42    This is a class that wraps javascript workers for local execution.
43    """
44    __worker_type__ = "javascript"
45
46    def __init__(self):
47        super().__init__()
48        self.file_name = None
49        self.class_name = None
50
51    def set_properties(self, file_name, class_name):  # pragma: unit
52        self.file_name = file_name
53        self.class_name = class_name
54
55    def _get_file_path(self):  # pragma: unit
56        return f"./workers/{self.file_name}/main.runner.tmp.js"
57
58    def execute(self, event, context):  # pragma: integration
59        tmp_file_executor = self._get_file_path()
60        with open(tmp_file_executor, "w") as f:
61            f.write(f"""
62const {{lambda_handler}} = require('./main');
63lambda_handler({json.dumps(event)}, {json.dumps(context)})
64    .then(result => console.log(JSON.stringify(result)));""")
65        p = subprocess.Popen(['node', tmp_file_executor], stdout=subprocess.PIPE)
66        string_result = p.stdout.read().decode("utf-8")
67        json_result = json.loads(string_result.strip().split("\n")[-1])
68        os.unlink(tmp_file_executor)
69        return json_result

This is a class that wraps javascript workers for local execution.

file_name
class_name
def set_properties(self, file_name, class_name):
51    def set_properties(self, file_name, class_name):  # pragma: unit
52        self.file_name = file_name
53        self.class_name = class_name
def execute(self, event, context):
58    def execute(self, event, context):  # pragma: integration
59        tmp_file_executor = self._get_file_path()
60        with open(tmp_file_executor, "w") as f:
61            f.write(f"""
62const {{lambda_handler}} = require('./main');
63lambda_handler({json.dumps(event)}, {json.dumps(context)})
64    .then(result => console.log(JSON.stringify(result)));""")
65        p = subprocess.Popen(['node', tmp_file_executor], stdout=subprocess.PIPE)
66        string_result = p.stdout.read().decode("utf-8")
67        json_result = json.loads(string_result.strip().split("\n")[-1])
68        os.unlink(tmp_file_executor)
69        return json_result

Overridable method. Your logic should be here.

This method is called by the handle method.

Inherited Members
Worker
logger
handle