import time


def get_runtime_s(job):
    """Returns job runtime in milliseconds."""

    scrapystats = job.metadata.get("scrapystats")
    finished_time = job.metadata.get("finished_time")
    start_time = scrapystats.get("start_time")
    if finished_time:
        return finished_time - start_time

    return int(round(time.time() * 1000)) - start_time