Dotnet 6触发的Webjob在部署后不会直接运行

huangapple go评论49阅读模式
英文:

Dotnet 6 Trigger Webjob doesn't run directly after deployment

问题

我在Azure中托管了一个Web应用程序。现在我为它创建了一个触发式的Web作业。我成功部署了它,但每次部署后状态都是'准备就绪',直到我在Azure的应用服务中手动运行Web作业,它才开始工作。

还有一个小问题,希望能够修复。计划已经存在,但在我手动运行计时作业后,它将按照正确的时间间隔工作,但在Azure的应用服务中的Web作业部分,计划将不会显示。

有什么建议吗?

目标是在部署后,触发式计时作业可以自动开始工作,无需手动在Azure中运行它。

英文:

I have a Webapp hosting in Azure. Now I created a Triggerd webjob for it. I could deploy it successfully, but after each deployment the status is 'ready' and it will not start to work until I run the WbJobs manually from App Service in Azure.

There is anohter small problem that is nice to have to be fixed. The Schedule is there and after I manually run the TimerJob it will work in correct time interval but in Azure in the App Service in WebJobs section the schedule will not be shown.

any idea?

Goal is that after deployment the Triggered timer job begin its work without to run it manually from the Azure.

答案1

得分: 1

I found the problem and fixed it.

但问题是什么呢?我想在我的解决方案中添加一个类型为触发器的 Webjob,然后使用 YAML 文件部署到 Azure 上的应用服务中。在以下链接中,我找到了编写触发 Webjob 的方法:https://thechimpbrain.com/time-triggered-azure-webjob/

在这个链接中,作者创建了一个 .NET Core 的控制台应用程序,并在程序文件中创建了一个主机生成器(hostbuilder),这个主机会不断运行,而具有 TimerTrigger 的函数将在特定时间调用该函数。

但这不是我想要的,因为如果你有一个应用服务,那么已经有一个持续运行的主机。你只需要定义触发时间。

这意味着你不需要从这个链接中获取任何东西。你只需要创建一个控制台应用程序并编写你的代码。你只需在项目中添加一个 settings.job 文件。在那里,你应该像这样定义你的计划:

{
  "schedule": "0 0 1 * * *"
}

在这个示例中,它每天都会运行。

然后,你应该将项目部署到正确的位置,就像在我的情况下是 App_Data/jobs/triggered/{YourJobName}

在你的 YAML 文件中,你将有两个步骤,CI 和 CD。

在 CI 部分:

- task: DotNetCoreCLI@2
  displayName: 'Build Webjob Montly'
  inputs:
    command: 'build'
    projects: 'xxx/xxx/xxx.csproj'
    arguments: '--configuration $(BuildConfiguration)'

- task: DotNetCoreCLI@2
  displayName: 'publish Webjob Montly'
  inputs:
    command: 'publish'
    publishWebProjects: false
    projects: 'xxx/xxx/xxx.csproj'
    arguments: '--output $(Build.BinariesDirectory)/publish_Webjob/App_Data/jobs/triggered/WebjobMontly'
    zipAfterPublish: false
    modifyOutputPath: false

- task: ArchiveFiles@2
  displayName: 'Zip Webjobs'
  inputs:
    rootFolderOrFile: '$(Build.BinariesDirectory)/publish_Webjob'
    includeRootFolder: false
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/Webjobs.zip'
    replaceExistingArchive: true

- task: PublishPipelineArtifact@1
  displayName: Publish drop Artifact
  inputs:
    targetPath: '$(Build.ArtifactStagingDirectory)'
    artifact: 'drop'
    publishLocation: 'pipeline'

在 CD 部分:

- task: AzureRmWebAppDeployment@4
  displayName: 'Deploy Webjobs'
  inputs:
    ConnectionType: 'AzureRM'
    azureSubscription: 'xxx'
    appType: 'webApp'
    WebAppName: 'xxx'
    deployToSlotOrASE: true
    ResourceGroupName: 'xxx'
    SlotName: 'Test'
    packageForLinux: '$(pipeline.workspace)/drop/Webjobs.zip'
    JSONFiles: '**/appsettings.json'

就是这样。

英文:

I found the problem and fixed it.

But what was the problem? I wanted to add a Webjobs of type trigger in my solution and then deploy it with a YAML file into the Azure in a App Service.
Under this link I found the way to write a Triggerd Webjob:
https://thechimpbrain.com/time-triggered-azure-webjob/

In this link the person creates a console application for .net core and in the program file he/she create a hostbuider and this host will run constantly and a function which has the TimerTrigger will call the function in certain time.

But this is not what I want, because if you have an app service, there is already a host running continuously. you should only define the trigger time.

That means you don't need anything form this link. All you do is create a console app and write your code. You should only add a settings.job file in your project. There you should define your schedule like this:

{
  "schedule": "0 0 1 * * *"
}

In this example it will run every day.

Then you should deploy your project in to the correct place which is in my case App_Data/jobs/triggered/{YourJobName}

In your YAML you will have two steps, CI and CD.

In your CI:

      - task: DotNetCoreCLI@2
        displayName: 'Build Webjob Montly'
        inputs:
          command: 'build'
          projects: 'xxx/xxx/xxx.csproj'
          arguments: '--configuration $(BuildConfiguration)'

      - task: DotNetCoreCLI@2
        displayName: 'publish Webjob Montly'
        inputs:
          command: 'publish'
          publishWebProjects: false
          projects: 'xxx/xxx/xxx.csproj'
          arguments: '--output $(Build.BinariesDirectory)/publish_Webjob/App_Data/jobs/triggered/WebjobMontly'
          zipAfterPublish: false
          modifyOutputPath: false

      - task: ArchiveFiles@2
        displayName: 'Zip Webjobs'
        inputs:
          rootFolderOrFile: '$(Build.BinariesDirectory)/publish_Webjob'
          includeRootFolder: false
          archiveType: 'zip'
          archiveFile: '$(Build.ArtifactStagingDirectory)/Webjobs.zip'
          replaceExistingArchive: true

      - task: PublishPipelineArtifact@1
        displayName: Publish drop Artifact
        inputs:
          targetPath: '$(Build.ArtifactStagingDirectory)'
          artifact: 'drop'
          publishLocation: 'pipeline'

and in your CD:

            - task: AzureRmWebAppDeployment@4
              displayName: 'Deploy Webjobs'
              inputs:
                ConnectionType: 'AzureRM'
                azureSubscription: 'xxx'
                appType: 'webApp'
                WebAppName: 'xxx'
                deployToSlotOrASE: true
                ResourceGroupName: 'xxx'
                SlotName: 'Test'
                packageForLinux: '$(pipeline.workspace)/drop/Webjobs.zip'
                JSONFiles: '**/appsettings.json'

and that's it.

答案2

得分: 0

以下是翻译好的部分:

  • 目标是在部署后,触发的定时作业应自动开始工作,而无需手动运行它从 Azure。

  • 检查以下步骤以自动运行触发的定时作业。

  • Visual Studio 中创建一个控制台应用程序。

  • 构建并在本地运行应用程序。

  • .exe 文件压缩到应用程序的 bin/net6.0 文件夹中。

  • 在 Azure 门户中,创建一个具有运行时堆栈 .NET6 的应用服务。

  • 应用服务 => WebJobs 中,添加一个新的类型 Triggered - Scheduled WebJob

  • 使用我提供的 CRON 表达式,即 0 */2 * * * *,WebJob 将在 2 分钟后启动。并且每 2 分钟运行一次。

  • 请查看 MSDoc 以获取 CRON 表达式的详细信息。

  • 初始 WebJob

  • 2 分钟后

  • 我们可以在 日志 下检查 WebJob 状态。

  • 选择 WebJob 并单击 日志

  • 单击可用的作业名称。

  • 您可以看到每 2 分钟触发并运行 Web Job。

  • 为了测试作业,我已安排它在几分钟内运行。

  • 请再次检查您的 CRON 表达式。尝试使用较短的时间并进行交叉检查。

英文:

> Goal is that after deployment the Triggered timer job begin its work without to run it manually from the Azure.

Check the below steps to run the Triggered timer job run automatically.

  • Create a Console Application in Visual Studio.

Dotnet 6触发的Webjob在部署后不会直接运行

  • Build and run the Application locally.
  • Zip the .exe file in the application bin/net6.0 folder.

Dotnet 6触发的Webjob在部署后不会直接运行

  • In Azure Portal, Create an App Service with Runtime stack .NET6 .

Dotnet 6触发的Webjob在部署后不会直接运行

  • In App Service => WebJobs, add a new Type Triggered - Scheduled WebJob.

Dotnet 6触发的Webjob在部署后不会直接运行

  • With the CRON expression which I have given 0 */2 * * * *, the webjob will start after 2 minutes. And will run after every 2 minutes.

Check the MSDoc for CRON expressions.

Initial WebJob

Dotnet 6触发的Webjob在部署后不会直接运行

After 2 min

Dotnet 6触发的Webjob在部署后不会直接运行

  • We can check the WebJob status, under Logs.
  • Select the WebJob and click on Logs.

Dotnet 6触发的Webjob在部署后不会直接运行

Click on the Available Job Name.

Dotnet 6触发的Webjob在部署后不会直接运行

  • You can see for every 2 minutes the Web Job is getting triggered and running.

Dotnet 6触发的Webjob在部署后不会直接运行

  • For testing the job, I have scheduled it to run within few minutes.

Check your CRON expression once.Try with the less timing and cross check.

huangapple
  • 本文由 发表于 2023年2月8日 22:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定