import { Command } from 'commander'; import { RuntimeClient } from '../runtime/_client'; import { remote } from '../proto/runtime/v1alpha1/remote'; import { JetroutineFunction, JetroutineFunctionReturnTypes, SupportedTypes, } from '../config/_symbols'; import { loadEntrypoint, loadRegisteredFunc } from './_util'; import { JetpackClient } from '../core/client'; import { execJetroutine } from '../core/exec'; const program = new Command(); program .command('register') .description('Registers jetpack functions with runtime') .option( '--entrypoint ', 'entrypoint of app', process.env.JETPACK_ENTRYPOINT, ) .action((options: { entrypoint: string }) => { loadEntrypoint(options.entrypoint) .then(() => new RuntimeClient().registerApp()) .then((response: remote.RegisterAppResponse) => { // console logging the response. This will be available in the pod logs. console.log(response); }) .catch((err) => { const errMessage = `unable to load entrypoint, receive error: ${err}`; console.log(errMessage); // Exit with error code. process.exit(1); }) .finally(() => { // When we load the user endpoint, the endpoint can have a // server running listening on some port. And loading the code // will mean that the server is started upon import. // In order to prevent the sdk register job hanging, we need to // force exit the program. process.exit(); }); }); program .command('exec-cronjob') .description('Executes a cronjob') .option( '--entrypoint ', 'entrypoint of app', process.env.JETPACK_ENTRYPOINT, ) .option('--qualified-symbol ', 'identifier of the cronjob') .action((options: { entrypoint: string, qualifiedSymbol: string }) => { loadRegisteredFunc(options.entrypoint, options.qualifiedSymbol) .then((func: JetroutineFunction) => func()) .catch((err) => { const errMessage = `unable to execute function, receive error: ${err}`; console.log(errMessage); process.exit(1); }) .finally(() => { process.exit(); }); }); program .command('exec-task') .description('Executes a task') .option( '--entrypoint ', 'entrypoint of app', process.env.JETPACK_ENTRYPOINT, ) .option('--exec-id ', 'task id of the jetroutine') .option('--qualified-symbol ', 'identifier of the jetroutine') .action((options: { entrypoint: string, execId: string, qualifiedSymbol: string }) => { const jetpackClient = new JetpackClient(); const runtimeClient = jetpackClient._getRuntimeClient(); const loadEncodedArgs = async () => { const task = await runtimeClient.getTask(options.execId); return JSON.parse(String.fromCharCode(...task.encoded_args)) as SupportedTypes[]; }; Promise.all( [loadRegisteredFunc(options.entrypoint, options.qualifiedSymbol), loadEncodedArgs()], ).then(([func, encodedArgs]) => execJetroutine( func, encodedArgs, ( res?: JetroutineFunctionReturnTypes, err?: remote.Error, ) => runtimeClient.postResult(options.execId, res, err), )) .then(({ error }) => { // Rethrow it for catch block to execute if (error) throw error; }) .catch((err) => { console.log(err); // Exit with error code. process.exit(1); }) .finally(() => { process.exit(); }); }); export default program;