jrxFive / python-nomad

Client library Hashicorp Nomad
https://python-nomad.readthedocs.io/en/latest/
MIT License
139 stars 73 forks source link

Namespace in base object. #168

Open Penton7 opened 2 months ago

Penton7 commented 2 months ago

Hello, I will try to formulate my question and suggestion. I encountered a problem, and the approach implemented in this module seems incorrect to me. I am not a full-time developer, and my experience in programming in Python is not exceptional, but still. Why does the implementation of obtaining information from different endpoints for different jobs look like this? More specifically, I am creating functionality where I need to manipulate jobs and check their status. I have a variable that sets up the connection to the cluster, and then different jobs are passed through functions. They are in different namespaces. From my point of view, there should be a possibility in a specific function call to get information about a job that is located in a particular namespace. For example, when calling get_deployments(), I should pass the name of the job itself and the namespace in which it is located, rather than specifying the namespace in the object where the cluster address and token are specified. Examples where I have issues are attached above.

Example create connect to nomad: my_nomad = nomad.Nomad(host=Config.NOMAD_ADDR, token=Config.NOMAD_TOKEN)

Example some func my func:

def stop_project(namespace):
    jobs = get_jobs(namespace) # func where i read namespaces from db
    logging.info(jobs)
    for job in jobs:
        status_before = my_nomad.job.get_job(id=job.job_name, namespace=job.namespace) 
        if status_before["Stop"]:
            logging.info(f"JOB {job.job_name} STOPPED.")
            continue
        else:
            logging.info("Update last definition!")
            get_def = my_nomad.job.get_job(namespace=job.namespace, id=job.job_name)
            to_db = update_job(job.job_name, definition=json.dumps(get_def))
            logging.info(f"Update. {to_db}")
            if to_db == "err":
                logging.error("failed to update!")
            logging.info(f"STOP JOB {job.job_name}")
            stop = my_nomad.job.deregister_job(f"{job.job_name}?namespace={job.namespace}") 
            status_after = my_nomad.job.get_job(id=job.job_name, namespace=job.namespace)
            logging.info(f"{stop} Job stop?:  {status_after['Stop']}")

why for get_job() we have a var namespace, but for deregister_job() we dont have a the same var. I use query in this, but its not good.

I can use nomad.Nomad() in the function and call this function each time, passing the namespace variable to it. However, it seems to me that the connection definition should be set once during initialization, and it should contain the data for all requests. If I am wrong and my question and suggestion seem foolish, I apologize. In that case, I would appreciate a detailed explanation.