Ejemplo n.º 1
0
def api_send(**kwargs):
    objects = kwargs.get("objects") or kwargs.get(
        "ids", [])  # TODO: ids is deprecated. use objects instead
    id_action = kwargs.get("id_action", False)
    settings = kwargs.get("settings", {})
    db = kwargs.get("db", DB())
    user = kwargs.get("user", anonymous)
    restart_existing = kwargs.get("restart_existing", True)
    restart_running = kwargs.get("restart_running", False)

    if not user.has_right("job_control", anyval=True):
        return NebulaResponse(403,
                              "You are not allowed to execute this action")
        # TODO: Better ACL

    if not id_action:
        return NebulaResponse(400, "No valid action selected")

    if not objects:
        return NebulaResponse(400, "No asset selected")

    if not user.has_right("job_control", id_action):
        return NebulaResponse(400, "You are not allowed to start this action")

    logging.info(
        "{} is starting action {} for the following assets: {}".format(
            user, id_action, ", ".join([str(i) for i in objects])))

    for id_object in objects:
        send_to(
            id_object,
            id_action,
            settings=settings,
            id_user=user.id,
            restart_existing=restart_existing,
            restart_running=restart_running,
            db=db,
        )

    return NebulaResponse(
        200, "Starting {} job{}".format(len(objects),
                                        "s" if len(objects) > 1 else ""))
Ejemplo n.º 2
0
    def _proc(self, id_asset, db):
        asset = Asset(id_asset, db = db)
        for id_action in self.conditions:
            if "broker/started/{}".format(id_action) in asset.meta:
                continue
            cond_title, cond, priority = self.conditions[id_action]
            if eval(cond):
                logging.info("{} matches action condition {}".format(asset, cond_title))
                res, msg = send_to(asset.id, id_action, settings={}, id_user=0, priority=priority, restart_existing=False, db=db)

                if success(res):
                    logging.info(msg)
                else:
                    logging.error(msg)

                asset["broker/started/{}".format(id_action)] = 1
                asset.save()
Ejemplo n.º 3
0
def hive_send_to(user, params):
    id_action = params.get("id_action", False)
    settings  = params.get("settings", {})
    restart_existing = params.get("restart_existing", True)

    if not id_action:
        yield 400, "Bad request"
        return

    if not user.has_right("job_control", id_action):
        yield 403, "Not authorised"
        return

    logging.info("{} is starting action {} for following assets: {}".format(user, id_action, params.get("objects", [])))

    db = DB()
    for id_object in params.get("objects", []):
        yield -1, send_to(id_object, id_action, settings={}, id_user=user.id, restart_existing=restart_existing, db=db)[1]
    yield 200, "OK"
    return
Ejemplo n.º 4
0
    def proc(self, asset):
        for action in self.actions:
            if action.created_key in asset.meta:
                continue

            if action.should_create(asset):
                logging.info(
                    f"{asset} matches action condition {action.title}")
                result = send_to(asset.id,
                                 action.id,
                                 restart_existing=False,
                                 restart_running=False,
                                 db=asset.db)

                if result:
                    logging.info(result.message)
                else:
                    logging.error(result.message)

                asset[action.created_key] = 1
                asset.save(set_mtime=False)
Ejemplo n.º 5
0
    def main(self):
        db = self.db
        storage = storages[self.playout_config["playout_storage"]]
        if not storage:
            if STORAGE_STATUS.get(storage.id, True):
                logging.error(f"{storage} is not available")
                STORAGE_STATUS[storage.id] = False
            return
        STORAGE_STATUS[storage.id] = True
        storage_path = storage.local_path

        for asset, scheduled in get_scheduled_assets(self.id_channel, db=db):
            old_status = asset.get(self.status_key, DEFAULT_STATUS)

            # read playout file props
            try:
                fs = os.stat(asset.get_playout_full_path(self.id_channel))
                file_exists = stat.S_ISREG(fs[stat.ST_MODE])
            except FileNotFoundError:
                file_exists = False

            if file_exists:
                file_size = fs[stat.ST_SIZE]
                file_mtime = fs[stat.ST_MTIME]
            else:
                file_size = file_mtime = 0

            if file_exists:
                if file_size:
                    file_status = ONLINE
                else:
                    file_status = CORRUPTED
            else:
                file_status = OFFLINE

            ostatus = old_status.get("status", OFFLINE)
            omtime = old_status.get("mtime", 0)
            osize = old_status.get("size", 0)
            duration = old_status.get("duration", 0)

            now = time.time()

            # if file changed, check using ffprobe
            if file_status == ONLINE:
                if omtime != file_mtime or osize != file_size:
                    file_status, duration = check_file_validity(
                        asset, self.id_channel)

                else:
                    if ostatus == CREATING:
                        if now - file_mtime > 10 and omtime == file_mtime:
                            file_status = ONLINE
                        else:
                            file_status = CREATING
                    elif ostatus == UNKNOWN:
                        if now - file_mtime > 10:
                            file_status = CORRUPTED

            if ostatus != file_status or omtime != file_mtime or osize != file_size:
                logging.info(
                    f"Set {asset} playout status to {get_object_state_name(file_status)}"
                )
                asset[self.status_key] = {
                    "status": file_status,
                    "size": file_size,
                    "mtime": file_mtime,
                    "duration": duration
                }
                asset.save()

            if file_status not in [
                    ONLINE, CREATING, CORRUPTED
            ] and self.send_action and asset["status"] == ONLINE and scheduled:
                result = send_to(asset.id,
                                 self.send_action,
                                 restart_existing=True,
                                 restart_running=False,
                                 db=db)
                if result.response == 201:
                    logging.info(
                        f"Sending {asset} to playout {self.playout_config['title']} : {result.message}"
                    )