precondition with variables

194 views
Skip to first unread message

bswift

unread,
Oct 29, 2011, 11:44:52 AM10/29/11
to psake-users
I'm running a .net project, and am doing some integration tests to the
database. I want to run mongo both on my CI server (teamcity) and
locally. The problem is that locally I have a database installed,
and on the server I want it to run in process, or for anyone checking
out the code to not have to install mongo.

What I want to wrap my tests with an initial "StartMongo" task and end
it with a "StopMongo" task. I'm trying to use preconditions for
this.

My first trial was to set a boolean property, and on the first "Init"
task, see if mongo is running, set that property and use that property
in the preconditions. It seems like preconditions are evaluated when
the script is loaded, if it's running off a property variable.

I can't use the code that I have here, because the "StopMongo"
precondition will ALWAYS return true.

Any ideas on how I can get this to work? As you see in the script
below, I have the precondition commented out on the StopMongo task to
output the variable that I want to use to see if mongo is running, it
always evaluates to false.

Thanks!


properties {
$root_dir = Resolve-Path .
$test_dir = "$root_dir\test\"
$lib_dir = "$root_dir\lib\"
$temp_dir = "$root_dir\temp"

$solution_name = "ToolRegistry"
$solution_file = "$solution_name.sln"

$mongoDb_dir = "$root_dir\temp\data\db\"
$mongoDbExe = "$root_dir\lib\MongoDbExe\bin\mongod.exe"
$mongoDbParams = "--dbpath $mongoDb_dir"

$killMongoProcess = $false

}

#move to functions file
function Get-FrameworkDirectory(){
$
([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())
}

function CheckForRunningMongo{
((Get-Process "mongod" -ea SilentlyContinue)) -eq $null
}

task default -depends StartMongo, StopMongo

task StartMongo -Depends Init -precondition { return
CheckForRunningMongo} {
write-host "starting mongo $mongoDbExe"
Start-Process -FilePath $mongoDbExe -ArgumentList $mongoDbParams
write-host -foregroundcolor green "Mongo Process id :" $mongoPid.Id
$killMongoProcess = $true
}

task StopMongo {# -precondition {return CheckForRunningMongo} {
write-host -foregroundcolor green "Kill Mongo Process?
$killMongoProcess "
Stop-Process -processname mongod
}










Jorge Matos

unread,
Oct 30, 2011, 11:59:07 PM10/30/11
to psake...@googlegroups.com
hmmm pre-conditions are evaluated before a task is executed, not when the script is loaded.

I don't see anything obviously wrong with your script but try this - replace the calls to the "CheckForRunningMongo" function with the code [((Get-Process "mongod" -ea SilentlyContinue)) -eq $null}] or try adding "()" to the end of the function call ["CheckForRunningMongo()"]

I think your precondition code is always returning true because you are not invoking the function and returning a result but simply returning a reference to the function.

But I could be wrong. :)

Sent from my iPhone

> --
> You received this message because you are subscribed to the Google Groups "psake-users" group.
> To post to this group, send email to psake...@googlegroups.com.
> To unsubscribe from this group, send email to psake-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/psake-users?hl=en.
>

bswift

unread,
Oct 31, 2011, 12:36:20 AM10/31/11
to psake-users
I figured it out.

Instead of setting the $killMongoProcess variable in my "Init" task,
I call it right from within the properties section.

$killMongoProcess = CheckForRunningMongo


Also --> if I instead use your suggestion: CheckForRunningMongo() -
it complains about the parenthesis because it's looking to parse some
parameters.


Thanks for the help though.




On Oct 30, 9:59 pm, Jorge Matos <matos.jo...@gmail.com> wrote:
> hmmm pre-conditions are evaluated before a task is executed, not when the script is loaded.
>
> I don't see anything obviously wrong with your script but try this - replace the calls to the "CheckForRunningMongo" function with the code [((Get-Process "mongod" -ea SilentlyContinue)) -eq $null}] or try adding "()" to the end of the function call ["CheckForRunningMongo()"]
>
> I think your precondition code is always returning true because you are not invoking the function and returning a result but simply returning a reference to the function.
>
> But I could be wrong. :)
>
> Sent from my iPhone
>
Reply all
Reply to author
Forward
0 new messages