Ejemplo n.º 1
0
        protected override async Task <Action <NativeActivityContext> > ExecuteAsync(NativeActivityContext context, CancellationToken cancellationToken)
        {
            string path = Path.Get(context);

            if (!path.IsNullOrEmpty() && !Directory.Exists(path))
            {
                throw new DirectoryNotFoundException(string.Format(Resources.InvalidPathException, path));
            }

            cancellationToken.ThrowIfCancellationRequested();

            _pythonEngine = EngineProvider.Get(Version, path, !Isolated, TargetPlatform, ShowConsole);

            var workingFolder = WorkingFolder.Get(context);

            if (!workingFolder.IsNullOrEmpty())
            {
                var dir = new DirectoryInfo(workingFolder);
                if (!dir.Exists)
                {
                    throw new DirectoryNotFoundException(Resources.WorkingFolderPathInvalid);
                }
                workingFolder = dir.FullName; //we need to pass an absolute path to the python host
            }

            try
            {
                await _pythonEngine.Initialize(workingFolder, cancellationToken);
            }
            catch (Exception e)
            {
                Trace.TraceError($"Error initializing Python engine: {e.ToString()}");
                Cleanup();
                throw new InvalidOperationException(Resources.PythonInitializeException, e);
            }

            cancellationToken.ThrowIfCancellationRequested();
            return(ctx =>
            {
                ctx.ScheduleAction(Body, _pythonEngine, OnCompleted, OnFaulted);
            });
        }
Ejemplo n.º 2
0
        protected override async Task <Action <NativeActivityContext> > ExecuteAsync(NativeActivityContext context, CancellationToken cancellationToken)
        {
            string path        = Path.Get(context);
            string libraryPath = LibraryPath.Get(context);

            if (!path.IsNullOrEmpty() && !Directory.Exists(path))
            {
                throw new DirectoryNotFoundException(string.Format(Resources.InvalidPathException, path));
            }

            cancellationToken.ThrowIfCancellationRequested();

            _pythonEngine = EngineProvider.Get(Version, path, libraryPath, !Isolated, TargetPlatform, ShowConsole);

            var workingFolder = WorkingFolder.Get(context);

            if (!workingFolder.IsNullOrEmpty())
            {
                var dir = new DirectoryInfo(workingFolder);
                if (!dir.Exists)
                {
                    throw new DirectoryNotFoundException(Resources.WorkingFolderPathInvalid);
                }
                workingFolder = dir.FullName; //we need to pass an absolute path to the python host
            }

            var operationTimeout = OperationTimeout.Get(context);

            if (operationTimeout == 0)
            {
                operationTimeout = 3600; //default to 1h for no values provided.
            }

            try
            {
                await _pythonEngine.Initialize(workingFolder, cancellationToken, operationTimeout);
            }
            catch (Exception e)
            {
                Trace.TraceError($"Error initializing Python engine: {e.ToString()}");
                try
                {
                    Cleanup();
                }
                catch (Exception) { }
                if (Version != Version.Auto)
                {
                    Version autodetected = Version.Auto;
                    EngineProvider.Autodetect(path, out autodetected);
                    if (autodetected != Version.Auto && autodetected != Version)
                    {
                        throw new InvalidOperationException(string.Format(Resources.InvalidVersionException, Version.ToFriendlyString(), autodetected.ToFriendlyString()));
                    }
                }
                throw new InvalidOperationException(Resources.PythonInitializeException, e);
            }

            cancellationToken.ThrowIfCancellationRequested();

            return(ctx =>
            {
                ctx.ScheduleAction(Body, _pythonEngine, OnCompleted, OnFaulted);
            });
        }