Jump to content

Risks of using module init()?


Recommended Posts

@szabesz actually the bot do not contain all the data I want, there is something like 5k rows of data, and to achieve it I need to scrap almost all the forum. Even if the process is not resource consumming there might be something else, It doesn't hurt to ask first.

  • Like 1
Link to comment
Share on other sites

On 5/31/2024 at 4:03 PM, flydev said:

Just played with a chatbot that will be soon publicly available

I use Github copilot but didn't think to use it on that occasion...

2076592673_Screenshot2024-06-01235728.png.c43434849890ab96cb537572e288b5e8.png

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...
On 5/20/2024 at 10:54 PM, MarkE said:

BTW I had done something similar with 'ready' - I'm not sure that caused the problems but I moved it all back into ready.php just in case

A bit of an update. Firstly, the problem was caused by ready(), not init(), so my comment above was incorrect.

What I did was

  1. Moved all the code from init.php into a similar file in my module folder, then added include('init.php'); in the module's init() method. That seems to work OK.
  2. Did a similar thing with ready.php (including it from the module's ready() method). That worked OK in the dev environment, but in the live environment gave the following error on viewing the page tree: You don't have access to list page /

So I put some hooks in ready.php to track the listable status:

wire()->addHookBefore('ProcessPageList::execute', function(HookEvent $event) {
	$process = $event->object;
	bd($process, 'process');
	bd(debug_backtrace(), 'backtrace in processPageList execute');
	bd($process->page->listable, 'listable');
	if(!$process->page->listable) bd(debug_backtrace(), 'backtrace not listable');
});

and 

wire()->addHookAfter('Page::listable', function(HookEvent $event) {
	$page = $event->object;
	$currentUser = $page->wire()->user;
	bd([$page->path, $currentUser->id], 'PagePath, UserId In listable hook');
	bd(Debug::backtrace(), 'backtrace in listable hook');
....

I then ran the code in two ways: (a) including the module ready.php from the site ready.php file (otherwise empty) and (b) including it in the module's ready() method. In the dev environment, both these work. In the live environment, (a) yields the following:

925906762_Screenshot2024-06-13232115.thumb.png.7b2f9615a06d1599e393beca0259c637.png

whereas, with (b), I get :

622958067_Screenshot2024-06-13233518.thumb.png.bbf984f394f5a22424dbfbdb0daebb57.png

The only difference is that in (a), listable is true, whereas in (b) it is false. To repeat, this is running exactly the same ready.php code on exactly the same database - the only difference is in how it is called. The calls are made from ProcessWire.php (lines 664 to 676):

		// call any relevant internal methods
		if($status == self::statusInit) {
			$this->init();
		} else if($status == self::statusReady) {
			$config->admin = $this->isAdmin();
			$this->ready();
			if($this->debug) Debug::saveTimer('boot', 'includes all boot timers');
		} 
	
		// after status include file, names like 'init', 'ready', etc.
		$file = empty($files[$name]) ? null : $path . basename($files[$name]);
		if($file !== null) $this->includeFile($file, $data);

The only significant difference seems to be that the module ready is called earlier, but there is no processing between the calls.

GitHub copilot is completely stumped (as am I), so I am hoping that some expert humans might be able to shine a little light 🙂

 

Link to comment
Share on other sites

Quote

The only significant difference seems to be that the module ready is called earlier


You didn’t told us what’s the result if you set / change your hook priority.

edit: and are you 100% x 3 sure that roles and permission are the same on both envs ?

edit2: (again 🙃) profiling your code will give you more skills than copilot, and loose less time on this whole thing.

edit3: you should read this issue to see  if it help about what are trying to achieve: https://github.com/ryancramerdesign/ProcessWire/issues/302


edit4: what the bot say:

The issue could be related to the priority of hooks and the sequence in which they are executed. In ProcessWire, hooks can be attached in different methods such as init()and ready(). The init() method is called before ProcessWire handles a web request, while the ready()method is called after ProcessWire determines what page is going to be viewed but before the page is rendered. If the module ready is called earlier, it might affect the sequence in which hooks are executed, potentially leading to the observed issue.

To solve this issue, you can try the following steps:

  • Ensure that the hooks are attached in the correct method (init() or ready()) based on when they need to be executed.
  • Adjust the priority of the hooks to control the order in which they are executed.

Additionally, you can use the standardized include/hook files provided by ProcessWire:

  • /site/init.php: This file is included during ProcessWire's boot initialization, immediately after autoload modules have been loaded and had their init() methods called. This is an excellent place to attach hooks that don't need to know anything about the current page.
  • /site/ready.php: This file is included immediately after the API is fully ready. It behaves the same as a ready() method in an autoload module. The current $page has been determined, but not yet rendered. This is an excellent place to attach hooks that may need to know something about the current page.

By ensuring that your hooks are placed in the appropriate files and adjusting their priorities, you should be able to resolve the issue with the listable status in the live environment.

  • Like 1
Link to comment
Share on other sites

Thanks @flydev and I will follow up on the priority matter. I have been using profiling as well as AI, and it has been more helpful. As you can see from this other post

 I have boiled the issue down to one of purely where the hooks are called from - ready.php or the module ready(). So I am 99.99% certain it is nothing to do with roles and permissions.

Link to comment
Share on other sites

11 hours ago, flydev said:

you should read this issue to see  if it help about what are trying to achieve: https://github.com/ryancramerdesign/ProcessWire/issues/302

 

Thanks for that. Aside from the problem with calling the hook from the module ready(), the listable hook I am using works perfectly, with additional code I added to display the correct number of children.

Link to comment
Share on other sites

11 hours ago, flydev said:

Ensure that the hooks are attached in the correct method (init() or ready()) based on when they need to be executed.

The hooks are attached in ready(). They don't work at all in init() - it is too early.

11 hours ago, flydev said:

Adjust the priority of the hooks to control the order in which they are executed.

But there is no duplicate calling of the hooks, so how is priority relevant?

Link to comment
Share on other sites

I just suggested priority in case there is another hook somewhere of even to not let the system decide when to call it as we have not a profiling report, just set a priority highter, it doesn't hurt to make a test. Also, the reason of why I posted the issue is because I am assuming from your screenshots that you are not making tests with root user id (#7815), so you should test with root user on both envs in order to tackle in first instance the permission issue you posted in your other thread, and post the results.

  • Thanks 1
Link to comment
Share on other sites

1 minute ago, flydev said:

I just suggested priority in case there is another hook somewhere of even to not let the system decide when to call it, just set a priority highter

Only just saw this @flydev. Meanwhile I solved the problem as shown here: 

Your suggestion is absolutely spot on! But at least I worked out exactly why it is necessary. 

Many many thanks for your persistence in helping me to solve this problem. I have learned a whole lot more about ProcessWire along the way!

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...