Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with rsync Deployments #20

Open
azharc opened this issue Nov 18, 2021 · 0 comments
Open

Working with rsync Deployments #20

azharc opened this issue Nov 18, 2021 · 0 comments

Comments

@azharc
Copy link

azharc commented Nov 18, 2021

Hello! Thank you for this awesome plugin!

I have written a simple rsync deployment script, and used your plugin successfully, but had to make one (possibly stupid) modification.

Here's how the deployment works.

A route is created to trigger a deployment

It returns a JSON response

'routes' => [
		[
			'pattern' => 'deploy-secret-url',
			'method' => 'POST|GET',
			'action' => function() {
				return Response::json(array('status' => 'deployment in progress'));
			}
		],
	],

A hook runs after the route

It uses rsync to deploy the website

	'hooks' => [
		'route:after' => function($route, $path, $method, $result) {
			if ($path == "deploy-secret-url") {
                               // Simplified for brevity
				$rsync->sync($origin, $target);

				if ($rsync->getExitCode() == 0) {
					$request = new Remote('example.com/webhook/deploy/success', array(
						'method' => 'POST',
					));
				} else {
					$request = new Remote('example.com/webhook/deploy/error', array(
						'method' => 'POST',
					));
				}
			}
		}
	]

The problem I was having is that these two things happen exactly at the same time in this order:

  • the post request to the success route is sent
  • the response to the original request is sent (in response to 'deploy-secret-url')

Because of this the field receives a "success" state and a "progress" state at the same time, but since the progress state comes just after the success state, the success state is overwritten, and the field never reaches the success state.

The way I fixed this was by adding this code to the setState function in WebhookField.php after line 100:

        if ($status == 'progress' && time() - $cache->modified($hookName) < 1000) {
            return 'progress status ignored';
        }

Which ignores the progress state that travels right after/with the success state given the way this code is executing.

There's probably a far more elegant solution out there (would love to learn – I didn't want to introduce queueing or async curl requests) but thought I'd post this in case it is helpful for anyone.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant