dbrand666 / usmobile-lifeguard

Minds your USMobile pool so you don't have to
GNU General Public License v3.0
15 stars 2 forks source link

feat: support exit after single check #5

Closed matthewadams closed 1 year ago

matthewadams commented 1 year ago

This change allows the script to be scheduled via cron job or cloud scheduler.

dbrand666 commented 1 year ago

The thing about having an external scheduler trigger this script is that it's fundamentally a scraper application. If the auth token gets expired or invalidated or if the (undocumented) APIs change, the best that will happen is that the script will keep failing. Worst case, it keeps thinking the topup failed and tries to buy more data.

If we want the script to disable itself we need a tiny bit of persistence.

Maybe we should just return a fail exit code and leave it to the scheduler to deal with?

matthewadams commented 1 year ago

Ok, I added exiting with an error code on any exception. The problem is when max_errors is nonzero due to the program being written to support multiple pools. I say only support one pool at a time, or throw if any pool update operation fails. We can support max_errors tries on every rest call pretty easily with the following:

def with_tries(n: int, op):
    i = 0
    while True:
        try:
            return op()
        except:
            i += 1
            if i >= n:
                raise

I think max_errors would be reinterpreted as "max_tries - 1". WDYT?

I also refactored config to be more typesafe up front.

matthewadams commented 1 year ago

WRT running once, I'd say remove the retry logic, unless you want to use my with_tries suggestion but only on rest calls. Also, I'd remove the loop over all pools and just support a single pool per invocation. Handling multiple pools could come later as required, IMHO.