webspecs / publican

The set of tools used to automate publishing in the WebSpecs project
MIT License
1 stars 1 forks source link

lib/tasks/purge-all is broken #22

Open renoirb opened 9 years ago

renoirb commented 9 years ago

I’ve had issues with the purge-all task. After some digging, its caused by superagent who makes request with 'Content-Encoding'.

Here is a refactor proposal to use the request package instead. I picked the same version as phantom uses.

diff --git c/lib/tasks/purge-all.js w/lib/tasks/purge-all.js
index 56cecd6..ed42258 100644
--- c/lib/tasks/purge-all.js
+++ w/lib/tasks/purge-all.js
@@ -1,16 +1,28 @@

-var sua = require("superagent");
+var request = require("request");

 // expects:
 //  purgeAllURL:    the URL to purge Fastly
 //  purgeAllKey:    the key for the Fastly service
 module.exports = function (ctx, cb) {
-    sua.post(ctx.purgeAllURL)
-        .set("Fastly-Key", ctx.purgeAllKey)
-        .set('Accept', 'application/json')
-        .end(function (err, res) {
-            if (!err && res.body && res.body.status === "ok") return cb();
-            cb(new Error(res.body ? res.body.status : "No response body for purge"));
-        })
-    ;
+
+    var options = {
+          url: ctx.purgeAllURL
+        , method: 'POST'
+        , headers: {
+                  'User-Agent': 'WebPlatformPublican/1'
+                , 'Fastly-Key': ctx.purgeAllKey
+        }
+    };
+
+    return request(options, function requestCallback(error, response, body) {
+        var info = JSON.parse(body);
+        if (!error && response.statusCode == 200) {
+            console.log('Purge worked', info);
+            return cb();
+        } else {
+            cb( new Error('Error purging, message ', info ) );
+        }
+    });
+
 };

In package.json;

+    "request": "2.42.0"

My concern about purge all is that its a very powerful thing to do. Maybe I should just adjust the lifetime of items to last a few minutes but set it so that it can remain "stale" for a long period.

This would do what we both want; don’t need to purge every now and then, AND keep the site up even when the servers underneath are down.