antoinemartin / django-windows-tools

Django application providing management commands to host Django projects in Windows environments
BSD 2-Clause "Simplified" License
51 stars 13 forks source link

Instantaneous "The service did not respond to the start to control request in a timely fashion" #19

Closed iarp closed 5 years ago

iarp commented 7 years ago

Windows 10 Python 3.6.1 django-windows-tools 0.2

virtualenv located at D:\virtualenvs-36\venv-django_forms

services.ini:

[services]
run = celeryd celerybeat
clean = C:\Projects\Django\django_forms\logs\beat.log;C:\Projects\Django\django_forms\logs\celery.log

[celeryd]
command = celery
parameters = -A django_forms worker -P eventlet -f C:\Projects\Django\django_forms\logs\celery.log -l debug

[celerybeat]
command = celery
parameters = -A django_forms beat -f C:\Projects\Django\django_forms\logs\beat.log -l debug

[log]
filename = C:\Projects\Django\django_forms\logs\service.log
level = DEBUG

python service.py install

Installing service django-django_forms-service
Service installed

python service.py start

Starting service django-django_forms-service
Error starting service: The service did not respond to the start or control request in a timely fashion.

Windows Event Log:

The Django django_forms background service service failed to start due to the following error: 
The service did not respond to the start or control request in a timely fashion.

No .log files are created, the error messages appears instantly.

I've also tried setting the user on the service to myself with the same result.

I can run these two commands from two virtualenv activated consoles without any issue and everything works:

celery -A django_forms worker -l info -P eventlet
celery -A django_forms beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
iarp commented 7 years ago

I opened the service and copied the command given and running that in console results in:

The code execution cannot proceed because pywintypes36.dll was not found. Reinstalling the program may fix this problem

Some more debugging, I added the path for pywintypes36.dll to my environment path variable: D:\virtualenvs-36\venv-django_forms\Lib\site-packages\pypiwin32_system32 and the error message changed but still no log files.

So this time I ran the services command from console manually and get the following message:

C:\Windows\system32>"D:\virtualenvs-36\venv-django_forms\lib\site-packages\win32\PythonService.exe" -debug django-django_forms-service
Debugging service django-django_forms-service - press Ctrl+C to stop.
Error 0xC0000004 - Python could not import the service's module

Traceback (most recent call last):
  File "C:\Projects\Django\django_forms\service.py", line 14, in <module>
    from django_windows_tools.service import DjangoService,test_commands
ModuleNotFoundError: No module named 'django_windows_tools'

(null): (null)

So now I'm starting to think that the service isn't picking up on the virtualenv need.

I tried activating the virtualenv and running the same command, exact same error message.

Now I don't really know where to go from here.

mrbean-bremen commented 7 years ago

Did you add the virtualenv path (e.g the Scripts path) to the system path? This is needed, at least if you run the service as the standard local service user.

iarp commented 7 years ago

I've added the Scripts folder and restarted, we're getting farther:

C:\Windows\system32>"D:\virtualenvs-36\venv-django_forms\lib\site-packages\win32\PythonService.exe" -debug django-django_forms-service
Debugging service django-django_forms-service - press Ctrl+C to stop.
Info 0x400000FF - Initialization
Info 0x400000FF - C:\Projects\Django\django_forms\service.ini
Info 0x400000FF - starting
Info 0x400000FF - Spawned C:\Python36\lib\site-packages\django_windows_tools\service.py celery -A django_forms worker -P eventlet -f C:\Projects\Django\django_forms\logs\celery.log -l debug
Info 0x400000FF - Spawned C:\Python36\lib\site-packages\django_windows_tools\service.py celery -A django_forms beat -f C:\Projects\Django\django_forms\logs\beat.log -l debug
Info 0x400000FF - Started. Waiting for stop

None of the tasks are running though. Nor are the beat.log or celery.log file being created.

Do my services look correct?

mrbean-bremen commented 7 years ago

Hm, I don't have much experience with Celery (I replaced it with a more light-weight solution some time ago), and I don't have the environment to test this here (at home). I tested that environment (Python 3, Windows 10) only with the FastCGI/IIS stuff, where it works - though for deployment we don't use a virtual environment, as it does not go very well with Windows services. So I currently don't have an idea, sorry... I may have another look tomorrow.

iarp commented 7 years ago

Hmm ok, I'm trying to eliminate about 40 Scheduled Tasks I currently have running on our server. Celery or the like would be a giant help.

mrbean-bremen commented 7 years ago

Oh, celery will certainly help - it just was overkill for us, as we had only a few tasks that can be handled easily using a light weight scheduler. We had celery running under Django 1.6 IIRC, Python 2.7 and maybe even Windows XP (don't remember), I just haven't used it since. And we didn't use a virtual environment in the deployment, as I said. Currently we use Django 1.11, Python 3.6 and Windows 10, but only for FastCGI/IIS, as mentioned. One thing I just noticed - there is pypiwin32 as a requirement, I remember having some problems with that. You can try to use the binary pywin32 installer instead (using easy_install if you are working in the virtual environment) - but that is just a shot in the dark. Also make sure that your log file directory is existing and writable from the service user. You can also try to use the plain Python installation instead of a virtual environment just in case (preferable installing Python in a path without spaces, as this could be another caveat). I currently don't have the time to test it myself - maybe I can try something on the weekend, if I find the time.

iarp commented 7 years ago

It's ok. I eneded up coming across this page and its worked well so far.

I think the issue I had/have is the commands being used. The default services.ini wants to use celeryd and celerybeat in the same fashion where you would run it like python manage.py celeryd or python manage.py celerybeat however in the latest versions those commands no longer work. I'm having to use celery.exe found within the Scripts folder.

We have too many other projects that run on the server to not use a virtualenv for this project. I have been able to install pypiwin32 without any issue.

mrbean-bremen commented 7 years ago

Ok, good that you have it running - these where just things I could think of off my head. About pypiwin32 - right, that was the previous version that did not work (219), which is the only version that is available for Python 2 - it is fixed in 220 (Python 3 only). The task scheduler is a good idea if services are a problem indeed - didn't think about that. So you are saying that the manage commands no longer work for celery? Maybe we have to update the README accordingly in this case. I would appreciate if you can add the information needed to run celery with a current version if you get it running fine - I would add it to the README, or you can add a PR, if you want. Thanks!

iarp commented 7 years ago
(venv-django_forms) C:\Projects\Django\django_forms>python manage.py celeryd
Unknown command: 'celeryd'
Type 'manage.py help' for usage.

(venv-django_forms) C:\Projects\Django\django_forms>python manage.py celerybeat
Unknown command: 'celerybeat'
Type 'manage.py help' for usage.

The commands I use are:

celery -A django_forms worker -l info -P eventlet
celery -A django_forms beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler

celery can be found in the Scripts/ folder (D:\virtualenvs-36\venv-django_forms\Scripts\)

The worker command I use required -P eventlet on my machines.

Python: 3.6.1 Django: 1.10.8 Windows: 10 and Server 2008 R2 Celery: 4.1.0

mrbean-bremen commented 7 years ago

Thanks - I will check if I can update the documentation later.

lewisMachilika commented 4 years ago

if anyone got this right, please help