kaxil commented on code in PR #73352:
URL: https://github.com/apache/airflow/pull/73352#discussion_r4462760657


##########
providers/ssh/src/airflow/providers/ssh/utils/remote_job.py:
##########
@@ -271,37 +271,43 @@ def build_windows_wrapper_command(
             env_setup += f"$env:{key} = '{escaped_value}'; "
 
     def ps_escape(s: str) -> str:
+        return s.replace("`", "``").replace('"', '`"')
+
+    def ps_escape_sq(s: str) -> str:
         return s.replace("'", "''")
 
     job_dir = ps_escape(paths.job_dir)
-    log_file = ps_escape(paths.log_file)
-    exit_code_file = ps_escape(paths.exit_code_file)
-    exit_code_tmp = ps_escape(paths.exit_code_tmp_file)
-    pid_file = ps_escape(paths.pid_file)
-    status_file = ps_escape(paths.status_file)
+    sep = paths.sep
+    job_id = ps_escape_sq(paths.job_id)
     escaped_command = ps_escape(command)
-    job_id = ps_escape(paths.job_id)
 
-    child_script = f"""$ErrorActionPreference = 'Continue'
-$env:LOG_FILE = '{log_file}'
-$env:STATUS_FILE = '{status_file}'
+    # Use $d and $b for directories to avoid passing long paths multiple times.
+    # Write command to disk to avoid double base64 encoding.
+    # Add-Content writes each pipeline object to disk immediately (no block 
buffering),
+    # giving real-time log updates.
+    # Launch as Win32_Process so that it doesn't die when SSH exits.
+    child_script = f"""$ErrorActionPreference = "Continue"
+$b = $PSScriptRoot
+$env:LOG_FILE = "$b{sep}stdout.log"
+$env:STATUS_FILE = "$b{sep}status"
 {env_setup}
-{escaped_command}
+{escaped_command} 2>&1 | Add-Content -Path $env:LOG_FILE

Review Comment:
   Fixed in c1bccb4: the command runs as `cmd.ps1` with `*>&1`, so every 
statement and stream, `Write-Host` included, reaches the log, which now streams 
while the job runs.



##########
providers/ssh/src/airflow/providers/ssh/utils/remote_job.py:
##########
@@ -271,37 +271,43 @@ def build_windows_wrapper_command(
             env_setup += f"$env:{key} = '{escaped_value}'; "
 
     def ps_escape(s: str) -> str:
+        return s.replace("`", "``").replace('"', '`"')
+
+    def ps_escape_sq(s: str) -> str:
         return s.replace("'", "''")
 
     job_dir = ps_escape(paths.job_dir)
-    log_file = ps_escape(paths.log_file)
-    exit_code_file = ps_escape(paths.exit_code_file)
-    exit_code_tmp = ps_escape(paths.exit_code_tmp_file)
-    pid_file = ps_escape(paths.pid_file)
-    status_file = ps_escape(paths.status_file)
+    sep = paths.sep
+    job_id = ps_escape_sq(paths.job_id)
     escaped_command = ps_escape(command)
-    job_id = ps_escape(paths.job_id)
 
-    child_script = f"""$ErrorActionPreference = 'Continue'
-$env:LOG_FILE = '{log_file}'
-$env:STATUS_FILE = '{status_file}'
+    # Use $d and $b for directories to avoid passing long paths multiple times.
+    # Write command to disk to avoid double base64 encoding.
+    # Add-Content writes each pipeline object to disk immediately (no block 
buffering),
+    # giving real-time log updates.
+    # Launch as Win32_Process so that it doesn't die when SSH exits.
+    child_script = f"""$ErrorActionPreference = "Continue"
+$b = $PSScriptRoot
+$env:LOG_FILE = "$b{sep}stdout.log"
+$env:STATUS_FILE = "$b{sep}status"
 {env_setup}
-{escaped_command}
+{escaped_command} 2>&1 | Add-Content -Path $env:LOG_FILE
 $ec = $LASTEXITCODE
 if ($null -eq $ec) {{ $ec = 0 }}
-Set-Content -NoNewline -Path '{exit_code_tmp}' -Value $ec
-Move-Item -Force -Path '{exit_code_tmp}' -Destination '{exit_code_file}'
+Set-Content -NoNewline -Path "$b{sep}exit_code.tmp" -Value $ec
+Move-Item -Force -Path "$b{sep}exit_code.tmp" -Destination "$b{sep}exit_code"
 """
-    child_script_bytes = child_script.encode("utf-16-le")
-    encoded_script = base64.b64encode(child_script_bytes).decode("ascii")
 
-    wrapper = f"""$jobDir = '{job_dir}'
-New-Item -ItemType Directory -Force -Path $jobDir | Out-Null
-$log = '{log_file}'
-'' | Set-Content -Path $log
+    wrapper = f"""$d = "{job_dir}"
+New-Item -ItemType Directory -Force -Path $d | Out-Null
+$sf = "$d{sep}job.ps1"
+@'
+{child_script}'@ | Set-Content -Path $sf -Encoding UTF8
+'' | Set-Content -Path "$d{sep}stdout.log"
 
-$p = Start-Process -FilePath 'powershell.exe' -ArgumentList @('-NoProfile', 
'-NonInteractive', '-EncodedCommand', '{encoded_script}') 
-RedirectStandardOutput $log -RedirectStandardError $log -PassThru -WindowStyle 
Hidden
-Set-Content -NoNewline -Path '{pid_file}' -Value $p.Id
+$cmd = "powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass 
-File `"$sf`""
+$p = ([wmiclass]'Win32_Process').Create($cmd, $null, $null)

Review Comment:
   Fixed in c1bccb4: a non-zero `ReturnValue` now fails the submission before a 
pid or job id is written, the job starts in the SSH session's directory, and 
the docstring describes the new launch.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to