0

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

totor
  • 156
  • 1
  • 9

2 Answers2

0

According to your description, per my experience, I think the issue was caused by using the relative path in your code.

On Azure WebApps, we have the permission of doing any operations under the path D:/home.

My suggestion is using the absolute path instead of the relative path, such as D:/home/site/wwwroot/App_Data/jobs/triggered/<triggered-job-name>/test.txt or /home/site/wwwroot/App_Data/jobs/triggered/<triggered-job-name>/test.txt instead of test.txt. And please make sure the directory & parent directories had been made via use os.path.exists(path) to check the path exists and use os.makedirs to make them before writing a file.

Meanwhile, you also can try to use the WebJobs API to do some operations, such as creating a webjob via uploading a zip file or updating settings.job. For using these WebJobs APIs, be sure you had configured the Deployment credentials on Azure portal as the figure below, and add the basic auth Authorization: Basic <BASE64-Encode("deployment-username":"password")>in the request header.

enter image description here

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • Thanks ! The crash is actually due to the shutil.copy() function. The file gets copied in the webjob directory, but then an error happens, and the code fails. Any idea? I also tried shutil.copyfile (both with absolute paths) but it fails too – totor Feb 16 '17 at 16:43
  • There is no specific information, even in the Azure logs. Just Server error 500 But see my answer below, I am quite sure that this is due to the fact that azure tries to have access to the "run.py" file (maybe to copy it in the temp directory?) once it is detected – totor Feb 17 '17 at 21:05
0

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 !

totor
  • 156
  • 1
  • 9