Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add persistent launch #47

Closed

Conversation

teawinzero
Copy link
Contributor

  1. Added cleanup after closing the browser
  2. Added closing the browser when Ctrl + C signal is received
  3. Added option to specify directory of user data

src/browser.ts Outdated
Comment on lines 69 to 71
userDataDir: string | null;
persistent: boolean;
gracefulShutdown: boolean;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand what usecase this solves. At most, this PR should introduce the userDataDir (kind of wordy name btw) option.

If the user specifies where they want the data dir to be, they probably want it to be persisted. It'd be weird to care about the location and then not make it persisted.

I think gracefulShutdown should be true 100% of the time. I don't understand why it's a flag to be enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user specifies where they want the data dir to be, they probably want it to be persisted. It'd be weird to care about the location and then not make it persisted.

When I wrote this functionality I was thinking about the possibility of specifying any folder, including its ability to be temporary.

Of course, I could rewrite some of the code and remove the persistent option, and only save the directory when userDataDir is specified.

I think gracefulShutdown should be true 100% of the time. I don't understand why it's a flag to be enabled.

Maybe then add an env check if this built-in functionality should be disabled?

if (Deno.env.get("DISABLE_ASTRAL_GRACEFUL_SHUTDOWN") === undefined) { ... }

For example, if you need to implement custom behavior with signals other than the current one and the ability to close all browsers using the closeAllBrowsers method.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, I could rewrite some of the code and remove the persistent option, and only save the directory when userDataDir is specified.

I would be in favor of that.

Maybe then add an env check if this built-in functionality should be disabled?

I don't think it's necessary. If someone asks for it, we can consider it but I think this over-complicates the API surface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary. If someone asks for it, we can consider it but I think this over-complicates the API surface.

In my pet project I have implemented waiting for an empty task queue and disconnecting from the database at termination.

The problem is that signals are processed synchronously and do not wait for other listeners, so I need to disable listening inside this implementation and manually close browsers.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the advantage of graceful shutdown? Shouldn't users manually close the browser anyways?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you attempt to terminate a process early, graceful shutdown ensures an orderly and controlled termination of an application, allowing it to complete necessary tasks, release resources, and maintain system stability.

Copy link
Owner

@lino-levan lino-levan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry for not being super clear here, I don't really see the advantage of adding this much complexity to the API when this would quite literally be like 4 or 5 lines to write if the user really needed it. I think managing the browser instances should be done by the API consumer and not us.

I could be wrong about this, and maybe there's a usecase that you're thinking of that I'm not that requires this functionality built into Astral, but it really seems complicated for something unrelated to what we do.

I do really like the customizability of the userDataDir and would be happy to accept this PR with just those changes.

@teawinzero teawinzero changed the title feat: add persistent launch and graceful shutdown feat: add persistent launch and cleanup Nov 22, 2023
// Launch the browser
const browser = await launch({
// Setting the directory where the browser will store user-specific data, such as cookies, local storage, and other browsing data.
userDataDir: resolve("./my-browser-data"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need resolve here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do not do this, the directory will be created in the folder where the browser is launched.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably automatically resolve it (in launch). I feel like making it spawn where the browser is launched would definitely not be behavior people would expect.

src/browser.ts Outdated
@@ -66,6 +66,8 @@ async function runCommand(
export interface BrowserOptions {
headless: boolean;
product: "chrome" | "firefox";
userDataDir: string | null;
persistent: boolean;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

persistent seems to be removable with no ill affect.

src/browser.ts Outdated Show resolved Hide resolved
Co-authored-by: Lino Le Van <[email protected]>
@lino-levan lino-levan changed the title feat: add persistent launch and cleanup feat: add persistent launch Dec 9, 2023
@lino-levan
Copy link
Owner

@teawinzero Are you willing to push this forward?

@lino-levan
Copy link
Owner

Closing due to merge conflicts, sorry this couldn't get merged!

@lino-levan lino-levan closed this May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants