Start-Job Problems - powershell-2.0

Why this code not works ?
function teste
{
begin
{
function lala {
while ($true) {
"JJJJ" | Out-File c:\Testes\teste.txt -Append
}
}
}
process {
Start-Job -ScriptBlock {lala}
}
}

My best guess is scoping. When Start-Job runs your script block, it runs it in a different context -- one where "lala" is not defined. However, if you were to rephrase your code like so:
function Run-As-Background-Job
{
begin
{
$appendToFile = {
while ($true) {
"JJJJ" | Out-File c:\Testes\teste.txt -Append
}
}
}
process {
Start-Job -ScriptBlock $appendToFile
}
}
the background job wouldn't try to invoke a name that isn't defined -- instead, the entire script block would be passed to it and things should work.
Note, that I recommend you test without the while loop like I did, because that's going to fill up your disk rather quickly.
Also, please aim for more meaningful function and variable names when posting code. :-)

Related

Sleep inside Future in Scala.js

Is it possible to sleep inside a Future in Scala.js ?
Something like:
Future {
Thread.sleep(1000)
println("ready")
}
If I try this then I get an exception saying that sleep method does not exist.
It seems like that it is possible to sleep in JS : What is the JavaScript version of sleep()? even though it is not possible to block.
You cannot really pause in the middle of the future body, but you can register your future as a followup to a "delay" Future, which you can define as:
def delay(milliseconds: Int): Future[Unit] = {
val p = Promise[Unit]()
js.timers.setTimeout(milliseconds) {
p.success(())
}
p.future
}
and which you can then use as:
val readyLater = for {
delayed <- delay(1000)
} yield {
println("ready")
}

Passing flag variable to go program causing strange output

sergiotapia at Macbook-Air in ~/Work/go/src/github.com/sergiotapia/gophers on master [!]
$ go build && go install && gophers -github_url=https://github.com/search?utf8=%E2%9C%93&q=location%3A%22San+Fransisco%22+location%3ACA+followers%3A%3E100&type=Users&ref=advsearch&l=
[1] 51873
[2] 51874
[3] 51875
[4] 51877
[2] Done q=location%3A%22San+Fransisco%22+location%3ACA+followers%3A%3E100
[3] Done type=Users
[4]+ Done ref=advsearch
I'm trying to use the long github url as a parameter in my code for Gophers. It works fine for all other url types such as organisations or stargazers. However when I try to use the search results page I get the strange output above.
https://github.com/search?utf8=%E2%9C%93&q=location%3A%22San+Fransisco%22+location%3ACA+followers%3A%3E100&type=Users&ref=advsearch&l=
package main
import (
"flag"
"log"
"strings"
"github.com/PuerkitoBio/goquery"
)
type user struct {
name string
email string
url string
username string
}
func main() {
url := flag.String("github_url", "", "github url you want to scrape")
flag.Parse()
githubURL := *url
doc, err := goquery.NewDocument(githubURL)
if err != nil {
log.Fatal(err)
}
if strings.Contains(githubURL, "/orgs/") {
scrapeOrganization(doc, githubURL)
} else if strings.Contains(githubURL, "/search?") {
scrapeSearch(doc, githubURL)
} else if strings.Contains(githubURL, "/stargazers") {
scrapeStarGazers(doc, githubURL)
} else {
scrapeProfile(doc)
}
}
It's a bash command line (or whatever the mac uses). & and ? are shell metacharacters that you MUST escape. The shell has absolutely no idea what a URL is, nor should it ever have to.
go 'http://....'
^-----------^
Adding quotes will prevent the shell from parsing the metacharacters. The alternative is to manually escape each and ever metachar yourself:
go http://example.com/script.php\?foo=bar\&baz=qux
^--------^
which quickly gets tedious, and error prone.

Create & write a file which is having 'execute' permission?

I'm writing a bash script with dart.
Below code create a file. but that file doesn't have 'execute' permission.
so I'm not able to execute by doing ./ex.sh.
new File('ex.sh').writeAsStringSync(script_str);
Perhaps, I need to set FileStat to file. but i'm not able to find any APIs.
Haven't tried it but what if you try:
new File('ex.sh').writeAsString(script_str).then((final File file) {
return file.stat().then((final FileStat stat) => stat.mode = 777);
});
It seems this function is not yet implemented.
See code.google.com/p/dart/issues/detail?id=15078
As workaround, just made a utility function to run chmod command.
void _runBashCommandSync(GrinderContext context, String command, {String cwd, bool log: true}) {
context.log(command);
ProcessResult result =
Process.runSync('/bin/bash', ['-c', command], workingDirectory: cwd);
if (!log) return;
if (result.stdout.isNotEmpty) {
context.log(result.stdout);
}
if (result.stderr.isNotEmpty) {
context.log(result.stderr);
}
if (result.exitCode > 0) {
context.fail("exit code ${result.exitCode}");
}
}
_runBashCommandSync(context, 'chmod 777 ex.sh');

Disable blocking in phpseclib/SSH2?

I'm running some commands on multiple servers and I want them to all run concurrently,
foreach($clust['hosts'] as $hostStr) {
list($host, $port) = Str::rsplit($hostStr,':',2,22);
$ssh = new \Net_SSH2($host, $port);
if(!$ssh->login($username,$key)) {
throw new \Exception("Could not connect to $username#$host:$port");
}
$connections[] = $ssh;
}
foreach($connections as $i=>$ssh) {
$ssh->exec('cd /path/to/my/project && hg up', function($str) {
echo $str;
});
echo "right after ls $i\n";
}
But this always runs sequentially. Can I tell Net_SSH2 to be non-blocking?
One thing you could probably do is to use $ssh->setTimeout(1) or $ssh->setTimeout(0.5) or something.
You could also probably do $ssh->write(...) and just not do $ssh->read().

Stop code when a condition is true

I am trying to build a safety that will check for a condition that will either be true or false. This will be called multiple times through out a long bit of code. if the condition is true it will cause the rest of the code to stop. I cant seem to figure it out. Can someone point me in the right direction? By the way Exit will not work as it will close the whole program that I use.
proc _CheckEsc {} {
if {condition is true} {
return
}
return
}
proc testType {} {
set TestResult 0
while {$TestResult < 10} {
_CheckEsc;
incr TestResult
}
return;
}
You can make _CheckEsc stop it's caller by using some of the more advanced features of return. In particular, we can use it to make _CheckEsc act itself like a break or a return.
This mechanism is very much like throwing an exception in other languages (and in fact you can regard Tcl as having special exception classes for return, break and continue, except things are rather more complicated than that under the covers).
Making the caller's loop stop
proc _CheckEsc {} {
if {condition is true} {
return -code break
}
}
Making the caller return
proc _CheckEsc {} {
if {condition is true} {
return -level 2
# Or, if you want to return a value from the caller:
### return -level 2 "the value to return"
}
}
Note that the -level option isn't supported in Tcl 8.4 and before; that limits what you can do with it, but your use case works provided you do this instead:
proc _CheckEsc {} {
if {condition is true} {
return -code return
# Or, if you want to return a value from the caller:
### return -code return "the value to return"
}
}
Will something like this works for you?
proc _CheckEsc {} {
return {condition is true}; # I don't know what you have here
}
proc testType {} {
set TestResult 0
while {_CheckEsc && $TestResult < 10} {
incr TestResult
}
}
You can help us out by being more specific of what _CheckEsc does.

Resources