Skip to content

Commit

Permalink
[jpm] Improved service handling
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Kriens <peter.kriens@aqute.biz>
  • Loading branch information
pkriens committed Jun 18, 2015
1 parent b0cf323 commit 0579d17
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public void run() {
try {
Service service = getService(sd.name);
reporter.trace("Starting " + service);
String result = service.start();
String result = service.start(true);
if (result != null)
reporter.error("Started error " + result);
else
Expand Down
38 changes: 27 additions & 11 deletions biz.aQute.jpm/src/aQute/jpm/lib/Service.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package aQute.jpm.lib;

import static aQute.lib.io.IO.*;
import static aQute.lib.io.IO.collect;

import java.io.*;
import java.net.*;
import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;

import aQute.lib.io.*;
import aQute.lib.io.IO;
import aQute.lib.io.IOConstants;

public class Service {
static final int BUFFER_SIZE = IOConstants.PAGE_SIZE * 16;
static final int BUFFER_SIZE = IOConstants.PAGE_SIZE * 16;

final ServiceData data;
final JustAnotherPackageManager jpm;
Expand All @@ -21,13 +26,23 @@ public class Service {
}

public String start() throws Exception {
if (lock.exists())
return "Already running";

return start(false);
}

public String start(boolean force) throws Exception {
if (lock.exists()) {
if (!force)
return "Already running";

lock.delete();
Thread.sleep(2000);
}

if (lock.createNewFile()) {

jpm.platform.chown(data.user, false, lock);
try {
Thread.sleep(1000);
int result = jpm.platform.launchService(data);
if (result != 0)
return "Could not launch service " + data.name + " return value " + result;
Expand All @@ -37,7 +52,7 @@ public String start() throws Exception {
Thread.sleep(100);
if (getPort() != -1)
return null;

}
lock.delete();
return "Could not establish a link to the service, likely failed to start";
Expand Down Expand Up @@ -112,7 +127,7 @@ int getPort() {
return Integer.parseInt(parts[0]);
}
catch (Exception e) {
//e.printStackTrace();
// e.printStackTrace();
return -1;
}
}
Expand Down Expand Up @@ -171,4 +186,5 @@ public void clear() {
IO.delete(new File(data.work));
new File(data.work).mkdir();
}

}
8 changes: 7 additions & 1 deletion biz.aQute.jpm/src/aQute/jpm/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -540,25 +540,31 @@ public void _service(ServiceOptions opts) throws Exception {
}

if (opts.create() != null) {
trace("create service");
if (s != null) {
error("Service already exists, cannot be created: %s. Update or remove it first", name);
return;
}

ArtifactData target = jpm.getCandidate(opts.create());
if (target == null)
if (target == null) {
error("Cannot find candidate for coordinates", opts.create());
return;
}

ServiceData data = new ServiceData();
CommandData cmd = jpm.parseCommandData(target);
for (Field f : cmd.getClass().getFields()) {
f.set(data, f.get(cmd));
}

trace("service data %s", cmd);

data.name = name;

updateServiceData(data, opts);

trace("update service data");
String result = jpm.createService(data, false);
if (result != null)
error("Create service failed: %s", result);
Expand Down
32 changes: 21 additions & 11 deletions biz.aQute.jpm/src/aQute/jpm/platform/Unix.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package aQute.jpm.platform;

import java.io.*;
import java.util.*;
import java.util.regex.*;

import aQute.jpm.lib.*;
import aQute.lib.getopt.*;
import aQute.lib.io.*;
import aQute.lib.strings.*;
import aQute.libg.command.*;
import aQute.libg.glob.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formatter;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import aQute.jpm.lib.CommandData;
import aQute.jpm.lib.ServiceData;
import aQute.lib.getopt.Arguments;
import aQute.lib.getopt.Description;
import aQute.lib.getopt.Options;
import aQute.lib.io.IO;
import aQute.lib.strings.Strings;
import aQute.libg.command.Command;
import aQute.libg.glob.Glob;

public abstract class Unix extends Platform {

Expand Down Expand Up @@ -110,7 +119,6 @@ public int launchService(ServiceData data) throws Exception {
String DAEMON = "\n### JPM BEGIN ###\n" + "jpm daemon >" + JPM_GLOBAL + "/daemon.log 2>>"
+ JPM_GLOBAL + "/daemon.log &\n### JPM END ###\n";
static Pattern DAEMON_PATTERN = Pattern.compile("\n### JPM BEGIN ###\n.*\n### JPM END ###\n", Pattern.MULTILINE);

@Override
public void installDaemon(boolean user) throws Exception {
if (user)
Expand All @@ -127,6 +135,8 @@ public void installDaemon(boolean user) throws Exception {
if (s.contains(DAEMON))
return;

// TODO handle exit 0 at end of file

s += DAEMON;
IO.store(s, rclocal);

Expand Down

0 comments on commit 0579d17

Please sign in to comment.