def stop_write_job_now(job_handler: JobHandler) -> None:
    if job_handler.get_state() == JobState.WRITING:
        job_handler.set_stop_time(datetime.now())
        while not job_handler.is_done():
            time.sleep(1)
        print("FileWriter successfully stopped.")
    sys.exit()
def stop_write_job(args: argparse.Namespace, job_handler: JobHandler) -> None:
    stop_time = float(args.stop_after[1])
    timeout = int(current_time()) + args.timeout
    stop_time = datetime.now() + timedelta(seconds=stop_time)
    stop_handler = job_handler.set_stop_time(stop_time)
    while not stop_handler.is_done() and not job_handler.is_done():
        if int(current_time()) > timeout:
            raise ValueError("Timeout.")
def verify_write_job(job_handler: JobHandler) -> None:
    if job_handler.get_state() == JobState.WRITING:
        print("The write process is confirmed. Stopping...")
    else:
        raise ValueError("There are no write jobs associated with the "
                         "given job id. Please check broker, topic and "
                         "id information and try again.")
Beispiel #4
0
def prepare_write_job(args: argparse.Namespace) -> WriteJob:
    global JOB_HANDLER
    global ACK_TIMEOUT

    file_name = args.filename
    job_id = args.job_id
    host = args.broker
    command_topic = args.command_status_topic
    pool_topic = args.job_pool_topic
    config = args.config
    ACK_TIMEOUT = args.timeout
    command_channel = WorkerJobPool(f"{host}/{pool_topic}",
                                    f"{host}/{command_topic}")
    JOB_HANDLER = JobHandler(worker_finder=command_channel)
    with open(config, "r") as f:
        nexus_structure = f.read()
    if job_id:
        write_job = WriteJob(nexus_structure,
                             file_name,
                             host,
                             datetime.now(),
                             job_id=job_id)
    else:
        write_job = WriteJob(
            nexus_structure,
            file_name,
            host,
            datetime.now(),
        )
    return write_job
def create_job_handler(args: argparse.Namespace, job_id: str) -> JobHandler:
    host = args.broker
    command_topic = args.command_status_topic
    pool_topic = args.job_pool_topic
    command_channel = WorkerJobPool(f"{host}/{pool_topic}",
                                    f"{host}/{command_topic}")
    job_handler = JobHandler(worker_finder=command_channel, job_id=job_id)
    # Required for formation of the handler.
    time.sleep(3)
    return job_handler
Beispiel #6
0
import time
from datetime import datetime, timedelta

from file_writer_control import CommandState
from file_writer_control import JobHandler
from file_writer_control import JobState
from file_writer_control import WorkerJobPool
from file_writer_control import WriteJob

if __name__ == "__main__":
    kafka_host = "dmsc-kafka01:9092"
    worker_job_pool = WorkerJobPool("{}/job_topic".format(kafka_host),
                                    "{}/command_topic".format(kafka_host))
    job_handler = JobHandler(worker_finder=worker_job_pool)
    start_time = datetime.now()
    with open("file_writer_config.json", "r") as f:
        nexus_structure = f.read()
    write_job = WriteJob(
        nexus_structure,
        "{0:%Y}-{0:%m}-{0:%d}_{0:%H}{0:%M}.nxs".format(start_time),
        kafka_host,
        start_time,
    )

    print("Starting write job")
    start_handler = job_handler.start_job(write_job)
    while not start_handler.is_done():
        print("Waiting: {}".format(start_handler.get_state()))
        if start_handler.get_state() == CommandState.ERROR:
            print("Got error when starting job. The error was: {}".format(
                start_handler.get_message()))
Beispiel #7
0
    # Job pool is where idle file-writers listen for start messages.
    # The start message can come from any instrument and only one file-writer will
    # respond to it.
    job_pool_topic = "job_pool_topic"

    # Control topic is a "private" topic dedicated to a specific instrument.
    # This is where the selected file-writer writes status messages and
    # listens for stop messages from that instrument.
    control_topic = "ymir_control_topic"

    with open("file_writer_config.json", "r") as f:
        nexus_structure = f.read()

    command_channel = WorkerJobPool(f"{kafka_host}/{job_pool_topic}",
                                    f"{kafka_host}/{control_topic}")
    job_handler = JobHandler(worker_finder=command_channel)
    start_time = datetime.now()

    write_job = WriteJob(
        nexus_structure,
        "{0:%Y}-{0:%m}-{0:%d}_{0:%H}{0:%M}.nxs".format(start_time),
        kafka_host,
        start_time,
        control_topic=control_topic,
    )

    print("Starting write job")
    start_handler = job_handler.start_job(write_job)
    while not start_handler.is_done():
        time.sleep(1)
    print("Job started")