Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cdn
taskman-lite
Commits
9820962a
Commit
9820962a
authored
9 years ago
by
Simon Rettberg
Browse files
Options
Download
Email Patches
Plain Diff
Switch to semaphore for signalling the mainloop to check for work
parent
fd3c703e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
api/src/main/java/org/openslx/taskmanager/api/AbstractTask.java
+1
-0
...c/main/java/org/openslx/taskmanager/api/AbstractTask.java
api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java
+17
-10
...n/java/org/openslx/taskmanager/api/SystemCommandTask.java
daemon/src/main/java/org/openslx/taskmanager/main/Taskmanager.java
+6
-18
...c/main/java/org/openslx/taskmanager/main/Taskmanager.java
with
24 additions
and
28 deletions
+24
-28
api/src/main/java/org/openslx/taskmanager/api/AbstractTask.java
+
1
-
0
View file @
9820962a
...
...
@@ -299,6 +299,7 @@ public abstract class AbstractTask implements Runnable
}
else
{
this
.
status
.
statusCode
=
StatusCode
.
TASK_ERROR
;
}
LOG
.
debug
(
"Finished task "
+
this
.
getClass
().
getSimpleName
()
+
": "
+
this
.
status
.
statusCode
.
toString
()
+
" ("
+
this
.
id
+
")"
);
if
(
this
.
finishCallback
!=
null
)
this
.
finishCallback
.
taskFinished
();
}
...
...
This diff is collapsed.
Click to expand it.
api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java
+
17
-
10
View file @
9820962a
...
...
@@ -14,9 +14,8 @@ public abstract class SystemCommandTask extends AbstractTask
private
static
final
Logger
log
=
Logger
.
getLogger
(
SystemCommandTask
.
class
);
private
String
[]
command
=
null
;
private
Process
process
=
null
;
private
Process
process
=
null
;
@Override
protected
final
boolean
execute
()
...
...
@@ -50,7 +49,7 @@ public abstract class SystemCommandTask extends AbstractTask
processStdOut
(
line
);
}
}
}
catch
(
IO
Exception
e
)
{
}
catch
(
Exception
e
)
{
}
}
}
);
...
...
@@ -67,7 +66,7 @@ public abstract class SystemCommandTask extends AbstractTask
processStdErr
(
line
);
}
}
}
catch
(
IO
Exception
e
)
{
}
catch
(
Exception
e
)
{
}
}
}
);
...
...
@@ -76,9 +75,17 @@ public abstract class SystemCommandTask extends AbstractTask
stderr
.
start
();
// Wait for everything
stdout
.
join
();
stderr
.
join
();
process
.
waitFor
();
try
{
process
.
getErrorStream
().
close
();
}
catch
(
Throwable
t
)
{
}
try
{
process
.
getOutputStream
().
close
();
}
catch
(
Throwable
t
)
{
}
stdout
.
join
(
2000
);
stderr
.
join
(
2000
);
return
processEnded
(
process
.
exitValue
()
);
...
...
@@ -98,14 +105,14 @@ public abstract class SystemCommandTask extends AbstractTask
process
.
destroy
();
}
}
/**
* Write data to the process's stdin.
*
* @param data stuff to write
* @return success or failure mapped to a boolean in a really complicated way
*/
protected
final
boolean
toStdIn
(
byte
[]
data
)
protected
final
boolean
toStdIn
(
byte
[]
data
)
{
try
{
process
.
getOutputStream
().
write
(
data
);
...
...
@@ -122,7 +129,7 @@ public abstract class SystemCommandTask extends AbstractTask
* @param text stuff to write
* @return success or failure mapped to a boolean in a really complicated way
*/
protected
final
boolean
toStdIn
(
String
text
)
protected
final
boolean
toStdIn
(
String
text
)
{
return
toStdIn
(
text
.
getBytes
(
StandardCharsets
.
UTF_8
)
);
}
...
...
@@ -147,7 +154,7 @@ public abstract class SystemCommandTask extends AbstractTask
* Called when the process has finished running
*
* @param exitCode the process' exit code
* @return
* @return
*/
protected
abstract
boolean
processEnded
(
int
exitCode
);
...
...
This diff is collapsed.
Click to expand it.
daemon/src/main/java/org/openslx/taskmanager/main/Taskmanager.java
+
6
-
18
View file @
9820962a
...
...
@@ -5,11 +5,9 @@ import java.util.Map;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.RejectedExecutionException
;
import
java.util.concurrent.Semaphore
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.locks.Condition
;
import
java.util.concurrent.locks.Lock
;
import
java.util.concurrent.locks.ReentrantLock
;
import
org.apache.log4j.Logger
;
import
org.openslx.taskmanager.Global
;
...
...
@@ -46,8 +44,7 @@ public class Taskmanager implements FinishCallback, Runnable
*/
private
final
Map
<
String
,
AbstractTask
>
instances
=
new
ConcurrentHashMap
<>();
private
final
Lock
workLock
=
new
ReentrantLock
();
private
final
Condition
doCheckForWork
=
workLock
.
newCondition
();
private
final
Semaphore
doCheckForWork
=
new
Semaphore
(
0
);
/*
* Static methods
...
...
@@ -142,12 +139,7 @@ public class Taskmanager implements FinishCallback, Runnable
*/
protected
void
checkForWork
()
{
workLock
.
lock
();
try
{
doCheckForWork
.
signalAll
();
}
finally
{
workLock
.
unlock
();
}
doCheckForWork
.
release
();
}
@Override
...
...
@@ -161,12 +153,8 @@ public class Taskmanager implements FinishCallback, Runnable
{
try
{
while
(
!
Global
.
doShutdown
)
{
workLock
.
lock
();
try
{
doCheckForWork
.
await
(
1
,
TimeUnit
.
MINUTES
);
}
finally
{
workLock
.
unlock
();
}
doCheckForWork
.
tryAcquire
(
1
,
TimeUnit
.
MINUTES
);
doCheckForWork
.
drainPermits
();
try
{
for
(
Iterator
<
AbstractTask
>
it
=
instances
.
values
().
iterator
();
it
.
hasNext
();
)
{
AbstractTask
task
=
it
.
next
();
...
...
@@ -176,8 +164,8 @@ public class Taskmanager implements FinishCallback, Runnable
continue
;
}
if
(
task
.
canStart
()
)
{
threadPool
.
execute
(
task
);
log
.
debug
(
"Started Task "
+
task
.
getClass
().
getSimpleName
()
+
" ("
+
task
.
getId
()
+
")"
);
threadPool
.
execute
(
task
);
}
}
}
catch
(
RejectedExecutionException
e
)
{
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment