从MSBuild执行长时间运行的SQL

时间:2014-06-25 21:48:08

标签: msbuild sqlcommand

我希望使用MSBuild实现一个进程来调用长时间运行的sql SPROC(它将大量数据填充到某些表中)。我有以下代码从我的构建服务器调用SPROC

    <Target Name="Test"  AfterTargets="Deploy">
    <MSBuild.ExtensionPack.SqlServer.SqlExecute TaskAction="ExecuteRawReader" Sql="DECLARE  @return_value int, @Result nvarchar(1000) EXEC  @return_value = [dbo].[usp_ProcessWarehouse] @Result = @Result OUTPUT SELECT    @Result as N'@Result' SELECT    'Return Value' = @return_value"
        ConnectionString="Data Source=localhost;Initial Catalog=ETL;Integrated Security=True;Connection Timeout=10000">
        <Output PropertyName="RawResult" TaskParameter="RawReaderResult"/>
    </MSBuild.ExtensionPack.SqlServer.SqlExecute>
    <Message Text="$(RawResult)"/>
</Target>

SPROC如下

ALTER PROCEDURE [dbo].[usp_ProcessWarehouse]
   @Result Nvarchar(1000) OUTPUT
AS
EXEC msdb.dbo.sp_start_job N'MB_ProcessWarehouse'
WaitFor Delay '00:00:05'
Declare @cnt int = 0
WHILE (@cnt <> 5)
BEGIN    
   set @cnt =   (
                 SELECT count(*)
                 FROM msdb.dbo.sysjobactivity AS sja
                 INNER JOIN msdb.dbo.sysjobs AS sj ON sja.job_id = sj.job_id
                 WHERE sja.start_execution_date IS NOT NULL
                 AND sja.stop_execution_date IS NULL)
                 if @cnt = 0
                 Begin
                 set  @cnt = 5
                 Break
                 End
                 Print 'Job Still Running'
                 WaitFor Delay '00:00:05'
END
Print 'Job Completed'

Set @result = (
               SELECT top 1
               CASE jh.run_status WHEN 0 THEN 'Failed'
               WHEN 1 THEN 'Success'
               WHEN 2 THEN 'Retry'
               WHEN 3 THEN 'Canceled'
               WHEN 4 THEN 'In progress'
               END AS Status
               FROM msdb.dbo.sysjobs j 
               INNER JOIN msdb.dbo.sysjobhistory jh 
               ON jh.job_id = j.job_id AND jh.step_id = 0 
               inner join msdb.dbo.syscategories sc
               on j.category_id = sc.category_id
               WHERE j.[name] like 'MB_ProcessWarehouse'
               order by jh.run_date desc, jh.run_time desc
               )

所以这个SPROC基本上做的是运行一些SQL来启动工作,然后检查它何时完成并检查它的状态。这项工作可能需要30分钟才能完成。

调用实际目标并启动作业,但msbuild任务因此错误而失败

SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding

我尝试了一些方法,例如在注册表中更改SQL的超时,但是60秒后仍然出现此错误。我有办法解决这个问题吗?或者有更好的方法来做我想做的事情。在部署以在较低环境中填充数据之后,我想将此SPROC作为构建过程的一部分启动。

1 个答案:

答案 0 :(得分:1)

ConnectionTimeout与此无关,您正在寻找CommandTimeouttask property

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="SqlExecute" AssemblyFile="C:\Users\Ilya.Kozhevnikov\Dropbox\MSBuildExtensionPack\Solutions\Main\BuildBinaries\MSBuild.ExtensionPack.dll" />

    <Target Name="Foo">
        <SqlExecute
            TaskAction="ExecuteRawReader"
            Sql="WAITFOR DELAY '00:00:31'"
            ConnectionString="Integrated Security=true"
            CommandTimeout="32"
        />
    </Target>
</Project>