@Override
  protected boolean processEvent(Kind<?> kind, final Path filename) throws IOException {
    metrics.getFilesystemEventsMeter().mark();

    if (!isS3MetadataFile(filename)) {
      return false;
    }

    runLock.lock();

    try {
      if (isStopped()) {
        LOG.warn("Driver is stopped, ignoring file watch event for {}", filename);
        return false;
      }

      final Path fullPath = configuration.getS3MetadataDirectory().resolve(filename);

      if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE)) {
        Optional<SingularityS3Uploader> found =
            Iterables.tryFind(
                metadataToUploader.values(),
                new Predicate<SingularityS3Uploader>() {
                  @Override
                  public boolean apply(SingularityS3Uploader input) {
                    return input.getMetadataPath().equals(fullPath);
                  }
                });

        LOG.trace("Found {} to match deleted path {}", found, filename);

        if (found.isPresent()) {
          expiring.add(found.get());
        }
      } else {
        return handleNewOrModifiedS3Metadata(fullPath);
      }

      return false;
    } finally {
      runLock.unlock();
    }
  }
Example #2
0
  @Override
  public void run() {
    while (running) {
      try {
        WatchKey key = watcher.take();
        sleep(100);
        for (WatchEvent<?> event : key.pollEvents()) {
          Kind<?> kind = event.kind();
          String dateiname = event.context().toString();
          int linie;
          LineReader lr;
          switch (kind.name()) {
            case "ENTRY_CREATE":
              // HTreff<X>PN<Y>.ctl
              // HTreff<X>PN<Y>.nrt
              // HTreff<X>MN<Y>.ctl
              // HTreff<X>MN<Y>.nrt
              if (!dateiname.startsWith("HTreff")) break;

              int endIndex = dateiname.indexOf('N') - 1;
              linie = Integer.parseUnsignedInt(dateiname.substring(6, endIndex));
              lr = getLineReaderByNumber(linie);
              if (lr != null) {
                File datei = new File(dateiname);
                if (dateiname.endsWith(".ctl")) {
                  FileInputStream reader = null;
                  try {
                    reader = new FileInputStream(dateiname);
                    List<String> lines = IOUtils.readLines(reader, Charset.forName("UTF-8"));
                    if (lines.size() > 0) lr.addTreffer(new Hit(lines.get(0)));
                  } catch (IOException e) {
                    e.printStackTrace();
                  } finally {
                    if (reader != null) IOUtils.closeQuietly(reader);
                  }
                }
                if (!datei.delete()) {
                  System.out.println(dateiname + " konnte nicht gelöscht werden.");
                }
              }
              break;
            case "ENTRY_DELETE":
              // HServ<X>.ctl
              if (dateiname.startsWith("HServ") && dateiname.endsWith(".ctl")) {
                linie = Integer.parseUnsignedInt(dateiname.substring(5, dateiname.length() - 4));
                lr = getLineReaderByNumber(linie);
                if (lr != null) lr.reenable();
              }
              break;
            default: // OVERFLOW
              continue;
          }
        }
        key.reset();
      } catch (InterruptedException | ClosedWatchServiceException e) {
        if (running) {
          e.printStackTrace();
        }
      }
    }
  }