Home > database >  Django model save not working in daemon mode but working with runserver
Django model save not working in daemon mode but working with runserver

Time:05-21

I am saving github repo to server once user add their github repo, see this models.

class Repo(models.Model):
    url = models.CharField(help_text='github repo cloneable',max_length=600)
    def save(self, *args, **kwargs):
        # os.system('git clone https://github.com/somegithubrepo.git')
        os.system('git clone {}'.format(self.url))
        super(Repo, self).save(*args, **kwargs)

Everything is working fine what I want in both local server and remote server like digitalOcean Droplet, when I add public github repo, the clone always success.

it works when I run the server like this: python3 manage.py runserver 0.0.0.0:800

But when I ran in daemon mode with gunicorn and nginx, it doesn't work,

Everything is working even saving the data in database, only it is not cloning in daemon mode, what's wrong with it?

this is my gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data

WorkingDirectory=/var/www/myproject
Environment="PATH=/root/.local/share/virtualenvs/myproject-9citYRnS/bin"
ExecStart=/usr/local/bin/pipenv run gunicorn --access-logfile - --workers 3 --bind unix:/var/www/myproject/config.sock -m 007 myproject.wsgi:application

[Install]
WantedBy=multi-user.target

Note again, everything is working even the gunicorn, the data is saving in database, only it is not cloning github repo, even not firing any error.\

What's the wrong with this? can anyone please help me to fix the issue?

CodePudding user response:

In the systemd unit file, you overwrite the root user's PATH variable in Environment with the path of the virtualenv. Therefore the root user does not have the usual items in the PATH, e.g. /usr/bin where the git command usually exists. So you need to use the absolute path of git, e.g. /usr/bin/git, or add the git's bin directory to the PATH, for example:

Environment="PATH=/usr/bin:/root/.local/share/virtualenvs/myproject-9citYRnS/bin"

CodePudding user response:

I had the same problem with apache. That means the database is not writable for www-data user.

If it is using sqlite, you need to make sure the sqlite db file. After I do these it works:

sudo chown www-data:www-data db/sqlite.db
sudo chown www-data:www-data db

Ability to write directory is also required as a temporary lock file will be created.

  • Related