AzureのWebジョブでaborted due to no output nor CPU activity for 121 seconds.が発生した時の対処方法

Azure の Webジョブを稼働させているとき、稀にエラーで終了しているときがあります。

Azure ポータルで確認するとこんな感じ。
![20201211-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/946371/46c12041-bc01-1855-6a6d-e3eb833964a0.png)

上の画面からログを押して、エラーの状況を確認するとメッセージは以下のような感じで、ようするに
「121 秒の間 CPU を使わずアイドル状態だったので終了させましたよ」
というもの。

```Error.log
[12/08/2020 14:11:56 > be9f97: ERR ] Command 'cmd /c ""XXXXXX ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed.
cmd /c ""XXXXX.exe""
[12/08/2020 14:11:56 > be9f97: SYS INFO] Status changed to Failed
[12/08/2020 14:11:56 > be9f97: SYS ERR ] System.AggregateException: One or more errors occurred. ---> Kudu.Core.Infrastructure.CommandLineException: Command 'cmd /c ""XXXXX ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed.
cmd /c ""XXXXX.exe""
   at Kudu.Core.Infrastructure.IdleManager.WaitForExit(IProcess process) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\IdleManager.cs:line 96
   at Kudu.Core.Infrastructure.ProcessExtensions.<Start>d__13.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\ProcessExtensions.cs:line 254
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Kudu.Core.Infrastructure.Executable.<ExecuteAsync>d__31.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 255
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at Kudu.Core.Infrastructure.Executable.ExecuteInternal(ITracer tracer, Func`2 onWriteOutput, Func`2 onWriteError, Encoding encoding, String arguments, Object[] args) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 216
   at Kudu.Core.Infrastructure.Executable.ExecuteReturnExitCode(ITracer tracer, Action`1 onWriteOutput, Action`1 onWriteError, String arguments, Object[] args) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 165
   at Kudu.Core.Jobs.BaseJobRunner.RunJobInstance(JobBase job, IJobLogger logger, String runId, String trigger, ITracer tracer, Int32 port) in C:\Kudu Files\Private\src\master\Kudu.Core\Jobs\BaseJobRunner.cs:line 272
---> (Inner Exception #0) ExitCode: -1, Output: Command 'cmd /c ""XXXXX ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed., Error: Command 'cmd /c ""XXXXX ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed., Kudu.Core.Infrastructure.CommandLineException: Command 'cmd /c ""XXXXX ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed.
cmd /c ""XXXXX.exe""
   at Kudu.Core.Infrastructure.IdleManager.WaitForExit(IProcess process) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\IdleManager.cs:line 96
   at Kudu.Core.Infrastructure.ProcessExtensions.<Start>d__13.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\ProcessExtensions.cs:line 254
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Kudu.Core.Infrastructure.Executable.<ExecuteAsync>d__31.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 255<---
```

確かに15秒程度の Thread.Sleep はあるのだが、121 秒もアイドルなことはないと思うけど....

でもなぜか不定期に発生する。

で、対応としては上のメッセージにもあるように WEBJOBS_IDLE_TIMEOUT を設定してタイムアウト値を延ばす。

Webジョブが稼働する App Service を開き、**構成** → **＋新しいアプリケーション設定** を選択。
名前：WEBJOBS_IDLE_TIMEOUT 
値：秒を指定（例では 3,600 = 1 時間）
を入力して **OK**
![20201211-01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/946371/2f15714b-086c-1864-db57-c05e7578d747.png)

その後、**保存** を選択すると

> アプリケーション設定と接続文字列を変更すると、アプリケーションが再起動します。続行しますか？

と聞かれるので**続行**を選択。

注意点は、これで設定した App Service 全体が再起動となるのだが、App Service そのものは再起動されるが、配下で稼働している Web ジョブは停止したままとなるので必要に応じて手動で起動が必要なこと。