diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json deleted file mode 100644 index c796291..0000000 --- a/Godeps/Godeps.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "ImportPath": "directqueue", - "GoVersion": "go1.6", - "Deps": [ - { - "ImportPath": "github.com/codegangsta/cli", - "Comment": "1.2.0-224-gaca5b04", - "Rev": "aca5b047ed14d17224157c3434ea93bf6cdaadee" - }, - { - "ImportPath": "github.com/go-sql-driver/mysql", - "Comment": "v1.2-188-g66312f7", - "Rev": "66312f7fe2678aa0f5ec770f96702f4c4ec5aa8e" - }, - { - "ImportPath": "gopkg.in/DATA-DOG/go-sqlmock.v1", - "Comment": "v1.1.3", - "Rev": "9958e5c69de03e97ec215b23f6fcae1f600c3fb6" - } - ] -} diff --git a/Godeps/Readme b/Godeps/Readme deleted file mode 100644 index 4cdaa53..0000000 --- a/Godeps/Readme +++ /dev/null @@ -1,5 +0,0 @@ -This directory tree is generated automatically by godep. - -Please do not edit. - -See https://github.com/tools/godep for more information. diff --git a/README.md b/README.md index 11ed6b5..0bacfe1 100644 --- a/README.md +++ b/README.md @@ -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: ``` @@ -21,7 +21,8 @@ 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. | @@ -29,13 +30,14 @@ You can also wrap it in a upstart/init.d script, examples on that will come soon | --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 ``` diff --git a/directqueue.go b/directqueue.go index 2dd711f..ec2cc68 100644 --- a/directqueue.go +++ b/directqueue.go @@ -31,6 +31,7 @@ type Config struct { Site string URI string Password string + Port string HandleQueues []string SkipQueues []string QueueWorkers map[string]int @@ -48,6 +49,7 @@ func main() { var ( console string + drush string site string uri string skipQueues string @@ -55,15 +57,22 @@ func main() { 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: "", @@ -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.") @@ -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") @@ -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() } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2574e93 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ca59b12 --- /dev/null +++ b/go.sum @@ -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=