Skip to content

Commit

Permalink
chore: use no network during mvn compile (#191)
Browse files Browse the repository at this point in the history
Fixes #189
  • Loading branch information
mxschmitt committed Apr 4, 2021
1 parent 325be74 commit 753ebba
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
38 changes: 26 additions & 12 deletions internal/worker/files.go
Expand Up @@ -4,21 +4,23 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"sync"

"github.com/fsnotify/fsnotify"
)

type filesCollector struct {
dir string
done chan bool
watcher *fsnotify.Watcher
dir string
ignorePatterns []string
done chan bool
watcher *fsnotify.Watcher

filesMu sync.Mutex
files []string
}

func newFilesCollector(dir string) (*filesCollector, error) {
func newFilesCollector(dir string, ignorePatterns []string) (*filesCollector, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, fmt.Errorf("could not create new fs watcher: %w", err)
Expand All @@ -27,10 +29,11 @@ func newFilesCollector(dir string) (*filesCollector, error) {
return nil, fmt.Errorf("could not add dir to watcher: %w", err)
}
fw := &filesCollector{
done: make(chan bool),
watcher: watcher,
dir: dir,
files: []string{},
done: make(chan bool),
watcher: watcher,
dir: dir,
ignorePatterns: ignorePatterns,
files: []string{},
}
go fw.watch()
return fw, nil
Expand Down Expand Up @@ -69,6 +72,16 @@ func (fw *filesCollector) watch() {
}

func (fw *filesCollector) consumeCreateEvent(event fsnotify.Event) error {
for _, ignorePattern := range fw.ignorePatterns {
matched, err := filepath.Match(ignorePattern, event.Name)
if err != nil {
return fmt.Errorf("could not match pattern: %w", err)
}
if matched {
return nil
}
}

fi, err := os.Stat(event.Name)
if err != nil {
return fmt.Errorf("could not stat: %w", err)
Expand All @@ -77,10 +90,11 @@ func (fw *filesCollector) consumeCreateEvent(event fsnotify.Event) error {
if err := fw.watcher.Add(event.Name); err != nil {
return fmt.Errorf("could not add folder recursivelyt: %w", err)
}
} else {
fw.filesMu.Lock()
fw.files = append(fw.files, event.Name)
fw.filesMu.Unlock()
return nil
}

fw.filesMu.Lock()
fw.files = append(fw.files, event.Name)
fw.filesMu.Unlock()
return nil
}
3 changes: 2 additions & 1 deletion internal/worker/worker.go
Expand Up @@ -73,7 +73,7 @@ func (w *Worker) ExecCommand(name string, args ...string) error {
if err != nil {
return fmt.Errorf("could not command lookup path: %w", err)
}
collector, err := newFilesCollector(w.tmpDir)
collector, err := newFilesCollector(w.tmpDir, w.options.IgnoreFilePatterns)
if err != nil {
return fmt.Errorf("could not create file collector: %w", err)
}
Expand Down Expand Up @@ -191,6 +191,7 @@ type WorkerExectionOptions struct {
Handler executionHandler
ExecutionDirectory string
TransformOutput func(output string) string
IgnoreFilePatterns []string
}

func NewWorker(options *WorkerExectionOptions) *Worker {
Expand Down
4 changes: 1 addition & 3 deletions worker-java/Dockerfile
Expand Up @@ -25,9 +25,7 @@ RUN mkdir /home/pwuser/project/
COPY worker-java/pom.xml /home/pwuser/project/

RUN cd /home/pwuser/project/ && \
mvn install

COPY worker-java/settings.xml /home/pwuser/.m2/
mvn dependency:resolve-plugins dependency:go-offline

COPY --from=builder /app /app

Expand Down
3 changes: 2 additions & 1 deletion worker-java/main.go
Expand Up @@ -26,7 +26,7 @@ func handler(w *worker.Worker, code string) error {
if err := os.WriteFile(filepath.Join(basePath, fmt.Sprintf("%s.java", className)), []byte(code), 0644); err != nil {
return fmt.Errorf("could not write Java source files: %v", err)
}
return w.ExecCommand("mvn", "compile", "exec:java", "-q", "-D", "jdk.module.illegalAccess=deny", "-D", fmt.Sprintf("exec.mainClass=org.example.%s", className))
return w.ExecCommand("mvn", "compile", "exec:java", "-q", "--offline", "-D", fmt.Sprintf("exec.mainClass=org.example.%s", className))
}

const NEW_LINE_SEPARATOR = "\n"
Expand Down Expand Up @@ -61,5 +61,6 @@ func main() {
Handler: handler,
ExecutionDirectory: projectDir,
TransformOutput: transformOutput,
IgnoreFilePatterns: []string{filepath.Join(projectDir, "target", "**")},
}).Run()
}
5 changes: 5 additions & 0 deletions worker-java/pom.xml
Expand Up @@ -19,6 +19,11 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down

0 comments on commit 753ebba

Please sign in to comment.