Search code examples
pythondjangoazurewebjob

Create Azure webjobs from Django/python web app


I have a Django app deployed on a Azure Web App, and I want to dynamically create webjobs. More precisely, when I save a Django model called Importer, I want to be able to create a new web job, made of 3 files:

  • run.py : the Python webjob
  • settings.job : the cron schedule
  • config.ini : a config file for the webjob

The content of the "settings.job" & "config.ini" comes from an admin form and is specific to the webjob.

When I save my model, the code creates a new directory in

App_Data\jobs\triggered{my job name}

, and copies there the "run.py" from my app directory.

This works. However, when I try to create a new text file called "settings.job" in the job directory and write the cron schedule in it, I got a server error.

I tried many things, but the following basic test causes a failure:

file = open('test.txt','w')
file.write('this is a test')
file.close()

It seems that I don't have the right to write a file to the disk. How can that be solved? Also, I want to be able to modify the content of the config and settings.job files when I update the corresponding Django model.

In addition, I tried to copy another file called "run2.py" to the webjob directory, and that fails too ! I cannot copy another file that run.py in that directory


Solution

  • Here is my analysis:

    • there is no problem copying other files than "run.py" to the webjob directory
    • the code crashes after (successfully) copying the file "run.py" from my Django app directory to the webjob directory. It does not matter if I use shutil.copy/shutil.copyfile or simply open("{path}/run.py","w"), the problem occurs when I try to write a file called "run.py" to the disk in the webjob directory.

    I think that when we create a new webjob directory, if the system detects a file called "run.py" it tries to carry out some operations. Then there is a conflict with two processes trying to access the same file at the same time.

    My solution: I copy the python script to the webjob directory with the name "myrun.txt", and then I rename it to run.py using os.rename

    It works !