Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage reporting tweaks #536

Merged
merged 6 commits into from
Jul 8, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions examples/simple-service-poc/simple_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ class SimpleService(Service):
SIMPLE_SERVICE = "/golem/run/simple_service.py"
SIMPLE_SERVICE_CTL = "/golem/run/simulate_observations_ctl.py"

def __init__(self, *args, instance_name, **kwargs):
def __init__(self, *args, instance_name, show_usage: bool = False, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd either specify types for both instance_name and show_usage, or for neither of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, here, done ;)

super().__init__(*args, **kwargs)
self.name = instance_name
self._show_usage = show_usage

@staticmethod
async def get_payload():
Expand Down Expand Up @@ -81,18 +82,20 @@ async def run(self):
steps = self._ctx.commit()
yield steps

print(f" --- {self._ctx.provider_name} USAGE: {await self._ctx.get_usage()}")
print(f" --- {self._ctx.provider_name} STATE: {await self._ctx.get_state()}")
print(f" --- {self._ctx.provider_name} COST: {await self._ctx.get_cost()}")
if self._show_usage:
print(f" --- {self.name} USAGE: {await self._ctx.get_usage()}")
print(f" --- {self.name} STATE: {await self._ctx.get_state()}")
print(f" --- {self.name} COST: {await self._ctx.get_cost()}")

async def shutdown(self):
# handler reponsible for executing operations on shutdown
self._ctx.run(self.SIMPLE_SERVICE_CTL, "--stop")
yield self._ctx.commit()
print(f" --- {self._ctx.provider_name} COST: {await self._ctx.get_cost()}")
if self._show_usage:
print(f" --- {self.name} COST: {await self._ctx.get_cost()}")


async def main(subnet_tag, running_time, driver=None, network=None, num_instances=1):
async def main(subnet_tag, running_time, driver=None, network=None, num_instances=1, show_usage=False):
async with Golem(
budget=1.0,
subnet_tag=subnet_tag,
Expand Down Expand Up @@ -120,7 +123,7 @@ async def main(subnet_tag, running_time, driver=None, network=None, num_instance
cluster = await golem.run_service(
SimpleService,
instance_params=[
{"instance_name": f"simple-service-{i+1}"} for i in range(num_instances)
{"instance_name": f"simple-service-{i+1}", "show_usage": show_usage} for i in range(num_instances)
],
expiration=datetime.now(timezone.utc) + timedelta(minutes=120),
)
Expand Down Expand Up @@ -184,7 +187,8 @@ def still_starting():
"(in seconds, default: %(default)s)"
),
)
parser.add_argument("--num-instances", type=int, default=1)
parser.add_argument("--num-instances", type=int, default=1, help="The number of instances of the service to spawn")
parser.add_argument("--show-usage", action="store_true", help="Show usage and cost of each instance while running.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ;)

now = datetime.now().strftime("%Y-%m-%d_%H.%M.%S")
parser.set_defaults(log_file=f"simple-service-yapapi-{now}.log")
args = parser.parse_args()
Expand All @@ -207,6 +211,7 @@ def still_starting():
driver=args.driver,
network=args.network,
num_instances=args.num_instances,
show_usage=args.show_usage,
)
)

Expand Down