pulsar-client 1.13.0-rc.2 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,8 +17,12 @@
17
17
  # specific language governing permissions and limitations
18
18
  # under the License.
19
19
  #
20
-
21
- import sys, json, urllib.request, os, shutil, zipfile, tempfile
20
+ import sys
21
+ import requests
22
+ import os
23
+ import shutil
24
+ import zipfile
25
+ import tempfile
22
26
  from pathlib import Path
23
27
 
24
28
  if len(sys.argv) != 3:
@@ -39,36 +43,35 @@ workflow_run_id = int(sys.argv[1])
39
43
  dest_path = sys.argv[2]
40
44
 
41
45
  workflow_run_url = LIST_URL % workflow_run_id
42
- request = urllib.request.Request(workflow_run_url,
43
- headers={'Accept': ACCEPT_HEADER, 'Authorization': 'Bearer ' + GITHUB_TOKEN})
44
- with urllib.request.urlopen(request) as response:
45
- data = json.loads(response.read().decode("utf-8"))
46
- for artifact in data['artifacts']:
47
- name = artifact['name']
48
- url = artifact['archive_download_url']
46
+ headers = {'Accept': ACCEPT_HEADER, 'Authorization': f'Bearer {GITHUB_TOKEN}'}
49
47
 
50
- print('Downloading %s from %s' % (name, url))
51
- artifact_request = urllib.request.Request(url,
52
- headers={'Authorization': 'Bearer ' + GITHUB_TOKEN})
53
- with urllib.request.urlopen(artifact_request) as response:
54
- tmp_zip = tempfile.NamedTemporaryFile(delete=False)
55
- try:
56
- #
57
- shutil.copyfileobj(response, tmp_zip)
58
- tmp_zip.close()
48
+ response = requests.get(workflow_run_url, headers=headers)
49
+ response.raise_for_status()
59
50
 
60
- dest_dir = os.path.join(dest_path, name)
61
- Path(dest_dir).mkdir(parents=True, exist_ok=True)
62
- with zipfile.ZipFile(tmp_zip.name, 'r') as z:
63
- z.extractall(dest_dir)
64
- finally:
65
- os.unlink(tmp_zip.name)
51
+ data = response.json()
52
+ for artifact in data['artifacts']:
53
+ name = artifact['name']
54
+ url = artifact['archive_download_url']
66
55
 
67
- for root, dirs, files in os.walk(dest_path, topdown=False):
68
- for name in files:
69
- shutil.move(os.path.join(root, name), dest_path)
70
- if not os.listdir(root):
71
- os.rmdir(root)
56
+ print(f'Downloading {name} from {url}')
57
+ artifact_response = requests.get(url, headers=headers, stream=True)
58
+ artifact_response.raise_for_status()
72
59
 
60
+ with tempfile.NamedTemporaryFile(delete=False) as tmp_zip:
61
+ for chunk in artifact_response.iter_content(chunk_size=8192):
62
+ tmp_zip.write(chunk)
63
+ tmp_zip_path = tmp_zip.name
73
64
 
65
+ try:
66
+ dest_dir = os.path.join(dest_path, name)
67
+ Path(dest_dir).mkdir(parents=True, exist_ok=True)
68
+ with zipfile.ZipFile(tmp_zip_path, 'r') as z:
69
+ z.extractall(dest_dir)
70
+ finally:
71
+ os.unlink(tmp_zip_path)
74
72
 
73
+ for root, dirs, files in os.walk(dest_path, topdown=False):
74
+ for name in files:
75
+ shutil.move(os.path.join(root, name), dest_path)
76
+ if not os.listdir(root):
77
+ os.rmdir(root)
@@ -20,7 +20,7 @@
20
20
 
21
21
  set -e -x
22
22
 
23
- if [ $# -neq 2 ]; then
23
+ if [ "$#" -ne 2 ]; then
24
24
  echo "Usage: $0 \$DEST_PATH \$WORKFLOW_ID"
25
25
  exit 1
26
26
  fi
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pulsar-client",
3
- "version": "1.13.0-rc.2",
3
+ "version": "1.13.0",
4
4
  "description": "Pulsar Node.js client",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/src/Consumer.cc CHANGED
@@ -77,32 +77,28 @@ void MessageListenerProxy(Napi::Env env, Napi::Function jsCallback, MessageListe
77
77
  Napi::Object msg = Message::NewInstance({}, data->cMessage);
78
78
  Consumer *consumer = data->consumer;
79
79
 
80
- // `consumer` might be null in certain cases, segmentation fault might happend without this null check. We
81
- // need to handle this rare case in future.
82
- if (consumer) {
83
- Napi::Value ret;
84
- try {
85
- ret = jsCallback.Call({msg, consumer->Value()});
86
- } catch (std::exception &exception) {
87
- logMessageListenerError(consumer, exception.what());
88
- }
80
+ Napi::Value ret;
81
+ try {
82
+ ret = jsCallback.Call({msg, consumer->Value()});
83
+ } catch (std::exception &exception) {
84
+ logMessageListenerError(consumer, exception.what());
85
+ }
89
86
 
90
- if (ret.IsPromise()) {
91
- Napi::Promise promise = ret.As<Napi::Promise>();
92
- Napi::Function catchFunc = promise.Get("catch").As<Napi::Function>();
87
+ if (ret.IsPromise()) {
88
+ Napi::Promise promise = ret.As<Napi::Promise>();
89
+ Napi::Function catchFunc = promise.Get("catch").As<Napi::Function>();
93
90
 
94
- ret = catchFunc.Call(promise, {Napi::Function::New(env, [consumer](const Napi::CallbackInfo &info) {
95
- Napi::Error error = info[0].As<Napi::Error>();
96
- logMessageListenerError(consumer, error.what());
97
- })});
91
+ ret = catchFunc.Call(promise, {Napi::Function::New(env, [consumer](const Napi::CallbackInfo &info) {
92
+ Napi::Error error = info[0].As<Napi::Error>();
93
+ logMessageListenerError(consumer, error.what());
94
+ })});
98
95
 
99
- promise = ret.As<Napi::Promise>();
100
- Napi::Function finallyFunc = promise.Get("finally").As<Napi::Function>();
96
+ promise = ret.As<Napi::Promise>();
97
+ Napi::Function finallyFunc = promise.Get("finally").As<Napi::Function>();
101
98
 
102
- finallyFunc.Call(
103
- promise, {Napi::Function::New(env, [data](const Napi::CallbackInfo &info) { data->callback(); })});
104
- return;
105
- }
99
+ finallyFunc.Call(
100
+ promise, {Napi::Function::New(env, [data](const Napi::CallbackInfo &info) { data->callback(); })});
101
+ return;
106
102
  }
107
103
  data->callback();
108
104
  }
@@ -111,7 +107,7 @@ void MessageListener(pulsar_consumer_t *rawConsumer, pulsar_message_t *rawMessag
111
107
  std::shared_ptr<pulsar_message_t> cMessage(rawMessage, pulsar_message_free);
112
108
  MessageListenerCallback *listenerCallback = (MessageListenerCallback *)ctx;
113
109
 
114
- Consumer *consumer = (Consumer *)listenerCallback->consumer;
110
+ Consumer *consumer = static_cast<Consumer *>(listenerCallback->consumerFuture.get());
115
111
 
116
112
  if (listenerCallback->callback.Acquire() != napi_ok) {
117
113
  return;
@@ -135,7 +131,7 @@ void Consumer::SetListenerCallback(MessageListenerCallback *listener) {
135
131
  }
136
132
 
137
133
  if (listener != nullptr) {
138
- listener->consumer = this;
134
+ listener->consumerPromise.set_value(this);
139
135
  // If a consumer listener is set, the Consumer instance is kept alive even if it goes out of scope in JS
140
136
  // code.
141
137
  this->Ref();
@@ -168,11 +164,6 @@ struct ConsumerNewInstanceContext {
168
164
  auto cConsumer = std::shared_ptr<pulsar_consumer_t>(rawConsumer, pulsar_consumer_free);
169
165
  auto listener = consumerConfig->GetListenerCallback();
170
166
 
171
- if (listener) {
172
- // pause, will resume in OnOK, to prevent MessageListener get a nullptr of consumer
173
- pulsar_consumer_pause_message_listener(cConsumer.get());
174
- }
175
-
176
167
  deferred->Resolve([cConsumer, consumerConfig, listener](const Napi::Env env) {
177
168
  Napi::Object obj = Consumer::constructor.New({});
178
169
  Consumer *consumer = Consumer::Unwrap(obj);
@@ -180,11 +171,6 @@ struct ConsumerNewInstanceContext {
180
171
  consumer->SetCConsumer(cConsumer);
181
172
  consumer->SetListenerCallback(listener);
182
173
 
183
- if (listener) {
184
- // resume to enable MessageListener function callback
185
- resume_message_listener(cConsumer.get());
186
- }
187
-
188
174
  return obj;
189
175
  });
190
176
  }
@@ -21,14 +21,16 @@
21
21
  #define MESSAGELISTENER_H
22
22
 
23
23
  #include <napi.h>
24
+ #include <future>
24
25
 
25
26
  struct MessageListenerCallback {
26
27
  Napi::ThreadSafeFunction callback;
27
28
 
28
- // Using consumer as void* since the ListenerCallback is shared between Config and Consumer.
29
- void *consumer;
29
+ // Use future store consumer point, because need ensure sync.
30
+ std::promise<void *> consumerPromise;
31
+ std::shared_future<void *> consumerFuture;
30
32
 
31
- MessageListenerCallback() : consumer(nullptr) {}
33
+ MessageListenerCallback() : consumerPromise(), consumerFuture(consumerPromise.get_future()) {}
32
34
  };
33
35
 
34
36
  #endif