Skip to content

manage remote job

Functions

cancel_job(jobID=None)

Cancel a remote job. Syntax: fab cancel_job:jobID

note : if the jobID is empty, this function cancel all submitted job which are not in FINISHED status

Source code in fabsim/base/manage_remote_job.py
@task
@beartype
def cancel_job(jobID: Optional[str] = None) -> None:
    """
    Cancel a remote job.
    Syntax: fab <machine> cancel_job:jobID

    note : if the jobID is empty, this function cancel all submitted
           job which are not in FINISHED status
    """

    check_jobs_dispatched_on_remote_machine()

    if jobID is None:
        print(
            "ERROR: No jobID is passed,\n\tusage Syntax :"
            "\n\t\tfab <machine> cancel_job:jobID= <input_jobID>"
        )
        sys.exit()

    env.jobID = jobID

    if (
        hasattr(env, "dispatch_jobs_on_localhost")
        and isinstance(env.dispatch_jobs_on_localhost, bool)
        and env.dispatch_jobs_on_localhost
    ):
        local(template(template.template("$cancel_job_command")))
    elif env.manual_sshpass:
        sshpass_args = "-e" if env.env_sshpass else "-f '%(sshpass)s'" % env
        sshpass_cmd = f"sshpass {sshpass_args}"
        pre_cmd = sshpass_cmd + " ssh %(username)s@%(remote)s " % env
        manual_command = template("$cancel_job_command $jobID")
        local(pre_cmd + "'" + manual_command + "'", capture=False)

    else:
        run(template(template("$cancel_job_command")))

check_complete(jobname_syntax='')

Return true if the user has no job running containing jobname_syntax in their name

Source code in fabsim/base/manage_remote_job.py
@beartype
def check_complete(jobname_syntax: Optional[str] = "") -> bool:
    """
    Return true if the user has no job running containing
    jobname_syntax in their name
    """
    time.sleep(10)

    check_jobs_dispatched_on_remote_machine()
    jobs_dict = jobs_list(quiet=True)

    count = jobs_dict.count('\n') + 1

    if len(jobs_dict) > 0:
        print(
            f"The number of active (not finished) jobs = {count}"
            )
        return False

    print("All jobs are finished :)")
    return True

jobs_list(quiet=False)

Source code in fabsim/base/manage_remote_job.py
@beartype
def jobs_list(quiet: Optional[bool] = False) -> str:
    """
    options:
            quiet = True : hide the command output
    """
    CRED = "\33[31m"
    CEND = "\33[0m"
    if (
        hasattr(env, "dispatch_jobs_on_localhost")
        and isinstance(env.dispatch_jobs_on_localhost, bool)
        and env.dispatch_jobs_on_localhost
    ):
        # output = local(template("$stat "), capture=quiet).splitlines()
        output = run(template("$stat "), capture=quiet)
        return output

    elif env.manual_sshpass:
        sshpass_args = "-e" if env.env_sshpass else "-f '%(sshpass)s'" % env
        sshpass_cmd = f"sshpass {sshpass_args}"
        pre_cmd = sshpass_cmd + " ssh %(username)s@%(remote)s " % env
        manual_command = template("$stat")
        # manual_command = '"' + manual_command + '"'
        print("manual_command", manual_command)
        output = local(pre_cmd + "'" + manual_command + "'", capture=False)
        string = (
            CRED + "The stat of your submitted job is shown"
            " in the table above!" + CEND
        )
        return string
        # print('output', output)
        # output = local(
        #     template(
        #         pre_cmd +
        #         manual_command
        #     )
        # )
        # output = local(pre_cmd + str(manual_command) , capture=False)

    else:
        output = run(template("$stat"), capture=quiet)

        # One some machines (e.g. ARCHER2) output returns a tuple.
        # This branch isolates the string part of it.
        if isinstance(output, tuple):
            output = output[0]

        return output

    # return output

stat()

Check the remote message queue status for individual machines. Syntax: fab stat.

TODO: Respect varying remote machine queue systems.

Source code in fabsim/base/manage_remote_job.py
@task
def stat() -> None:
    """
    Check the remote message queue status for individual machines.
    Syntax: fab <machine> stat.

    TODO: Respect varying remote machine queue systems.
    """
    check_jobs_dispatched_on_remote_machine()
    jobsInfo = jobs_list(quiet=True)
    # jobs_list(quiet=True)
    print(jobsInfo)

wait_complete(jobname_syntax='')

Wait until jobs currently running containing jobname_syntax in their name are complete, then return

Source code in fabsim/base/manage_remote_job.py
@task
def wait_complete(jobname_syntax: str = "") -> None:
    """
    Wait until jobs currently running containing jobname_syntax in
    their name are complete, then return
    """
    # time.sleep(120)
    i = 0
    wait_times = [120, 180, 300, 600]
    while not check_complete(jobname_syntax):
        if i < 15:
            i += 1
        time.sleep(wait_times[int(i / 5)])
        print("Waiting {} seconds.".format(wait_times[int(i / 5)]))