7

I'm trying to run python script from Azure webjob. This is what I've done following this link

  1. Access the kudu tool via the url https://<webapp name>.scm.azurewebsites.net and installed Python 364x86 via Site Extensions tab
  2. Confirmed Python 364x86 is installed in the following path: D:\home\python364x86
  3. Added my script trading.py in D:\home\python364x86
  4. Created run.bat file with this line of code D:\home\python364x86\python.exe trading.py
  5. Included run.bat and trading.py in the webjob zip file
  6. Deployed, but getting error
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Status changed to Initializing
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Run script 'run.bat' with script host - 'WindowsScriptHost'
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Status changed to Running
[09/07/2019 07:02:00 > 0dd02c: ERR ] The filename, directory name, or volume label syntax is incorrect.
[09/07/2019 07:02:00 > 0dd02c: INFO] 
[09/07/2019 07:02:00 > 0dd02c: INFO] D:\local\Temp\jobs\triggered\z\2az54ret.wh4>D:\home\python364x86\python.exe trading.py 
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Status changed to Failed
[09/07/2019 07:02:00 > 0dd02c: SYS ERR ] Job failed due to exit code 1

Functions.cs

public void StartTheBot()
        {        
            // Local   
            //var fileName = @"C:\Users\robert\AppData\Local\Programs\Python\Python37-32\python.exe";
            //var script = @"C:\python-scripts\trading.py";

            // Production
            var fileName = @"D:\home\python364x86\python.exe";
            var script = @"D:\home\python364x86\trading.py";
            var errors = "";
            var results = "";        

            ProcessStartInfo psi = new ProcessStartInfo
            {
                FileName = fileName,
                Arguments = script,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            };            

            using (Process process = Process.Start(psi))
            {
                errors = process.StandardError.ReadToEnd();
                results = process.StandardOutput.ReadToEnd();               
            }

            Console.WriteLine("Errors:");
            Console.WriteLine(errors);
            Console.WriteLine();
            Console.WriteLine("Results:");
            Console.WriteLine(results);
        }

Above code executes python script. It works locally, but once I deploy it to production it fails. Tried so many times, spent plethora of hours, but still unsure why prod doesn't work. Help is appreciated.

trading.py

import telegram

my_token = 'mytoken'
bot = telegram.Bot(token = my_token)

chat_id = 'mychatid'
message = 'Hello
bot.sendMessage(chat_id=chat_id, text=message)
bbusdriver
  • 1,577
  • 3
  • 26
  • 58
  • Could you post some necessary codes of `trading.py` at here? I see your `run.bat` file has been triggered by Azure WebApp successfully, but just your `trading.py` script seems not to run incorrectly. I think there may be some issue of script code conflict with the limitation of WebApp runtime. – Peter Pan Sep 07 '19 at 09:00
  • Thanks for your comment! Well `trading.py` is just a simple one line `print("hi")` – bbusdriver Sep 07 '19 at 09:17
  • @PeterPan Ok, so this is what I tried. On Configuration's General settings tab I selected Python to be the stack and matched the version 3.6. In this way I could now get rid of `run.bat` file and run it naturally. And now I included a telegram library in `trading.py` but the message is not being sent. I tried to install the library following your article but the command is not relevant anymore – bbusdriver Sep 07 '19 at 19:39

1 Answers1

9

I tried to realize your needs with Python in WebJob and successfully make it works.

Here is my steps and sample code. My local environment is Python 3.7 on Windows 10.

  1. Create a Python virtual environment and install python-telegram-bot package via commands as below.

    $ mkdir telegram-webjob
    $ virtualenv telegram-webjob
    $ cd telegram-webjob
    $ Scripts\active
    $ pip install python-telegram-bot
    $ pip freeze
    

    enter image description here

  2. I changed your code as my sample code, then run it works fine on local, as below.

    import telegram
    from datetime import datetime as dt
    
    my_token = '<my token>'
    bot = telegram.Bot(token = my_token)
    
    chat_id = '<chat id>'
    message = f"Hello at {dt.now()}"
    bot.sendMessage(chat_id=chat_id, text=message)
    

    enter image description here

    enter image description here

  3. I created a new directoy named webjob and copy my trading.py file and all directories with their files into it, as below.

    enter image description here

  4. Write the startup bat script named run.bat, the code as below.

    D:\home\python364x86\python.exe trading.py
    

    The D:\home\python364x86\python.exe path is the python364x86 site extension installed path as the figure below.

    enter image description here

  5. Then, I packaged the all directories and files in the webjob directory as a zip file, as below.

    enter image description here

  6. I uploaded it to Azure WebApp as a webjob, as the figure below, and start it.

    enter image description here

    enter image description here

Finally, it works fine for me and I can see the message interval 10 sec displayed in my telegram client.

enter image description here

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • Thank you very much! I'd appreciate your effort. I will definitely try this. One question is, I need to get a return value from `trading.py` and this is why I'm using `ProcessStartInfo`. Can your solution also include c# part where I can run `StartTheBot` function? – bbusdriver Sep 09 '19 at 08:36
  • @bbusdriver Two ways: 1. A function does the RPC opertion to send a HTTP request to your C# service to post the return data at the end of `trading.py` codes; 2. Follow the [``WebJob API] document to manually trigger the webjob from C# to get the 200 status code. – Peter Pan Sep 09 '19 at 08:53
  • @Peter Pan I have a similar issue, cant get it going. Did something change? https://stackoverflow.com/questions/64547314/webjobs-running-error-3587fd-err-from-zipfile/64549879#64549879 – wwnde Oct 27 '20 at 09:07