def audio_encoder(sources): # Parse configuration parser = create_arg_parser() read_request, read_response = sources.argv.argv.pipe( ops.skip(1), argparse.parse(parser), ops.filter(lambda i: i.key == 'config'), ops.map(lambda i: file.Read(id='config', path=i.value)), file.read(sources.file.response), ) config = read_response.pipe( ops.filter(lambda i: i.id == "config"), ops.flat_map(lambda i: i.data), parse_config, ) # Transcode request handling encode_init = config.pipe( ops.map( lambda i: encoder.Initialize(storage_path=i.encode.storage_path))) encode_request = sources.httpd.route.pipe( ops.filter(lambda i: i.id == 'flac_transcode'), ops.flat_map(lambda i: i.request), ops.do_action(lambda i: print("[{}]http req: {}".format( datetime.datetime.now(), threading.get_ident()))), #.observe_on(encode_scheduler) ops.flat_map(lambda i: Observable.just(i, encode_scheduler)), ops.do_action(lambda i: print("[{}]encode req: {}".format( datetime.datetime.now(), threading.get_ident()))), ops.map(lambda i: encoder.EncodeMp3( id=i.context, data=i.data, key=i.match_info['key'])), ) encoder_request = rx.merge(encode_init, encode_request) # store encoded file store_requests = sources.encoder.response.pipe( ops.do_action(lambda i: print("[{}]encode res: {}".format( datetime.datetime.now(), threading.get_ident()))), ops.observe_on(s3_scheduler), ops.do_action(lambda i: print("[{}]s3 req: {}".format( datetime.datetime.now(), threading.get_ident()))), ops.map(lambda i: s3.UploadObject( key=i.key + '.flac', data=i.data, id=i.id, )), ) # acknowledge http request http_response = sources.s3.response.pipe( ops.do_action(lambda i: print("[{}]s3 res: {}".format( datetime.datetime.now(), threading.get_ident()))), ops.do_action( lambda i: print("httpd res: {}".format(threading.get_ident()))), ops.map(lambda i: httpd.Response( data='ok'.encode('utf-8'), context=i.id, )), ) # http server http_init = config.pipe( ops.flat_map(lambda i: rx.from_([ httpd.Initialize(request_max_size=0), httpd.AddRoute( methods=['POST'], path='/api/transcode/v1/flac/{key:[a-zA-Z0-9-\._]*}', id='flac_transcode', ), httpd.StartServer(host=i.server.http.host, port=i.server.http.port ), ]))) http = rx.merge(http_init, http_response) # s3 database s3_init = config.pipe( ops.map(lambda i: s3.Configure( access_key=i.s3.access_key, secret_key=i.s3.secret_key, bucket=i.s3.bucket, endpoint_url=i.s3.endpoint_url, region_name=i.s3.region_name, ))) # merge sink requests file_requests = read_request s3_requests = rx.merge(s3_init, store_requests) return Sink( encoder=encoder.Sink(request=encoder_request), s3=s3.Sink(request=s3_requests), file=file.Sink(request=file_requests), httpd=httpd.Sink(control=http), )
def audio_encoder(sources): # Parse configuration parser = create_arg_parser() parsed_argv = sources.argv.argv.pipe( ops.skip(1), argparse.parse(parser), ops.filter(lambda i: i.key == 'config'), ops.subscribe_on(aio_scheduler), ops.share(), ) # monitor and parse config file monitor_init = parsed_argv.pipe( ops.flat_map(lambda i: rx.from_([ inotify.AddWatch( id='config', path=i.value, flags=aionotify.Flags.MODIFY), inotify.Start(), ]))) config_update = sources.inotify.response.pipe( ops.debounce(5.0, scheduler=aio_scheduler), ops.map(lambda i: True), ops.start_with(True), ) read_request, read_response = rx.combine_latest( parsed_argv, config_update).pipe( ops.starmap( lambda config, _: file.Read(id='config', path=config.value)), file.read(sources.file.response), ) config = read_response.pipe( ops.filter(lambda i: i.id == "config"), ops.flat_map(lambda i: i.data), parse_config, ) # Transcode request handling encode_init = config.pipe( ops.map(lambda i: i.encode), ops.distinct_until_changed(), ops.map(lambda i: encoder.Configure(samplerate=i.samplerate, bitdepth=i.bitdepth)), ) encode_request = sources.httpd.route.pipe( ops.filter(lambda i: i.id == 'flac_transcode'), ops.flat_map(lambda i: i.request), ops.flat_map(lambda i: rx.just(i, encode_scheduler)), ops.map(lambda i: encoder.EncodeMp3( id=i.context, data=i.data, key=i.match_info['key'])), ) encoder_request = rx.merge(encode_init, encode_request) # store encoded file store_requests = sources.encoder.response.pipe( ops.observe_on(s3_scheduler), ops.map(lambda i: s3.UploadObject( key=i.key + '.flac', data=i.data, id=i.id, )), ) # acknowledge http request http_response = sources.s3.response.pipe( ops.map(lambda i: httpd.Response( data='ok'.encode('utf-8'), context=i.id, ))) # http server http_init = config.pipe( ops.take(1), ops.flat_map(lambda i: rx.from_([ httpd.Initialize(request_max_size=0), httpd.AddRoute( methods=['POST'], path='/api/transcode/v1/flac/{key:[a-zA-Z0-9-\._]*}', id='flac_transcode', ), httpd.StartServer(host=i.server.http.host, port=i.server.http.port ), ])), ) http = rx.merge(http_init, http_response) # s3 database s3_init = config.pipe( ops.take(1), ops.map(lambda i: s3.Configure( access_key=i.s3.access_key, secret_key=i.s3.secret_key, bucket=i.s3.bucket, endpoint_url=i.s3.endpoint_url, region_name=i.s3.region_name, )), ) # merge sink requests file_requests = read_request s3_requests = rx.merge(s3_init, store_requests) return Sink( encoder=encoder.Sink(request=encoder_request), s3=s3.Sink(request=s3_requests), file=file.Sink(request=file_requests), httpd=httpd.Sink(control=http), inotify=inotify.Sink(request=monitor_init), )
def audio_encoder(sources): http_s3_error, route_s3_error = make_error_router() http_encode_error, route_encode_error = make_error_router() # Parse configuration parsed_argv = (sources.argv.argv.skip(1).let( argparse.argparse, parser=Observable.just( argparse.Parser(description="audio encode server")), arguments=Observable.from_([ argparse.ArgumentDef(name='--config', help="Path of the server configuration file") ])).filter(lambda i: i.key == 'config').subscribe_on( aio_scheduler).share()) # monitor and parse config file monitor_init = (parsed_argv.flat_map(lambda i: Observable.from_([ inotify.AddWatch( id='config', path=i.value, flags=aionotify.Flags.MODIFY), inotify.Start(), ]))) config_update = (sources.inotify.response.debounce(5000).map( lambda i: True).start_with(True)) read_config_file = (Observable.combine_latest( parsed_argv, config_update, lambda config, _: file.Read(id='config', path=config.value))) config = sources.file.response.let(parse_config) # Transcode request handling encode_init = (config.map(lambda i: i.encode).distinct_until_changed().map( lambda i: encoder.Configure(samplerate=i.samplerate, bitdepth=i.bitdepth))) encode_request = ( sources.httpd.route.filter(lambda i: i.id == 'flac_transcode'). flat_map(lambda i: i.request).flat_map(lambda i: Observable.just( i, encode_scheduler)).map(lambda i: encoder.EncodeMp3( id=i.context, data=i.data, key=i.match_info['key']))) encoder_request = Observable.merge(encode_init, encode_request) # store encoded file store_requests = (sources.encoder.response.let( catch_or_flat_map, error_router=route_encode_error, error_map=lambda i: httpd.Response(data='encode error'.encode('utf-8'), context=i.args[0].id, status=500)). observe_on(s3_scheduler).map(lambda i: s3.UploadObject( key=i.key + '.flac', data=i.data, id=i.id, ))) # acknowledge http request http_response = (sources.s3.response.let( catch_or_flat_map, error_router=route_s3_error, error_map=lambda i: httpd.Response(data='upload error'.encode('utf-8'), context=i.args[0].id, status=500)).map( lambda i: httpd.Response( data='ok'.encode('utf-8'), context=i.id, ))) # http server http_init = (config.take(1).flat_map(lambda i: Observable.from_([ httpd.Initialize(request_max_size=0), httpd.AddRoute( methods=['POST'], path='/api/transcode/v1/flac/{key:[a-zA-Z0-9-\._]*}', id='flac_transcode', ), httpd.StartServer(host=i.server.http.host, port=i.server.http.port), ]))) http = Observable.merge(http_init, http_response, http_s3_error, http_encode_error) # s3 database s3_init = (config.take(1).map(lambda i: s3.Configure( access_key=i.s3.access_key, secret_key=i.s3.secret_key, bucket=i.s3.bucket, endpoint_url=i.s3.endpoint_url, region_name=i.s3.region_name, ))) # merge sink requests file_requests = read_config_file s3_requests = Observable.merge(s3_init, store_requests) return Sink( encoder=encoder.Sink(request=encoder_request), s3=s3.Sink(request=s3_requests), file=file.Sink(request=file_requests), httpd=httpd.Sink(control=http), inotify=inotify.Sink(request=monitor_init), )
def audio_encoder(sources): # Parse configuration read_config_file = (sources.argv.argv.skip(1).let( argparse.argparse, parser=Observable.just( argparse.Parser(description="audio encode server")), arguments=Observable.from_([ argparse.ArgumentDef(name='--config', help="Path of the server configuration file") ])).filter(lambda i: i.key == 'config').map( lambda i: file.Read(id='config', path=i.value))) config = sources.file.response.let(parse_config) # Transcode request handling encode_init = (config.map( lambda i: encoder.Initialize(storage_path=i.encode.storage_path))) encode_request = ( sources.httpd.route.filter(lambda i: i.id == 'flac_transcode'). flat_map(lambda i: i.request).map(lambda i: encoder.EncodeMp3( id=i.context, data=i.data, key=i.match_info['key']))) encoder_request = Observable.merge(encode_init, encode_request) # store encoded file store_requests = (sources.encoder.response.map(lambda i: s3.UploadObject( key=i.key + '.flac', data=i.data, id=i.id, ))) # acknowledge http request http_response = (sources.s3.response.map(lambda i: httpd.Response( data='ok'.encode('utf-8'), context=i.id, ))) # http server http_init = (config.flat_map(lambda i: Observable.from_([ httpd.Initialize(request_max_size=0), httpd.AddRoute( methods=['POST'], path='/api/transcode/v1/flac/{key:[a-zA-Z0-9-\._]*}', id='flac_transcode', ), httpd.StartServer(host=i.server.http.host, port=i.server.http.port), ]))) http = Observable.merge(http_init, http_response) # s3 database s3_init = (config.map(lambda i: s3.Configure( access_key=i.s3.access_key, secret_key=i.s3.secret_key, bucket=i.s3.bucket, endpoint_url=i.s3.endpoint_url, region_name=i.s3.region_name, ))) # merge sink requests file_requests = read_config_file s3_requests = Observable.merge(s3_init, store_requests) return Sink( encoder=encoder.Sink(request=encoder_request), s3=s3.Sink(request=s3_requests), file=file.Sink(request=file_requests), httpd=httpd.Sink(control=http), )