Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Weird issue with plugin at HostGator shared hosting #18

Open
gpeipman opened this issue Jun 14, 2018 · 10 comments
Open

Weird issue with plugin at HostGator shared hosting #18

gpeipman opened this issue Jun 14, 2018 · 10 comments

Comments

@gpeipman
Copy link

Hi,

I used this plugin at my blog hosted at HostGator but had to disable it because of few issues.

Suddenly my blog gave frequently error 500 and few times I saw error messages about curl (page rendered still well). I contacted support about server errors and they told me that my site has more processes than allowed for shared hosting. There were not many people visiting blog at this moment and I have had no issues like this even at some high peak moments. Issues disappeared after disabling this plugin.

As I got not much information by hosting support and they were not very interested in helping me. Therefore I have no feedback by their techies about what was going on at server. As far as I understood from mere mortal feedback given by first line support then for some reason my site spawned new processes and at one point it reached the limit.

Is it possible that some libraries used with this plugin start new processes and is there any way to avoid it?

@SergeyKanzhelev
Copy link
Contributor

I wonder if it's because of Wordpress's wp_remote_post here. Nothing else should cause process creation. Is there any information from them was it because of this call? I'll also dig around

@gpeipman
Copy link
Author

Support was support and seemed to me that guy was far from bits and bytes. So, there's no information that can help us. I asked him for technical guys but well… Still, maybe I have something for you.

I went through PHP configuration and I saw it is running in CGI mode. It should mean that every request gets its own PHP process. I think this is why they have page where suggested number of requests per hosting package are listed.

As my blog uses caching then requests to posts and other pages are usually fast - 0.2-0.5s and perhaps this is why it is able to survive more load than I have ever expected. Although cache is used it is served by WordPress plugin and it means that ApplicationInsights works also for cached requests (I suppose).

There's one option to make the number of requests to pile and this can happen with server-side requests to other sites when network connection is slow or when server on the other end doesn't respond fast enough. I mean, if request to external machine takes usually let's say one second then it's suddenly 8 seconds and requests start piling as they are waiting for response.

If this is the case then I can think about few ways how to solve the problem:

  1. Add request timeout as configuration option so users with busy sites can set it to some small value. If timeout comes then your plugin just fails clean. This way PHP processes cannot pile up so easily and go through the limits of smaller hosting packages.

  2. Send information to Application Insights service using JavaScript. Just prepare request data and let browser do the job. I'm not very much sure about this option as server-side page loading time is not so accurate anymore. But then there's no network dependencies on your plugin level.

  3. Mix of two above. If request to Application Insights times out then let's automatically switch to JavaScript option.

Not sure if point 2 is valid and has any meaning but it looks like a good idea to break one dependency to external resource.

@SergeyKanzhelev
Copy link
Contributor

@gpeipman I think this may be the case I don't think sending to Application Insights will take seconds, but sometimes it may be up to a second (with slower networks).

One solution may be to send request and do not wait for response. But wp_remote_post doesn't seem to allow for this. Alternative is to curl in separate process, but it wouldn't address your problem.

Another issue suggest to implement some batching and back off logic: microsoft/ApplicationInsights-PHP#50

So perhaps one good solution will be to start background process that uploads data and all requests just dump information to specific file location.

Does HostGator allow some writeable file system location and start separate processes?

@gpeipman
Copy link
Author

Yes, of course, otherwise it would be close to impossible to use WordPress normally.

My simple shared hosting package supports also cron jobs, so I think it has everything needed.

If it is okay for Application Insights then it's not a problem when cron job runs once or twice per hour.

@SergeyKanzhelev
Copy link
Contributor

@gpeipman I was thinking more like once a minute. Otherwise the delay for data upload will be too high. So maybe php script with the infinite loop and sleep. Not sure if it's a best practice in PHP.

Would you be interested to contribute? These SDKs (PHP and wordpress) are community supported, not officially maintained. But I'll be happy to review and help where I can.

@gpeipman
Copy link
Author

gpeipman commented Jun 15, 2018

Hosting guys usually doesn't like those endless scripts and some of them may have also guarding systems that kill processes that have been up for too long. To get rid of this there are two options:

  • cron job with one minute interval
  • upload script called from some external process using wget, curl etc

If cron job happens frequently it's possible that hosting takes it as a sign of something suspicious. In this means it would be important even there to have small request timeouts.

To contribute I have to understand first how the code works. I know PHP language and some internals of WordPress but I haven't written anything serious on PHP more than ten years. But let's try.

@SergeyKanzhelev
Copy link
Contributor

Contributing instructions are straightforward: https://github.com/Microsoft/ApplicationInsights-WordPress/blob/master/CONTRIBUTING.md You can use docker or just host a local version of wordpress.

This line constructs the client:
https://github.com/Microsoft/ApplicationInsights-WordPress/blob/7b782fd771d7df70874aaad6cba0d50f3d8409a2/src/Server_Instrumentation.php#L23

Constructor of TelemetryClient accepts channel as an argument: https://github.com/Microsoft/ApplicationInsights-PHP/blob/fd82b5e1c3b2002b63ac85fad69b3aae2c4c3620/ApplicationInsights/Telemetry_Client.php#L26-L30 So you can create a custom copy of channel in this repository that will implement the same methods as this channel: https://github.com/Microsoft/ApplicationInsights-PHP/blob/fd82b5e1c3b2002b63ac85fad69b3aae2c4c3620/ApplicationInsights/Channel/Telemetry_Channel.php#L221-L230

And method send will do some advanced logic. If long running scripts is bad - maybe store some last running time in server variables or in file system. And run script only when difference in time is more than couple minutes. So send will save data to file system, check the time difference and if much time passed - run a script to read data from file storage and send them to Application Insights.

@SergeyKanzhelev
Copy link
Contributor

How this note can help you get started. Let me know if you need more help. And don't hesitate to point on unclear contributing docs or code structure. We want to make contributions easy and straightforward

@rkmaier
Copy link

rkmaier commented Oct 2, 2018

@SergeyKanzhelev I am php dev and would like take care of this issue.

@gpeipman
Copy link
Author

gpeipman commented Sep 1, 2019

I got something done and it even works. Request parameters needed by trackRequest() method are saved to file and there's cron job that goes through all saved files, sends data to Application Insights and deletes files. As I'm not WordPress expert I don't have much idea how to fit it to this plugin the way that others can also use it.

Anyway, if somebody needs urgent solution then feel free to ping me while I try to figure out how to make this functionality as integral part of plugin.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants