Skip to content

Commit 4e25be3

Browse files
committed
get things up and running
Signed-off-by: Robert Landers <landers.robert@gmail.com>
1 parent f842a74 commit 4e25be3

File tree

7 files changed

+412
-286
lines changed

7 files changed

+412
-286
lines changed

cli/auth/resource.go

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
"github.com/bottledcode/durable-php/cli/appcontext"
9-
"github.com/bottledcode/durable-php/cli/glue"
10-
"github.com/bottledcode/durable-php/cli/ids"
11-
"github.com/nats-io/nats.go/jetstream"
12-
"go.uber.org/zap"
138
"net/http"
149
"os"
1510
"slices"
1611
"sync"
1712
"time"
13+
14+
"github.com/bottledcode/durable-php/cli/appcontext"
15+
"github.com/bottledcode/durable-php/cli/glue"
16+
"github.com/bottledcode/durable-php/cli/ids"
17+
"github.com/nats-io/nats.go/jetstream"
18+
"go.uber.org/zap"
1819
)
1920

21+
// LocalMessageSender is a function type for sending local messages
22+
type LocalMessageSender func(method, stateId string, context map[string]interface{}) (interface{}, error)
23+
24+
// SendLocalMessage is a global function pointer that gets set by the build package
25+
var SendLocalMessage LocalMessageSender
26+
2027
// Resource represents a resource with owners, shares, mode, expiration, and revision.
2128
// It allows operations such as updating, sharing ownership, applying permissions,
2229
// checking permissions, and serializing/deserializing to/from bytes.
@@ -139,19 +146,41 @@ func (r *Resource) getOrCreatePermissions(id *ids.StateId, ctx context.Context,
139146
if cached, found := cache.Load(id.Name()); found {
140147
perms = cached.(CreatePermissions)
141148
} else {
142-
result, err := os.CreateTemp("", "")
143-
if err != nil {
144-
return perms, err
145-
}
146-
defer os.Remove(result.Name())
147-
result.Close()
148-
149-
glu := glue.NewGlue(ctx.Value("bootstrap").(string), glue.GetPermissions, make([]any, 0), result.Name())
150-
env := map[string]string{"STATE_ID": id.String()}
151-
_, headers, _, _ := glu.Execute(ctx, make(http.Header), logger, env, nil, id, ids.SystemSource)
152-
data := headers.Get("Permissions")
153-
if err = json.Unmarshal([]byte(data), &perms); err != nil {
154-
return perms, err
149+
if SendLocalMessage != nil {
150+
// Use the new local message system
151+
contextData := map[string]interface{}{
152+
"bootstrap": ctx.Value("bootstrap"),
153+
}
154+
155+
response, err := SendLocalMessage("getPermissions", id.String(), contextData)
156+
if err != nil {
157+
return perms, err
158+
}
159+
160+
// The response should be JSON string
161+
if responseStr, ok := response.(string); ok {
162+
if err = json.Unmarshal([]byte(responseStr), &perms); err != nil {
163+
return perms, err
164+
}
165+
} else {
166+
return perms, errors.New("invalid response format from local message")
167+
}
168+
} else {
169+
// Fallback to old glue system
170+
result, err := os.CreateTemp("", "")
171+
if err != nil {
172+
return perms, err
173+
}
174+
defer os.Remove(result.Name())
175+
result.Close()
176+
177+
glu := glue.NewGlue(ctx.Value("bootstrap").(string), glue.GetPermissions, make([]any, 0), result.Name())
178+
env := map[string]string{"STATE_ID": id.String()}
179+
_, headers, _, _ := glu.Execute(ctx, make(http.Header), logger, env, nil, id, ids.SystemSource)
180+
data := headers.Get("Permissions")
181+
if err = json.Unmarshal([]byte(data), &perms); err != nil {
182+
return perms, err
183+
}
155184
}
156185
cache.Store(id.Name(), perms)
157186
}

cli/ext/build/ext.c

Lines changed: 85 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,28 @@ static inline ext_object *ext_object_from_obj(zend_object *obj) {
2828
}
2929

3030
static zend_object *ext_create_object(zend_class_entry *ce) {
31-
ext_object *intern = ecalloc(1, sizeof(ext_object) + zend_object_properties_size(ce));
31+
fprintf(stderr, "[C DEBUG] ext_create_object called!\n");
32+
fflush(stderr);
33+
34+
/* Always allocate without properties due to corrupted class entry */
35+
ext_object *intern = ecalloc(1, sizeof(ext_object));
36+
37+
fprintf(stderr, "[C DEBUG] About to call zend_object_std_init\n");
38+
fflush(stderr);
3239

3340
zend_object_std_init(&intern->std, ce);
34-
object_properties_init(&intern->std, ce);
41+
42+
fprintf(stderr, "[C DEBUG] zend_object_std_init completed\n");
43+
fflush(stderr);
44+
45+
/* Skip object_properties_init due to corrupted class entry */
3546

3647
intern->std.handlers = &object_handlers_ext;
3748
intern->go_handle = 0; /* will be set in __construct */
3849

50+
fprintf(stderr, "[C DEBUG] ext_create_object returning\n");
51+
fflush(stderr);
52+
3953
return &intern->std;
4054
}
4155

@@ -57,19 +71,43 @@ void init_object_handlers() {
5771
}
5872

5973
static zend_class_entry *Worker_ce = NULL;
60-
static ext_object *current_worker = NULL;
74+
static __thread uintptr_t current_go_worker_handle = 0; /* Thread-local Go worker handle */
6175

6276
PHP_METHOD(Bottledcode_DurablePhp_Ext_Worker, __construct) {
77+
fprintf(stderr, "[C DEBUG] Constructor called\n");
78+
fflush(stderr);
79+
6380
ZEND_PARSE_PARAMETERS_NONE();
6481

82+
fprintf(stderr, "[C DEBUG] Constructor parameters parsed\n");
83+
fflush(stderr);
84+
6585
ext_object *intern = ext_object_from_obj(Z_OBJ_P(ZEND_THIS));
6686

87+
fprintf(stderr, "[C DEBUG] Got intern object, go_handle=%lu\n", intern->go_handle);
88+
fflush(stderr);
89+
6790
/* Constructor is called more than once, make it no-op */
6891
if (intern->go_handle != 0) {
92+
fprintf(stderr, "[C DEBUG] Constructor already called, returning\n");
93+
fflush(stderr);
6994
return;
7095
}
7196

72-
intern->go_handle = create_Worker_object();
97+
/* Use the thread-local Go worker handle if available */
98+
if (current_go_worker_handle != 0) {
99+
fprintf(stderr, "[C DEBUG] Constructor using thread-local handle %lu\n", current_go_worker_handle);
100+
fflush(stderr);
101+
intern->go_handle = current_go_worker_handle;
102+
} else {
103+
/* Fallback: create a new Worker object */
104+
fprintf(stderr, "[C DEBUG] Constructor creating new Worker object\n");
105+
fflush(stderr);
106+
intern->go_handle = create_Worker_object();
107+
}
108+
109+
fprintf(stderr, "[C DEBUG] Constructor completed\n");
110+
fflush(stderr);
73111
}
74112

75113

@@ -220,13 +258,36 @@ PHP_METHOD(Bottledcode_DurablePhp_Ext_Worker, delete) {
220258
}
221259

222260
PHP_METHOD(Bottledcode_DurablePhp_Ext_Worker, GetCurrent) {
261+
fprintf(stderr, "[C DEBUG] GetCurrent method entry\n");
262+
fflush(stderr);
263+
223264
ZEND_PARSE_PARAMETERS_NONE();
224265

225-
if (current_worker == NULL) {
266+
fprintf(stderr, "[C DEBUG] GetCurrent called, thread-local handle=%lu\n", current_go_worker_handle);
267+
fflush(stderr);
268+
269+
/* If no thread-local worker handle, return NULL */
270+
if (current_go_worker_handle == 0) {
271+
fprintf(stderr, "[C DEBUG] No thread-local handle, returning NULL\n");
272+
fflush(stderr);
226273
RETURN_NULL();
227274
}
228275

229-
RETURN_OBJ_COPY(&current_worker->std);
276+
fprintf(stderr, "[C DEBUG] About to create Worker object with object_init_ex\n");
277+
fflush(stderr);
278+
279+
/* Create a new Worker object - PHP constructor will pick up the thread-local handle */
280+
zval obj;
281+
if (object_init_ex(&obj, Worker_ce) != SUCCESS) {
282+
fprintf(stderr, "[C DEBUG] object_init_ex failed\n");
283+
fflush(stderr);
284+
RETURN_NULL();
285+
}
286+
287+
fprintf(stderr, "[C DEBUG] object_init_ex succeeded, returning object\n");
288+
fflush(stderr);
289+
290+
RETURN_ZVAL(&obj, 0, 0);
230291
}
231292

232293
void register_all_classes() {
@@ -241,37 +302,34 @@ void register_all_classes() {
241302

242303
/* Function to set the current worker from Go */
243304
void set_current_worker_handle(uintptr_t handle) {
244-
if (current_worker != NULL) {
245-
/* Release previous worker */
246-
zend_object_release(&current_worker->std);
247-
current_worker = NULL;
248-
}
249-
250-
if (handle != 0) {
251-
/* Create a new Worker object and set its handle */
252-
zend_object *obj = ext_create_object(Worker_ce);
253-
current_worker = ext_object_from_obj(obj);
254-
current_worker->go_handle = handle;
255-
256-
/* Add ref to keep it alive */
257-
GC_ADDREF(&current_worker->std);
258-
}
305+
fprintf(stderr, "[C DEBUG] Setting thread-local worker handle to %lu\n", handle);
306+
fflush(stderr);
307+
current_go_worker_handle = handle;
308+
}
309+
310+
/* Function to get current worker handle */
311+
uintptr_t get_current_worker_handle() {
312+
return current_go_worker_handle;
259313
}
260314

261315
/* Function to clear current worker */
262316
void clear_current_worker() {
263-
if (current_worker != NULL) {
264-
zend_object_release(&current_worker->std);
265-
current_worker = NULL;
266-
}
317+
fprintf(stderr, "[C DEBUG] Clearing thread-local worker handle\n");
318+
fflush(stderr);
319+
current_go_worker_handle = 0;
267320
}
268321

269322
PHP_MINIT_FUNCTION(ext) {
270-
register_all_classes();
323+
fprintf(stderr, "[C DEBUG] MINIT starting\n");
324+
fflush(stderr);
271325

326+
register_all_classes();
327+
fprintf(stderr, "[C DEBUG] Classes registered\n");
328+
fflush(stderr);
272329

273330
go_init_module();
274-
331+
fprintf(stderr, "[C DEBUG] Go module initialized\n");
332+
fflush(stderr);
275333

276334
return SUCCESS;
277335
}

0 commit comments

Comments
 (0)