Queuejob hook not working

I have a Python Hook to change the assigned/default que for a user testuser1 to another Que testq if the user is found in a text file. But it is not working for me. the hook is not called at all as per the server logs. Can anyone help me to identify the problem?

$cat set_queue_based_on_user.PY

#!/usr/bin/env python3

import sys
import os
sys.path.append(f’/opt/pbs/lib/python/‘)
sys.path.append(f’/opt/pbs/lib/python/altair’)
import pbs

EVENT = pbs.event()

def set_queue_based_on_user():
try:
# Get the job object
job = EVENT.job

    # Read the reference file
    queue_map = {}
    ref_file = "/myhpc/pbs/server_priv/users/testq_user_list.txt"
    with open(ref_file, "r") as f:
        for line in f:
            if line.strip() and not line.startswith("#"):
                user, queue = line.split()
                queue_map[user.strip()] = queue.strip()

    # Get the user submitting the job
    job_user = job.euser  # Effective user submitting the job

    # Assign the queue based on the user
    if job_user in queue_map:
        job.queue = queue_map[job_user]
    elif "default" in queue_map:
        job.queue = queue_map["default"]
    else:
        e.reject("User not found in the queue reference file.")

except Exception as ex:
    pbs.logmsg(pbs.LOG_DEBUG, f"Hook error: {str(ex)}")
    e.reject(f"Hook error: {str(ex)}")

if EVENT.type == ‘queuejob’:
set_queue_based_on_user()
EVENT.accept()

#cat /etc/pbs/server_priv/users/testq_user_list.txt
testuser1 testq
default workq

#qmgr -c “list hook set_queue_based_on_user”
Hook set_queue_based_on_user
type = site
enabled = true
event = queuejob
user = pbsadmin
alarm = 30
order = 1
debug = false
fail_action = none

Issues:

Please try this code

import sys
import os
import pbs

e=pbs.event()
j=e.job
job_user = str(e.requestor)  # Effective user submitting the job
pbs.logmsg(pbs.LOG_DEBUG, "User is -----------> %s " % str(job_user))

if e.type == pbs.QUEUEJOB:
    try:
        #Read the reference file
        queue_map = {}
        ref_file = "/root/testq_user_list.txt"
        with open(ref_file, "r") as f:
            for line in f:
                if line.strip() and not line.startswith("#"):
                    user,queue = line.split()
                    queue_map[user.strip()] = queue.strip()
        # Assign the queue based on the user
        if job_user in queue_map:
            j.queue = pbs.server().queue(str(queue_map[job_user]))
        elif "default" in queue_map:
            j.queue = pbs.server().queue(str(queue_map["default"]))
        else:
            e.reject("User not found in the queue reference file.")
    except Exception as ex:
        pbs.logmsg(pbs.LOG_DEBUG, f"Hook error: {ex}")
        e.reject( f"Hook error: {ex}")

e.accept()

Thank You very much adarsh. It worked like a charm !!

1 Like