我有 2 个 Azure Web 作业,带有在 Web 应用程序中创建的计时器触发器。我们称它们为
AccountCreatedTimerWebJob
和 AccountUpdatedTimerWebJob
。
Web 作业和 Web 应用程序共享同一个项目(单体架构)。创建构建和发布管道,以便输出文件同时存在于
App_Data/jobs/continuous/AccountCreatedTimerWebJob
和 App_Data/jobs/continuous/AccountUpdatedTimerWebJob
中。
parameters:
- name: projectName
type: string
- name: webJobs
type: object
steps:
- task: DotNetCoreCLI@2
displayName: 'Publish ${{ parameters.projectName }}'
inputs:
command: publish
arguments: '--configuration Release --output publish_output/${{ parameters.projectName }}'
projects: '**/${{ parameters.projectName }}.csproj'
publishWebProjects: false
modifyOutputPath: false
zipAfterPublish: false
- ${{ each webJob in parameters.webJobs }}:
- task: CopyFiles@2
displayName: "${{ webJob.name }}: Copy files"
inputs:
SourceFolder: '$(System.DefaultWorkingDirectory)/publish_output/${{ parameters.projectName }}'
Contents: '**'
TargetFolder: '$(System.DefaultWorkingDirectory)/publish_output/${{ parameters.projectName }}/App_Data/jobs/${{ webJob.type }}/${{ webJob.name }}'
- task: DeleteFiles@1
inputs:
SourceFolder: '$(System.DefaultWorkingDirectory)/publish_output/${{ parameters.projectName }}/App_Data/jobs'
Contents: '**/**/App_data'
- task: ArchiveFiles@2
displayName: "Archive ${{ parameters.projectName }}"
inputs:
rootFolderOrFile: "$(System.DefaultWorkingDirectory)/publish_output/${{ parameters.projectName }}"
includeRootFolder: false
archiveFile: "$(System.DefaultWorkingDirectory)/${{ parameters.projectName }}.zip"
- task: PublishBuildArtifacts@1
displayName: "Artifact ${{ parameters.projectName }}"
inputs:
PathtoPublish: '$(System.DefaultWorkingDirectory)/${{ parameters.projectName }}.zip'
artifactName: '${{ parameters.projectName }}'
Kudu 调试控制台:
C:\home\site\wwwroot\App_Data\jobs\continuous>dir
Volume in drive C is Windows
Volume Serial Number is AC73-4F38
Directory of C:\home\site\wwwroot\App_Data\jobs\continuous
01/02/2025 11:08 AM <DIR> AccountCreatedTimerWebJob
01/02/2025 11:08 AM <DIR> AccountUpdatedTimerWebJob
0 File(s) 0 bytes
2 Dir(s) 0 bytes free
在程序文件中我添加了这些代码:
builder.Host.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddTimers();
});
AccountCreatedTimerWebJob
工作正常,但AccountUpdatedTimerWebJob
一直等待重新启动。
当我查看 Application Insights 内部时,我发现出现了此异常:
System.Net.Sockets.SocketException:无法绑定到地址http://127.0.0.1:5000:地址已在使用中。通常只允许每个套接字地址(协议/网络地址/端口)使用一次。
是否有可能解决此问题或为其他网络作业使用其他端口?
发现不需要为每个 Web 作业创建一个循环。一项名为
AllWebJobTriggers
的 Web 作业将运行所有 Web 作业。
parameters:
- name: projectName
type: string
steps:
- task: DotNetCoreCLI@2
displayName: 'Publish ${{ parameters.projectName }}'
inputs:
command: publish
arguments: '--configuration Release --output publish_output/${{ parameters.projectName }}'
projects: '**/${{ parameters.projectName }}.csproj'
publishWebProjects: false
modifyOutputPath: false
zipAfterPublish: false
- task: CopyFiles@2
displayName: "Copy files"
inputs:
SourceFolder: '$(System.DefaultWorkingDirectory)/publish_output/${{ parameters.projectName }}'
Contents: '**'
TargetFolder: '$(System.DefaultWorkingDirectory)/publish_output/${{ parameters.projectName }}/App_Data/jobs/continuous/AllWebJobTriggers'
- task: ArchiveFiles@2
displayName: "Archive ${{ parameters.projectName }}"
inputs:
rootFolderOrFile: "$(System.DefaultWorkingDirectory)/publish_output/${{ parameters.projectName }}"
includeRootFolder: false
archiveFile: "$(System.DefaultWorkingDirectory)/${{ parameters.projectName }}.zip"
- task: PublishBuildArtifacts@1
displayName: "Artifact ${{ parameters.projectName }}"
inputs:
PathtoPublish: '$(System.DefaultWorkingDirectory)/${{ parameters.projectName }}.zip'
artifactName: '${{ parameters.projectName }}'