From 9f84f7c94b9c80b50a6a169153d050acb6bbd9ef Mon Sep 17 00:00:00 2001 From: stevensusas Date: Tue, 19 Nov 2024 17:02:13 -0500 Subject: [PATCH] hello --- backend/routes/userRoutes.js | 82 +++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/backend/routes/userRoutes.js b/backend/routes/userRoutes.js index ba3375c..2287589 100644 --- a/backend/routes/userRoutes.js +++ b/backend/routes/userRoutes.js @@ -467,12 +467,18 @@ router.post('/request-instance', authenticateToken, authenticateUser, async (req await client.query('BEGIN'); - // Find an available instance of the requested type + // Log pre-allocation state + const preCheck = await client.query( + 'SELECT * FROM public.instance WHERE allocateduserid IS NULL AND booted = false LIMIT 1' + ); + console.log('Pre-allocation available instances:', preCheck.rows); + + // Find an available instance with proper casing const instanceCheck = await client.query(` SELECT i.instanceid, it.instancetypeid FROM public.instancetype it JOIN public.instance i ON it.instancetypeid = i.instancetypeid - WHERE it.instancetype = $1 + WHERE LOWER(it.instancetype) = LOWER($1) AND i.allocateduserid IS NULL AND i.booted = false LIMIT 1 @@ -486,52 +492,78 @@ router.post('/request-instance', authenticateToken, authenticateUser, async (req const { instanceid, instancetypeid } = instanceCheck.rows[0]; - // Allocate the instance to the user + // Allocate instance with explicit type casting const allocationResult = await client.query(` - UPDATE public.instance i - SET allocateduserid = $1 - WHERE instanceid = $2 + UPDATE public.instance + SET allocateduserid = $1::integer + WHERE instanceid = $2::integer RETURNING - i.instanceid, - i.instancename, - i.ipaddress, - i.username, - i.password, - i.booted as status, - (SELECT instancetype FROM public.instancetype WHERE instancetypeid = i.instancetypeid) as type, - (SELECT systemtype FROM public.instancetype WHERE instancetypeid = i.instancetypeid) as systemtype, - (SELECT cpucorecount FROM public.instancetype WHERE instancetypeid = i.instancetypeid) as cpucorecount, - (SELECT memory FROM public.instancetype WHERE instancetypeid = i.instancetypeid) as memory, - (SELECT storage FROM public.instancetype WHERE instancetypeid = i.instancetypeid) as storage, - (SELECT pt.priceperhour FROM public.pricetier pt - JOIN public.instancetype it ON pt.pricetierId = it.pricetierId - WHERE it.instancetypeid = i.instancetypeid) as priceperhour + instanceid, + instancename, + ipaddress, + username, + password, + allocateduserid, + booted as status `, [userId, instanceid]); + // Verify allocation + const verifyAllocation = await client.query( + 'SELECT * FROM public.instance WHERE instanceid = $1', + [instanceid] + ); + console.log('Post-allocation instance state:', verifyAllocation.rows[0]); + if (allocationResult.rowCount === 0) { throw new Error('Failed to allocate instance'); } - // Update the free count + // Update instance type count await client.query(` UPDATE public.instancetype - SET free_count = free_count - 1 + SET free_count = GREATEST(free_count - 1, 0) WHERE instancetypeid = $1 `, [instancetypeid]); + // Get full instance details + const instanceDetails = await client.query(` + SELECT + i.instanceid, + i.instancename, + i.ipaddress, + i.username, + i.password, + i.allocateduserid, + i.booted as status, + it.instancetype as type, + it.systemtype, + it.cpucorecount, + it.memory, + it.storage, + pt.priceperhour + FROM public.instance i + JOIN public.instancetype it ON i.instancetypeid = it.instancetypeid + JOIN public.pricetier pt ON it.pricetierId = pt.pricetierId + WHERE i.instanceid = $1 + `, [instanceid]); + await client.query('COMMIT'); - console.log('Instance allocated:', allocationResult.rows[0]); + console.log('Final allocated instance:', instanceDetails.rows[0]); res.json({ message: 'Instance allocated successfully', - instance: allocationResult.rows[0] + instance: instanceDetails.rows[0] }); } catch (err) { console.error('Transaction error:', err); await client.query('ROLLBACK'); - res.status(500).json({ message: 'Error allocating instance', error: err.message }); + res.status(500).json({ + message: 'Error allocating instance', + error: err.message, + userId: req.user.userId + }); } finally { client.release(); }