Laravel Flysystem & Rackspace CRON uploads fail with 401 while on-demand uploads work
<p>Our system generates various invoices every hour and uploads those to the cloud. Also it is possible to create the invoice on demand by clicking a button on our frontend.</p>

<p>When manually requesting to create the said invoice then it never fails to upload.
As for the cron generated invoices after some time all uploads fail with:</p>

<pre><code>Client error response
[status code] 401
[reason phrase] Unauthorized
[url] https://storage101.dfw1.clouddrive.com/v1/MossoCloudFS_930575/ &lt;...&gt; .pdf" @ Guzzle\Http\Exception\BadResponseException-&gt;factory 
</code></pre>

<p>Which should mean that the token has expired which it probably is. Rackspace tokens last for 24 hours but the Laravel's Storage should automatically refresh the token.</p>

<hr>

<p>Now here for some fun facts:</p>

<p>1) Every time our code is deployed with Capistrano the token seems to get refreshed and cron uploads work again for some time. Important thing to notice here is that every deploy creates a new folder similar to this <code>/releases/201605190925</code> pulls the code, installs dependencies from scratch and if all goes well then symlinks this folder with the one Apache is showing.</p>

<p>2) Laravel jobs get handled on a different process than www-data</p>

<p>3) It's hard to track down if after deploy the upload works more than 24 hours or not. I suspect that sometimes it works more than that. But it's hard to track down since not every hour there are invoices that need to be generated. There are more developers who deploy than just me etc.</p>

<p>4) When cron fails and I get the failing notification I can immediately go and successfully generate the invoice. After that the cron still keeps failing. So it seems like these two instances have different tokens stored somewhere.</p>

<p>5) Cache cleaning with <code>php artisan cache:clear</code> doesn't seem to have any impact</p>

<p>6) Probably have tried restarting Apache service but without results</p>

<p>Since it has been going on for some time now I have tested various things and even contacted Rackspace at one point but they couldn't find anything odd from their end... Reminded me simply to catch the 401 error, update the token and try again. But Laravel and Flysystem should handle it somewhere by itself</p>

<p>Since nobody else seems to be having similar problems with Flysystem nor Laravel nor Rackspace I suspect that it's some kind of unique problem with the cron process. I was simply hoping that soon we have a refactored version of our system ready and the problem simply goes away. As for now it's still in development and might take another month.</p>

<hr>

<p>Don't think it's related to code but anyway here's the upload line:</p>

<pre><code>Storage::put($folder . '/' . $filename, file_get_contents($filePath));
</code></pre>

<p>Here's our config:</p>

<pre><code>'default' =&gt; 'rackspace',

'disks' =&gt; [
    'rackspace' =&gt; [
        'driver'    =&gt; 'rackspace',
        'username'  =&gt; ' ... ',
        'key'       =&gt; ' ... ',
        'container' =&gt; ' ... ',
        'endpoint'  =&gt; 'https://identity.api.rackspacecloud.com/v2.0/',
        'region'    =&gt; 'DFW',
    ]
]
</code></pre>

<hr>

<p>Any thoughts on the matter are appreciated.</p>