repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import type { Sandbox } from '@cloudflare/sandbox'; |
| #2 | import type { MoltbotEnv } from '../types'; |
| #3 | import { R2_MOUNT_PATH, R2_BUCKET_NAME } from '../config'; |
| #4 | |
| #5 | /** |
| #6 | * Check if R2 is already mounted by looking at the mount table |
| #7 | */ |
| #8 | async function isR2Mounted(sandbox: Sandbox): Promise<boolean> { |
| #9 | try { |
| #10 | const proc = await sandbox.startProcess(`mount | grep "s3fs on ${R2_MOUNT_PATH}"`); |
| #11 | // Wait for the command to complete |
| #12 | let attempts = 0; |
| #13 | while (proc.status === 'running' && attempts < 10) { |
| #14 | await new Promise(r => setTimeout(r, 200)); |
| #15 | attempts++; |
| #16 | } |
| #17 | const logs = await proc.getLogs(); |
| #18 | // If stdout has content, the mount exists |
| #19 | const mounted = !!(logs.stdout && logs.stdout.includes('s3fs')); |
| #20 | console.log('isR2Mounted check:', mounted, 'stdout:', logs.stdout?.slice(0, 100)); |
| #21 | return mounted; |
| #22 | } catch (err) { |
| #23 | console.log('isR2Mounted error:', err); |
| #24 | return false; |
| #25 | } |
| #26 | } |
| #27 | |
| #28 | /** |
| #29 | * Mount R2 bucket for persistent storage |
| #30 | * |
| #31 | * @param sandbox - The sandbox instance |
| #32 | * @param env - Worker environment bindings |
| #33 | * @returns true if mounted successfully, false otherwise |
| #34 | */ |
| #35 | export async function mountR2Storage(sandbox: Sandbox, env: MoltbotEnv): Promise<boolean> { |
| #36 | // Skip if R2 credentials are not configured |
| #37 | if (!env.R2_ACCESS_KEY_ID || !env.R2_SECRET_ACCESS_KEY || !env.CF_ACCOUNT_ID) { |
| #38 | console.log('R2 storage not configured (missing R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, or CF_ACCOUNT_ID)'); |
| #39 | return false; |
| #40 | } |
| #41 | |
| #42 | // Check if already mounted first - this avoids errors and is faster |
| #43 | if (await isR2Mounted(sandbox)) { |
| #44 | console.log('R2 bucket already mounted at', R2_MOUNT_PATH); |
| #45 | return true; |
| #46 | } |
| #47 | |
| #48 | try { |
| #49 | console.log('Mounting R2 bucket at', R2_MOUNT_PATH); |
| #50 | await sandbox.mountBucket(R2_BUCKET_NAME, R2_MOUNT_PATH, { |
| #51 | endpoint: `https://${env.CF_ACCOUNT_ID}.r2.cloudflarestorage.com`, |
| #52 | // Pass credentials explicitly since we use R2_* naming instead of AWS_* |
| #53 | credentials: { |
| #54 | accessKeyId: env.R2_ACCESS_KEY_ID, |
| #55 | secretAccessKey: env.R2_SECRET_ACCESS_KEY, |
| #56 | }, |
| #57 | }); |
| #58 | console.log('R2 bucket mounted successfully - moltbot data will persist across sessions'); |
| #59 | return true; |
| #60 | } catch (err) { |
| #61 | const errorMessage = err instanceof Error ? err.message : String(err); |
| #62 | console.log('R2 mount error:', errorMessage); |
| #63 | |
| #64 | // Check again if it's mounted - the error might be misleading |
| #65 | if (await isR2Mounted(sandbox)) { |
| #66 | console.log('R2 bucket is mounted despite error'); |
| #67 | return true; |
| #68 | } |
| #69 | |
| #70 | // Don't fail if mounting fails - moltbot can still run without persistent storage |
| #71 | console.error('Failed to mount R2 bucket:', err); |
| #72 | return false; |
| #73 | } |
| #74 | } |
| #75 |