Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions Godeps/Godeps.json

This file was deleted.

5 changes: 0 additions & 5 deletions Godeps/Readme

This file was deleted.

12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ Direct Queue worker for Drupal 8 in Go

This Go file watches the Drupal Queue and processes items as soon as it notices them or there is space in the queue.

Drupal Console is required for getting the database connection and processing queue items.
[Drush](https://www.drush.org/) is required for getting the database connection and processing queue items.

The Drupal module [direct_queue](https://www.drupal.org/project/direct_queue) is required to process items.

## Running
Download any of the releases that matches your platform. You don't need any dependencies to run the program (besides being able to run Drupal Console). The workers can also run on a separate machine as long as the Drupal codebase, Drupal Console and database connection are available.
Download any of the releases that matches your platform. You don't need any dependencies to run the program (besides having [Drush](https://www.drush.org/) and [direct_queue](https://www.drupal.org/project/direct_queue) inside your Drupal project). The workers can also run on a separate machine as long as the Drupal codebase, Drush and database connection are available.

You can run the program by executing it on the shell:
```
Expand All @@ -21,21 +21,23 @@ You can also wrap it in a upstart/init.d script, examples on that will come soon
## Options
| Parameter | Default Value | Description |
| ---------------------- | -------------- | ------------ |
| --console | "console" | Path to Drupal Console. Full path or just binary to search in $PATH. |
| --drush | "drush" | Path to the binary of Drush. Full path or just binary to search in $PATH. (either use console or drush, console only exists for backwards compatibility). |
| --console | "drush" | Path to the binary of Drush. Full path or just binary to search in $PATH. |
| --site | "" | Full path to the Drupal root. |
| --uri | "" | URI to pass to Drupal, useful when you are generating links. |
| --skip-queues | "" | Comma separated list of queues to skip, can't be used together with handle-queues. |
| --handle-queues | "" | Comma separated list of queues to handle, can't be used together with skip-queues. |
| --queue-workers | "" | Amount of workers to use per queue, format: "publish_scheduler:1,entity_update:4". |
| --default-worker-count | Amount of CPUs-1 | Default amount of workers, default value is amount of CPUs - 1. |
| --db-password | DB Password | Password for the database if it can't be read by Drupal Console. |
| --db-password | "3306" | Set the database port. |

## Compiling
Make sure you are able to run a Go 1.6 (though 1.4/1.5 should also work) environment and that you have [Godeps](https://github.com/tools/godep) available.
Make sure you are able to run a Go 1.6 (though 1.4/1.5 should also work) environment.
```
git clone https://github.com/VDMi/DirectQueueGo.git
cd DirectQueueGo
godep restore
go install
go build
./DirectQueueGo help
```
Expand Down
58 changes: 38 additions & 20 deletions directqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
Site string
URI string
Password string
Port string
HandleQueues []string
SkipQueues []string
QueueWorkers map[string]int
Expand All @@ -48,22 +49,30 @@ func main() {

var (
console string
drush string
site string
uri string
skipQueues string
handleQueues string
queueWorkers string
defaultWorkerCount int
password string
port string
)

app.Flags = []cli.Flag{
cli.StringFlag{
Name: "console",
Value: "console",
Usage: "Binary of Drupal Console. Full path or just binary to search in $PATH.",
Value: "drush",
Usage: "Binary of Drush. Full path or just binary to search in $PATH. (either use console or drush, console only exists for backwards compatibility)",
Destination: &console,
},
cli.StringFlag{
Name: "drush",
Value: "drush",
Usage: "Binary of Drush. Full path or just binary to search in $PATH.",
Destination: &drush,
},
cli.StringFlag{
Name: "site",
Value: "",
Expand Down Expand Up @@ -106,21 +115,29 @@ func main() {
Usage: "Overwrite the db password.",
Destination: &password,
},
cli.StringFlag{
Name: "db-port",
Value: "3306",
Usage: "Set the database port.",
Destination: &port,
},
}

app.Name = "DirectQueue"
app.Usage = "Directly handles Queue items by using a Go daemon."

app.Action = func(c *cli.Context) {
if drush != "" {
console = drush
}
config := Config{
Console: console,
Site: site,
URI: uri,
Password: password,
Port: port,
DefaultWorkerCount: defaultWorkerCount,
Context: c,
}

// Make sure we have the required variables.
if site == "" {
log.Fatal("Site path can't be empty.")
Expand Down Expand Up @@ -361,47 +378,48 @@ func queueJobHandler(queue Queue, config Config, worker int) {
func getDBConnectString(config Config) (db_connect string, err error) {

// Execute the database:connect command in Drupal Console.
bytes, err := executeCommand(config, []string{"database:connect"})
bytes, err := executeCommand(config, []string{"sql-connect"})
if err != nil {
return "", err
}

output := string(bytes)
// Get output and replace -A with nothing
output := strings.Replace(string(bytes), "-A", "", 1)

// Try to parse database details from Drupal Console output.
re := regexp.MustCompile("(?ms)--database=(.*)--user=(.*)--password=(.*)--host=(.*)--port=(0|[1-9][0-9]*|)")
log.Printf("T" + output)
re := regexp.MustCompile("(?ms)--user=(.*)--password=(.*)--database=(.*)--host=(.*)(?:--port=(0|[1-9][0-9]*|))?")
matches := re.FindStringSubmatch(output)
cutset := " \n\r"

// See if we have enough matches.
if len(matches) < 6 {
if len(matches) < 5 {
err = errors.New("Could not find connection details.")
return "", err
}

// Add default port of MySQL.
if strings.Trim(matches[5], cutset) == "" {
matches[5] = "3306"
matches[5] = config.Port
}

if config.Password != "" {
matches[3] = config.Password
matches[2] = config.Password
}

// Put together the connection details.
// matches[1] = --database
// matches[2] = --user
// matches[3] = --password
// matches[1] = --user
// matches[2] = --password
// matches[3] = --database
// matches[4] = --host
// matches[5] = --port (or default)
db_connect = strings.Trim(matches[2], cutset) + ":" + strings.Trim(matches[3], cutset) + "@tcp(" + strings.Trim(matches[4], cutset) + ":" + strings.Trim(matches[5], cutset) + ")/" + strings.Trim(matches[1], cutset)

db_connect = strings.Trim(matches[1], cutset) + ":" + strings.Trim(matches[2], cutset) + "@tcp(" + strings.Trim(matches[4], cutset) + ":" + strings.Trim(matches[5], cutset) + ")/" + strings.Trim(matches[3], cutset)

return db_connect, nil
}

// Function to execute Drupal Console commands.
func executeCommand(config Config, args []string) ([]byte, error) {

// We do not want colors.
args = append(args, "--no-ansi")

Expand All @@ -414,9 +432,9 @@ func executeCommand(config Config, args []string) ([]byte, error) {
}

cmd := exec.Command(config.Console, args...)
// Make sure our site dir is the current working directory.
if config.Site != "" {
cmd.Dir = config.Site
}
// Make sure our site dir is the current working directory.
if config.Site != "" {
cmd.Dir = config.Site
}
return cmd.Output()
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module directqueue

go 1.19

require (
github.com/codegangsta/cli v1.13.1-0.20160307041229-aca5b047ed14
github.com/go-sql-driver/mysql v1.2.1-0.20160310222842-66312f7fe267
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.1.3
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/codegangsta/cli v1.13.1-0.20160307041229-aca5b047ed14 h1:oO2Sra3Njr2LAo17zT68PdIxb2dQE41I6+5MhWgsQJE=
github.com/codegangsta/cli v1.13.1-0.20160307041229-aca5b047ed14/go.mod h1:/qJNoX69yVSKu5o4jLyXAENLRyk1uhi7zkbQ3slBdOA=
github.com/go-sql-driver/mysql v1.2.1-0.20160310222842-66312f7fe267 h1:W7mv/YO8jTTEXpNS0wf+SLbzZRmyXlyZD9OArEm0HUk=
github.com/go-sql-driver/mysql v1.2.1-0.20160310222842-66312f7fe267/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.1.3 h1:9g2wYVtfupXhTOPXHLHzLrT8NdFOte5QsG7mV3rMM2k=
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.1.3/go.mod h1:OdE7CF6DbADk7lN8LIKRzRJTTZXIjtWgA5THM5lhBAw=